Does gluLookAt hate GLSL lighting ?

Some days ago, I tried to implement a simple per vertex lighting shader in my program, and I had a lot of problems with the specular lighting, I just didn’t managed to get it to work properly.
Here is my code :

float Shininess = 10.0;

void main()
	gl_Position = ftransform();
	vec3 Position = vec3(gl_ModelViewMatrix * gl_Vertex);
	vec3 Normal = normalize(gl_NormalMatrix * gl_Normal);
	vec3 Color =;
	vec3 ToLightVector = normalize(gl_LightSource[0] - Position);
	vec3 ToEyeVector = normalize(- Position);
	vec3 LightReflectVector = reflect(-ToLightVector, Normal);
	vec3 AmbientColor = Color * gl_LightSource[0].ambient.rgb;
	vec3 DiffuseColor = Color * gl_LightSource[0].diffuse.rgb * abs(dot(ToLightVector, Normal));
	vec3 SpecularColor = Color * gl_LightSource[0].specular.rgb * pow(max(dot(LightReflectVector, ToEyeVector), 0.0), Shininess);

	gl_FrontColor.rgb = AmbientColor + DiffuseColor + SpecularColor;
	gl_FrontColor.a = gl_Color.a;

After “debugging”, I found that it was the ToEyeVector that caused the problem, which explains that the ambientColor and diffuseColor were correct.

And I finally found (much time later) that deleting my gluLookAt line would make everything looks ok.
There is no problem anymore (since I don’t use gluLookAt anymore :stuck_out_tongue: ), but I just wanted to post this for other people who may have the same problem.
I’m also asking if anyone has any idea on the real problem with this damn gluLookAt :mad:
Thank you in advance.

I doubt anything is wrong with gluLookAt. It’s used by literally thousands of people in conjunction with GLSL, so I don’t think that’s the problem. I’ve never had a problem with it. You didn’t specify what the problem was besides that it not working so I don’t see how this post will help anyone.

Were you getting a blank screen? Were you getting shading artifacts? Was it a flat color? Are you just trying to warn people to never use gluLookAt?

What were you calling gluLookAt with? Which matrix mode were you in? Were you clearing the matrix with an identity call before calling gluLookAt or just concatenating them together over and over?

gluLookAt just computes a matrix for moving something infront of the “camera”, which is staring down the -z axis. That’s it. There’s no magic to it. It could be any number of issues in how you were calling it. If you put the eye and lookat position on top of each other, you’ll get the correct, but unusable matrix. If you give an up vector than points from your eye to your lookat position, you’ll get the correct, but unusable matrix. It’s just an equation for building a matrix. If something is going wrong you’re probably passing incorrect values or not setting up the call correctly (like correct matrix modes and clearing them before calling gluLookAt).

The problem was, the specular light wasn’t correctly calculated : as if the dot product between the two vectors was wrong.
I put the gluLookAt at the beginning of my application, with a call like :


gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);

//...Main Loop, drawing scene

If I well understand you, it could be a problem of wrong parameters given to gluLookAt ?

Hrmm, your OpenGL code seems fine. I’m not sure this is a OpenGL/C++ issue. Your camera is at (0,0,20), you’re looking at (0,0,0), and the up vector is (0,1,0). That all seems fine.

In GLSL let me make sure I’m understanding things correctly. “Position” is the vertex position in camera space since you’re multiplying it by the modelview matrix. “ToLightVector” is the vector from the vertex to the light source. This means your light source position should be in camera space as well. Is it or in C++ did you enter it in world space? The “ToEyeVector” is the vertex position in camera space pointing towards the camera (which is at the origin in camera space, right?) so that seems correct. Does the reflect function really need the negative there?

I guess the two things I’d look into is making sure you’re setting your camera in camera space and make sure that negative needs to be there in the reflect function.

How did you set light position? Before or after gluLookAt call? Light pos is also transformed by modelview matrix. Try to move setting light pos before gluLookAt or do this.

// assume current matrix mode is modelview
glLightf(GL_LIGHT0, GL_POSITION, light_posf);

If your rendering code is 100% shader based, you can stop using gl_LightSource[] and pass lights as uniforms. At least you will know whats going on.

Or move glLight after gluLookAt if it’s not and you want to do your calculations in camera space as your doing. You could pass it as a gl uniform as yooyo suggests but the important thing is to keep call your calculations in the same space.

Strattonbrazil : yes you’re understanding things correctly :slight_smile: Btw, my light is indeed in the camera space, no problem here. And I think the negative in reflect is needed, because I want the “VectorComingFromTheLight” to be reflected on the surface, so it would be -ToLightVector.

Strattonbrazil & Yooyo : I haven’t my code on this PC, but I will check later the order of my lights and glulookat calls. Thank you again.

I rewrote my line of glulookat, and :eek: :eek: well it works, I really do not know what was going on before :s
Anyway, I tried putting the glulookat line before or after the lightings calls and it still works. Subject close, I think.
Sorry for the troubles.