0
votes

I have shader code like below:

static const char s_v_shader[] =
"attribute vec4 vPosition; \n"
"attribute vec2 my_Texcoor;     \n"
"uniform   mat4 u_TransMatrix;   \n"
"varying vec2 vTexcoor;         \n"
"void main() \n"
"{ \n"
"  vTexcoor = my_Texcoor;       \n"
" gl_Position = u_TransMatrix*vPosition; \n"
"} \n";

static const char s_f_shader[] =
"precision mediump float;\n"
"uniform sampler2D my_Sampler;                    \n"
"varying vec2 vTexcoor;                           \n"

"void main() \n"
"{ \n"
" vec4 tex = texture2D(my_Sampler, vTexcoor);    \n"
"  gl_FragColor = tex;                            \n"
//" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";

I want to draw line on this texture and fill red color to the line. I am able to draw the line but color is always black.

Please someone help me to color the line with red or yellow or green.

3
Anybody there please? I am stuck completely. Someone can help me please ? I know without help I can't progressMohan Kumar

3 Answers

0
votes

You can not draw to a texture. You may draw to a FBO (frame buffer object) which has an attached texture.

To achieve this you simply need to generate a new frame buffer GLuint bufferID; glGenFramebuffers(1, &bufferID); then you need to bind the buffer and attach the texture to it glBindFramebuffer(GL_FRAMEBUFFER, bufferID) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0). Now you can normally draw to this buffer using your commands to draw the line. In this case there is no need to use a texture in the shader at all, you only need to output the color. But this only draws to the texture, if you need to show it then you will need to bind your main buffer and use the shader with the texture and draw a full screen rectangle with the texture you have.

On the other hand if you only want to draw the texture and the line over it then you need 2 draw calls. First draw the texture in the background by drawing a rectangle with the texture in the shader. Then draw a line with a shader that only takes in the color, no textures there.

0
votes
With reference to above shaders, below is the code

vertShaderNum = glCreateShader(GL_VERTEX_SHADER);
pixelShaderNum = glCreateShader(GL_FRAGMENT_SHADER);
CompileShader(s_v_shader, vertShaderNum);
CompileShader(s_f_shader, pixelShaderNum);
programHandle = glCreateProgram();
glAttachShader(programHandle, vertShaderNum);
glAttachShader(programHandle, pixelShaderNum);
glBindAttribLocation ( programHandle, 0, "vPosition" );
glLinkProgram(programHandle);
glUseProgram(programHandle);

locVertices = glGetAttribLocation(programHandle, "vPosition");
locTexcoord = glGetAttribLocation(programHandle, "my_Texcoor");
locTransformMat = glGetUniformLocation(programHandle, "u_TransMatrix");
locSampler = glGetUniformLocation(programHandle, "my_Sampler");

glGenTextures(1, &gTexObj);
        glBindTexture(GL_TEXTURE_2D, gTexObj);

const GLfloat vertices[][2] = {
                        { -1.0f, -1.0f},
                        {  1.0f, -1.0f},
                        { -1.0f, 1.0f},
                        {  1.0f,  1.0f}
                };

const GLfloat texcoords[][2] = {
        { 0.0f, 1.0f},
        { 1.0f, 1.0f},
        { 0.0f, 0.0f},
        { 1.0f, 0.0f}
};
GLfloat transformMatrix[16] =
{
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f
};
glUniformMatrix4fv(locTransformMat, 1, GL_FALSE, transformMatrix);
glUniform1i(locSampler, 0);

Now in infinite loop
While(1)
{

glEnableVertexAttribArray(locVertices);
        glEnableVertexAttribArray(locTexcoord);

        // set data in the arrays.
        glVertexAttribPointer(locVertices, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0][0]);
        glVertexAttribPointer(locTexcoord, 2, GL_FLOAT, GL_FALSE, 0, &texcoords[0][0]);

=========> I am rendering camera video logic......................

