0
votes

At the moment I have simple fragment shader which returns one color (red). If I want to change it a different RGBA color from C code, how should I be doing that?

Is it possible to change an attribute within the fragment shader from C directly or should I be changing a solid color attribute in my vertex shader and then passing that color to the fragment shader? I'm drawing single solid colour rectangles - nothing special.

void main()
{
  gl_FragColor = vec4( 1.0, 0, 0, 1 );"
}
1

1 Answers

2
votes

If you are talking about generating the shader at runtime, then you COULD use the c string formatting functions to insert the color into the line "gl_FragColor..."

I would not recommend you do this since it will be unneccessary work. The standard method to doing this is using uniforms as so:

// fragment shader:
uniform vec3 my_color; // A UNIFORM

void main()
{
    gl_FragColor.rgb = my_color;
    gl_FragColor.a = 1; // the alpha component
}

// your rendering code:
glUseProgram(SHADER_ID);
....
GLint color_location = glGetUniformLocation(SHADER_ID, "my_color");
float color[3] = {r, g, b};
glUniform3fv(color_location, 1, color);
....
glDrawArrays(....);