Shading the objects in my scene wasn’t difficult, but it took me a while to perfect. To start, I initialized the three different ShaderPrograms in my scene so I can set each individual shader to one of these three later. My three programs are for diffuse lighting, specular lighting, and my terrain.
After doing this, I simply applied the programs to each object. I followed the guidelines in the videos for the most part for specular and diffuse lighting, but I changed a few things. For example, I played around with how much ambience their was and how shiny my objects were. My specular shader looks like the following:
The biggest problem I had was setting my light source. Originally I set the light source as a const vec3 in each individual shader, but when I moved the camera the light source would follow my view. This meant that objects would always appear lit from the camera’s perspective. After a lot of playing around I realized the light source was in eye space and thus would always be in the same position relative to the camera. I fixed this by creating a Vector4 in my walkabout renderer that stored the light position, and each time onDraw() is called the light position is multiplied by the view matrix. Light position then became a uniform instead of a const vec3, and doing this allowed the light position to remain in the same position. I had previously tried normalizing the light source and multiplying the light source by xform, but neither of these worked, and looking back I’m not surprised.
The last touch I added was to set the light source to rotate around the scene’s y-axis. This was just because I wanted to play around with it’s position, and I’ll probably set it back to being constant for future parts of the project.
And lastly, once again here is my video. The shading looks better this time…