View Full Version : Incorrect Per Pixel Lighting

12-13-2004, 01:14 PM
I've been trying to implement PPL (in eye space) using OpenGL and Cg, but I can't get it work correctly. When I change my camera position (I use gluLookAt), lighting gets wrong - it looks like I would be moving/rotating the object, not the camera.
Download this (http://users.volja.net/dusanwing/PPL_hm.zip) to see what I mean. Use N/M, U/I and J/K to move the camera.

- Vertex Program (http://rafb.net/paste/results/7iZSvF16.html)
- Fragment Program (http://rafb.net/paste/results/Wsg3nx89.html)
- Main Program (http://rafb.net/paste/results/Dx3zk977.html)

Obviously, my light and eye (camera) position aren't in eye space, but in world space - I tried transforming them, but it didn't change anything.

I've been trying to solve this for over a month now, so I would highly appreciate if someone could help me.

Jens Scheddin
12-14-2004, 12:44 AM
Just a guess after a quick look: Don't you need to transform the light vector L into tangent space?


12-14-2004, 02:37 AM
How can I transform it?

Jens Scheddin
12-14-2004, 05:24 AM
Originally posted by Wingman:
How can I transform it?Do it in your vertex program. You need the tangent space vectors at every vertex (tangent, binormal, normal) and the light position in world space. Calculate the light vector from the vertex position and the light position and transform this vector with three DP3 instructions and the tangent space vectors.
This is basic bump mapping, so you should find a lot of sample source code floating around the net (i.e. nVidia developer site (http://www.nvidia.com/developer) ).

12-14-2004, 11:27 AM
Ok, thanks.
So if I get this right, I should actually do some sort of bump mapping to get correct lighting? Well, I guess I should just read on (Cg Tutorial book), becasue I don't know bump mapping yet. I hope it will help, thanks again.

12-14-2004, 12:10 PM
To do per-pixel lighting, you need to bring the normal and the light vector into the fragment program. Both of these need to be transformed into the same space, whether it is tangent space, world space, model space, or whatever. More than likely, you aren't transforming one of them into the same space as the other.

12-16-2004, 04:40 AM
If you look at the vertex program, you can see that I do transform object space vertex position and normal into eye space. You're saying I should transform normal and light vector only, that's all? And how can I transform world space to eye space within a shader, then?
But like I said, I already tried to transform everything into the same space, but it didn't work - lighting looked the same. So I assume problem's somewhere else as well, but don't have any idea where.


12-17-2004, 07:22 AM
Any ideas?

12-17-2004, 12:46 PM
monkey tennis?

12-18-2004, 08:57 AM
It just sounds like you need to make sure that you know which coordinate space you specify your different vectors in, and how the vector specification works (for example, the coordinates for glLight are transformed by the MODELVIEW at the time of specification (only)). Then just work out what lighting equation you actually want, and implement it appropriately. There's no silver bullet; just keeping things straight.