float vVertices[] =
        {
                -0.85f, -0.9f, -0.6f, -0.5f,
        };
        glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, vVertices );
        glEnableVertexAttribArray ( 0 );
        lDrawArrays ( GL_LINES , 0, 2 );
        glLineWidth( width_test );

Till now everything is good. camera Video is rendered on display.
Line is drawn on Video texture.
Now I want to color the line............. I am stuck as there is no API call support to color the line in opengles 2.0....
0
votes
@Matic
static const char s_v_shader[] =
"attribute vec4 vPosition; \n"
"attribute vec2 my_Texcoor;     \n"
"uniform   mat4 u_TransMatrix;   \n"
"varying vec2 vTexcoor;         \n"
"void main() \n"
"{ \n"
"  vTexcoor = my_Texcoor;       \n"
" gl_Position = u_TransMatrix*vPosition; \n"
"} \n";
static const char s_f_shader[] =
"precision mediump float;\n"
"uniform lowp vec4 Texture;  \n "
"uniform sampler2D my_Sampler;                    \n"
"varying vec2 vTexcoor;                           \n"

"void main() \n"
"{ \n"
" vec4 tex = texture2D(my_Sampler, vTexcoor);    \n"
"  gl_FragColor = tex;                            \n"
"} \n";
#endif

static const char s_v_shader_new[] =
"attribute vec4 vPosition_new; \n"
"attribute vec2 my_Texcoor_new;     \n"
"uniform   mat4 u_TransMatrix_new;   \n"
"varying vec2 vTexcoor_new;         \n"
"void main() \n"
"{ \n"
"  vTexcoor_new = my_Texcoor_new;       \n"
" gl_Position = u_TransMatrix_new*vPosition_new; \n"
"} \n";


static const char s_f_shader_new[] =
"precision mediump float;\n"
//"uniform sampler2D my_Sampler_new;                    \n"
"uniform lowp vec4 uniformColor;\n"
//"uniform vec4 uniformColor;\n"
"varying vec2 vTexcoor_new;                           \n"

"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";
#endif

const GLfloat vertices[][2] = {
    { -1.0f, -1.0f},
    {  1.0f, -1.0f},
    { -1.0f, 1.0f},
    {  1.0f,  1.0f}
};

const GLfloat texcoords[][2] = {
    { 0.0f, 1.0f},
    { 1.0f, 1.0f},
    { 0.0f, 0.0f},
    { 1.0f, 0.0f}
};

// Triangle Vertex colors.
const GLfloat color[][3] = {
    {1.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 1.0f},
    {1.0f, 1.0f, 0.0f}
};

// Start with an identity matrix.
GLfloat transformMatrix[16] =
{
    1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f
};

void LoadShaders_new(const char * vShader, const char * pShader)
{
        vertShaderNum_new = glCreateShader(GL_VERTEX_SHADER);
        pixelShaderNum_new = glCreateShader(GL_FRAGMENT_SHADER);
#if 1
        if (CompileShader(vShader, vertShaderNum_new) == 0)
        {
                printf("%d: PS compile failed.\n", __LINE__);
                return;
        }
#endif
        if (CompileShader(pShader, pixelShaderNum_new) == 0)
        {
                printf("%d: VS compile failed.\n", __LINE__);
                return;
        }

        programHandle_new = glCreateProgram();

        glAttachShader(programHandle_new, vertShaderNum_new);
        glAttachShader(programHandle_new, pixelShaderNum_new);

        // Bind vPosition to attribute 0
     //   glBindAttribLocation ( programHandle_new, 0, "vPosition_new" );
#if 1
    glLinkProgram(programHandle_new);
        // Check if linking succeeded.
        GLint linked = 0;
        glGetProgramiv(programHandle_new, GL_LINK_STATUS, &linked);
        if (!linked)
        {
                printf("%d: Link failed.\n", __LINE__);
                // Retrieve error buffer size.
                GLint errorBufSize, errorLength;
                glGetShaderiv(programHandle_new, GL_INFO_LOG_LENGTH, &errorBufSize);

                char * infoLog = (char*)malloc(errorBufSize * sizeof (char) + 1);
                if (infoLog)
                {
                        // Retrieve error.
                        glGetProgramInfoLog(programHandle_new, errorBufSize, &errorLength, infoLog);
                        infoLog[errorBufSize + 1] = '\0';
                        fprintf(stderr, "%s", infoLog);

                        free(infoLog);
                }

                return;
        }
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     //   glUseProgram(programHandle_new);
#endif
}



