3. GLSL Coloring
Jump To:
main.cpp Source
shader.vert Source
shader.frag Source
Download
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.
Main.CPP File: 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. glutSolidCube(2);
|
||||
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 swiftless@gmail.com
|
||||
Download: Download shader.h Source Code for this Tutorial Download shader.cpp Source Code for this Tutorial Download main.cpp Source Code for this Tutorial |
hello sir,
Can we assign gl_FragColor is fragment shader a value given from user dynamically in runtime? say I get the Color value from a textbox and pass it to fragment shader code by some mean and use it to render color. Please help me I need to accomplish it.
Thank you.
Arpan
Hi just wanted to give you a brief heads up and
let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different browsers and both show the same outcome.
Thanks for this! I was wondering why gl_Color was always 0,0,0,0 in my fragment shader. I didn’t realize it was interpolated from the vertex shader based on gl_FrontColor.
Hi Swiftless,
Every thing works great, but why am I seeing a White cube instead of a Red cube as set by glColor4f();
Hi Paras,
My guess would be that your shader isn’t loading correctly. Other than that, if the code is the same as the tutorial, it should work.
Thanks,
Swiftless
Finally, a tutorial which uses some old-school direct render calls, glVertex*, glColor*, with GL3 shaders!
This really solved my problem!
I agree with Steve, I’d like to see how to write and use a shader that works with direct glVertex*, glColor*, but does not use compatibility profile attribute variables.
I’d like to see how I could attach glVertex* and glColor* to some named attribute in a GLSL shader.
Hi Mr. What,
In the OpenGL 4 tutorials, I have named attributes which are sent to the shaders, however these don’t link to glVertex and glColor calls which have since been removed. They instead link to a VBO which stores the data.
In OpenGL 2.x and prior, glVertex and glColor calls were automatically mapped by OpenGL to GLSL variables gl_Color and gl_Vertex.
Cheers,
Swiftless
hm – all tutorials i read so far are using compatibility profile.
Build ins like gl_Color, gl_ModelViewProjectionMatrix, gl_Vertex … are all deprecated and removed in core profiles. I would appreciate it to read tutorials for core-profiles, not for compatibility profile using deprecated variables and functions.
Hey Steve,
I think you might want to check out the OpenGL 4 tutorials mate 😉
Cheers,
Swiftless