3. GLSL Coloring
From now on, I won’t be listing the source code for the shader.h and shader.cpp files, as these will not be changing. We have everything we need in place to play with our shaders.
What we are trying to do in this tutorial is simulate our fixed function pipeline in GLSL. In standard OpenGL, we are used to coloring objects with their respective glColor3f or glColor4f methods.
This tutorial will allow us to use the values assigned as the color, inside our shader.
Now this code, isn’t going to change at all, but the key line to focus on is:
glColor4f(1.0, 0.0, 0.0, 1.0);
Changing this line will change the output colour of the fragment shader.
And I have also changed from using a wire cube, just to show the color of the shape more clearly.
Vertex Shader Source:
Our change here, is going to be the introduction of a new line. This line will look like:
gl_FrontColor = gl_Color;
And what this means, is that we want the set the front face color for our objects, to that of the color entered in by the glColor3f and glColor4f functions.
If we have a shape with some back facing shapes, then we can also set gl_BackColor to the same value, and this will give us the same color for both front and back facing shapes. Or you could even change this value, so that the front of a shape has one color, and the back of a shape has another color.
Fragment Shader Source:
The only difference here, is the replacement of our vec4(1.0) color value, with the value gl_Color.
*Note that the gl_Color read in by the vertex shader, is different to that of the gl_Color value read in by the fragment shader. The one in the fragment shader, refers to the gl_Color value we get from the vertex shader, whilst the gl_Color value in our vertex shader is read in from our application.*
And that is all there is to it. It is often handy to set the colours, lighting values, etc in regular OpenGL mode, so that if our shader fails on a certain system, we will still get a similar result. It just won’t look as pretty.
If you have any questions, please email me at email@example.com