// Wrapper to load vetex and pixel shader.
void LoadShaders(const char * vShader, const char * pShader)
{
    vertShaderNum = glCreateShader(GL_VERTEX_SHADER);
    pixelShaderNum = glCreateShader(GL_FRAGMENT_SHADER);

    if (CompileShader(vShader, vertShaderNum) == 0)
    {
        printf("%d: PS compile failed.\n", __LINE__);
        return;
    }

    if (CompileShader(pShader, pixelShaderNum) == 0)
    {
        printf("%d: VS compile failed.\n", __LINE__);
        return;
    }

    programHandle = glCreateProgram();

    glAttachShader(programHandle, vertShaderNum);
    glAttachShader(programHandle, pixelShaderNum);

    // Bind vPosition to attribute 0
    glBindAttribLocation ( programHandle, 0, "vPosition" );


    glLinkProgram(programHandle);
    // Check if linking succeeded.
    GLint linked = 0;
    glGetProgramiv(programHandle, GL_LINK_STATUS, &linked);
    if (!linked)
    {
        printf("%d: Link failed.\n", __LINE__);
        // Retrieve error buffer size.
        GLint errorBufSize, errorLength;
        glGetShaderiv(programHandle, GL_INFO_LOG_LENGTH, &errorBufSize);

        char * infoLog = (char*)malloc(errorBufSize * sizeof (char) + 1);
        if (infoLog)
        {
            // Retrieve error.
            glGetProgramInfoLog(programHandle, errorBufSize, &errorLength, infoLog);
            infoLog[errorBufSize + 1] = '\0';
            fprintf(stderr, "%s", infoLog);

            free(infoLog);
        }

        return;
    }
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glUseProgram(programHandle);
}


main()

{

LoadShaders(s_v_shader, s_f_shader);
LoadShaders_new(s_v_shader_new, s_f_shader_new);

// Grab location of shader attributes.
    locVertices = glGetAttribLocation(programHandle, "vPosition");
    //locColors   = glGetAttribLocation(programHandle, "my_Color");
    locTexcoord = glGetAttribLocation(programHandle, "my_Texcoor");
    // Transform Matrix is uniform for all vertices here.
    locTransformMat = glGetUniformLocation(programHandle, "u_TransMatrix");
    locSampler = glGetUniformLocation(programHandle, "my_Sampler");

    /* Create the texture. */
    glGenTextures(1, &gTexObj);
    glBindTexture(GL_TEXTURE_2D, gTexObj);
    if (gTexObj == 0)
    {
        printf("Could not load the texture \n");
        return -1;
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);    

    glUniformMatrix4fv(locTransformMat, 1, GL_FALSE, transformMatrix);


    glUniform1i(locSampler, 0);


    glClearColor(0.0f, 0.5f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);


while(1)


{
        glEnableVertexAttribArray(locVertices);
    glEnableVertexAttribArray(locTexcoord);

    // set data in the arrays.
    glVertexAttribPointer(locVertices, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0][0]);
    glVertexAttribPointer(locTexcoord, 2, GL_FLOAT, GL_FALSE, 0, &texcoords[0][0]);

>>>>>>>>>>>>>>>>>>>>>> Render Video logic on Texture <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<



float vVertices[] =
    {
        -0.85f, -0.9f, -0.6f, -0.5f,
    };


    glUseProgram(programHandle_new); >>>>>>>>>>>>>>>>>>>>>>> Video render stops display is not streaming
    glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, vVertices );
        glEnableVertexAttribArray (0 );


    glDrawArrays ( GL_LINES , 0, 2 );
    glLineWidth( width_test );


}



}


}