Ok, so we can create a shader program, and render a shape with a specified color. Lets move into lighting. In this tutorial, I am going to show you how to perform per vertex lighting, which is then easily modified to achieve per pixel lighting in the next tutorial.
Lets get moving 🙂
There are going to be quite a few additions to this code, but you might recognise most of it from the lighting tutorials.
I am creating a directional light, that points in the direction [0,1,1]. It has a diffuse color of white, and an ambient color of black.
I have placed the calls to setup my lighting inside of a new method, which is called during our display method, *after* we set the camera position. I have a tip on the OpenGL Tips page, that explains why we do this.
Another minor change, is that I am now using a glutSolidSphere, I have kept it to a low polygon count, only 10 stacks and 10 slices. This is to show how per vertex will differ to per pixel lighting later on.
But enough of that, lets move onto our shaders. You should already know how to setup lighting in a standard OpenGL application.
Vertex Shader Source:
For this tutorial, I am going show you how to perform per
First I am going to teach you about varying variables. These
Because we are using per vertex lighting, the GLSL shader will
Now first off we need to know the surface normal of the current
Now as for our normal, I am storing it in the variable vec3 vertex_normal.
vec3 vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
Next on, we are going to work with our light. I am storing this
vec3 vertex_light_position = gl_LightSource.position.xyz;
This gives us the position of glLight0, and to get the position of
Now that we have our vertex_normal and the position of our vertex_light_position.
diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);
And from that, we have now calculated the intensity of the light
Fragment Shader Source:
Because we are using the varying float Diffuse in
varying float diffuse_value;
After that, because I am building on the last tutorial, we will multiply
gl_FragColor = gl_Color * diffuse_value;
Once you get this running, it should show a sphere that gets darker down the bottom, or if you add a camera, it will be dark behind as well. Which you can see in the image at the top of the page.
If you have any questions, please email me at email@example.com