1
votes

I want to color the line but not finding suitable API to do it in OpenGL es 2.0

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

With above code, I am able to draw the line with some width. Now I want to color the same line. Can someone please guide me with the API ?

1
It's your fragment shader that determines the output color. Simply change the fragment shader to output the color you want. Or, if you want to be able to change, introduce a uniform variable you can set.Reto Koradi
Hey Reto, Thanks. I am new and learning openGL, Can you please help me with code snippet ? suppose I want to set yellow color to line, how would I do it.Mohan Kumar
Hi Reto. How can I do it ? please provide some inputsMohan Kumar

1 Answers

1
votes

You provided very little information about what you are using. The short version of solution would be to add the uniform into your fragment shader that represents the color and then output the color in the fragment shader. At least seeing your shaders might help.

So to see where the shader color output is:

gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // Will output white color (RGBA values)

To change it something you can modify you will need to add the uniform so your shader will look something like this:

uniform lowp vec4 uniformColor;

void main() {
    gl_FragColor = uniformColor;
}

Now this means you can control the uniformColor from the CPU using openGL API . You need to use the current program, find the uniform location and pass the value you want to set as a color:

    GLuint myShaderProgram; // Your program ID you got when creating the program.
    GLfloat colorToSet[4] = {1.0f, .0f, .0f, 1.0f}; // Set whatever color, this should output red.
    glUseProgram(myShaderProgram);
    int uniformLocation = glGetUniformLocation(myShaderProgram, "uniformColor");
    if(uniformLocation < 0) {
        // Uniform locations must be 0 or greater, otherwise the uniform was not found in the shader or some other error occured
        // TODO: handle exception
    }
    else {
        glUniform4f(uniformLocation, colorToSet[0], colorToSet[1], colorToSet[2], colorToSet[3]);
    }

    ... continue with drawing (glDrawArrays) ...