Right I’ll dive straight in here, basically I’m trying to write a simple diffuse lighting shader, using the ‘dot product of incident beam and normal’ method, but have been having some problems, so to try elimate them I’ve written a simple shader to show what my normals are doing:
vertex shader:
varying vec3 normal;
void main()
{
normal = normalize(gl_Normal);
gl_Position = ftransform();
}
fragment shader:
varying vec3 normal;
void main()
{
vec3 n=normalize(normal);
vec4 color = vec4(n.xyz,1.0);
gl_FragColor = color;
}
Basically the idea is to colour a polygon based on where its normal is pointing, which does work fine, but when I rotate the model, the colours do not change, indicating that these aren’t the normals in ‘world space’ (sorry if I get terminology wrong, I’m new to the 3d scene really).
This is causing the model’s lighting to not depend on its orientation, which is obviously wrong.
Just in case it helps, the code regarding the rotation of the model is:
void drawPlayer(){
glPushMatrix();
glTranslatef(player.x, player.y, player.z);
glRotatef(-player.rotation/PIOVER180,0,1,0);
glPushMatrix();
glRotatef(180,0,1,0); //stands the model upright
if(model.bLoaded==true){model.draw();}
glPopMatrix();
glPopMatrix();
I had a suggestion before to replace normalize(gl_Normal) with normalize(gl_NormalMatrix * gl_Normal), but I have found that this then makes the normal direction dependant on the viewing angle, which will produce incorrect diffuse lighting.
Any help would be much appreciated, I’m completely stumped on this one. I’ve tried implementing many different shaders I’ve sourced from around the net, but nothing works so far.