Mar 10
25
Next: 6. GLSL Materials
Jump To:
main.cpp Source
shader.vert Source
shader.frag Source
Download
This tutorial is going to be quite simple. To achieve the effect of per pixel lighting, we simply need to move one calculation from our vertex shader to our fragment shader.
So lets go to it!
|
Main.CPP File: Just for the sake of it, I have switched to a glutSolidTeapot for this tutorial. It will come on handy when testing different effects.
|
||||
|
Vertex Shader Source: In the last tutorial, to achieve lighting, we had one varying variable called diffuse_value. In this tutorial, we are going to remove this varying, and create two others. One of them is going to contain the vertex_light_position, and the other is going to contain the vertex_normal: varying vec3 vertex_light_position; Now we need to store some values in here. This is collected the exact same way as last time: vertex_normal = normalize(gl_NormalMatrix * gl_Normal); And that’s all their is in the vertex shader. Note that I have removed the diffuse_value variable.
|
||||
|
Fragment Shader Source: Since I changed the varying variables in the Vertex Shader, I also need to update them in the fragment shader: varying vec3 vertex_light_position; Now, if you remember the diffuse_value calculation from the previous tutorial, it has been pretty much copied and pasted into this tutorial, only now it resides inside the fragment shader: float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0); And that is all their is to it. We now have per pixel lighting. That wasn’t too hard 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 |