View Full Version : Lighting problem -> math Problem. Problem with rotations/translations. Plz help!

09-26-2004, 10:21 AM
Hello Guys!
I Have a problem with lighting when i use Matrix Operations In OpenGL.

I have a Shader in ARB_fragment_program (ASM-like) to render the World. THis is the Shader code:

I am 100% sure that the shader is correct.
And also have a moving light that i control it from the keyboard.
I render the world and everything is perfect. I move the light and works correct.
Now the if i try to rotate the world but have the light in the same place then the lighting is not correct the lighting

"follows" the rotation but not the ligh coords. This has as the result to have exactly the same lighting as before no mater

how much rotations/translations or what translations/rotations i Do.
Well.. this is correct if you think this:

Lets Say i have a light at position(0,0,0) and i have a triangle that face the light. Lets say that the one vertex of the

triangle is (5,5,5).
If i render the scene the triangle will be lighten (forget the attenuation).
Now if I translate the triangle with glTranslatef(-1,-1,-1) the triangle should be lighten again but the lighting will not

exactly the same. it maybe more or less bright.
This is my problem. no matter what ranslations/rotations i do i get the same result!
This happen because the shader BEFORE the translation takes for input (0,0,0) for the light coords and (5,5,5) and 2 more

vertex coord for the vertex coords.
AFTER the translation the shader still gets the SAME values (because glTranslatef does not change the actual vertex value

e.g.: triangle.vertices[0])

I hope you understand my problem. Any idea what to do to fix this? Maybe some math magin? a matrix multiplication or


this is the code for rendering a polygon:

Here is a demo with the problem:

You will see the problem if you move the light a little and start rotate the level(if you not mmove the light and only rotate

the level you will not see the problem because the light is at (0,20,0) position and the level rotate around thes point at

the y axis)

The Keys for this game is:

W, A, S, D and mouse For moving the camera.
R For Reset the level in the origin position
O, P for rotating the world
NumPad 4, 5, 6, 8 for moving the light

The demo requires gfx card Supporting ARB_fragment_program and OGL Shading Language (not used here)

09-26-2004, 07:41 PM
Without sitting down and learning both STL and FP's I would guess that you problem is "simply" that you are not tranforming the light's position correctly for the fragment program.

When you do bump mapping without a fragment program, you have to calculate the half angle (I hope I've named it correctly) for each vertice for each frame (or rather when the light moves relative to the object). I don't know if you'd do this outside the FP or inside it but I can't see any evidence of you doing this.

The way I do this is I get the position of the light and multiply it by the inverse of the Modelview matrix for the object (although this will probably need some refinement because I haven't considered what effect a moving "camera" would have on this calculation).

Hope that helps.

09-26-2004, 09:14 PM
ok... Now At the place I Rotate the level I multiply the light position with with the tranpose modelViewMatrix of the level and it works!!! But If I move the camera then... all hell... Any idea how to fix this?
Here is my code:

09-27-2004, 04:18 AM
Does your "transpose Modelview matrix" take into account the position of the camera? You probably need to also multiply the position of the light by the inverse of the camera position.

09-27-2004, 05:27 AM
camera matrix in not only rotation. right? Should I find the inverse with an other way? or using matrix transpose?