Pandemonium
Lijsterbeslaan 3
B-2940 Stabroek, Belgium
IBAN: BE78 9794 3692 5286
BIC: ARSPBE22XXX
KBO: 0314.595.348
VAT: BE 0 881.023.185
request blrootsales@pandemonium.be
ID:  Pass:    [ Not logged in. ]

 Weird lighting problem 
 Dirk Myers
Joined 2007-07-22 21:00:26.
(3838 Days old)

Weird lighting problem

Posted on: 2011-05-01 11:24:52 
IP: Logged 
guys I'm having a really weird lighting problem I've been struggling with for a few days now :/

The problem is as follows: when opengl is rendering my scene, my normals spin with the objects. To test this I'm just outputting gl_Normal to gl_FragColor which gives me the classic red green and blue polygons. So for example when I draw a cube which has normals pointing towards the camera, and I spin that cube 180, those normals will be pointing away from the camera.

This of course screws up lighting because as the models move or rotate, so do the shadows on them, which isn't right.

I've tried multiplying the normals by the modelview matrix but then the faces which face towards the camera are always unshaded and the ones aligned with the view direction are fully shaded. Moving the light around doesn't solve this because moving the light to the back of the model fully shades all faces, and moving the light in front of the model fully shades all faces.

It's a bit of a bizarre problem description but does anyone have thoughts on what the source might be?

  Showing entry "Weird lighting problem".
Pandemonium 2009

 2 solutions 
 Dave Driesen
Joined 2007-05-18 20:59:14.
(3903 Days old)

2 solutions

Posted on: 2011-05-02 04:17:22 
IP: Logged 
Dirk Myers wrote:
The problem is as follows: when opengl is rendering my scene, my normals spin with the objects. To test this I'm just outputting gl_Normal to gl_FragColor which gives me the classic red green and blue polygons. So for example when I draw a cube which has normals pointing towards the camera, and I spin that cube 180, those normals will be pointing away from the camera.
You were on the right track. As you noticed by rotating your models and outputting to gl_FragColor, opengl feeds you your vertex normals in object space. The position of your light source though, given to you through gl_LightSource[x].position, is in camera space. So if you want to do lighting (be it per pixel or per vertex) then you need to either bring your light position into object space or bring your vertices into camera space. Cost-wise the latter is probably the better choice but your best modus operandi depends on what else you are doing in your shader. You can calculate your normal in camera space by multiplying it by gl_NormalMatrix. After that, you calculate the angle between the normal and the light direction (through simple dot product) and shade your vertices/fragments accordingly.

Moving the light around doesn't solve this because moving the light to the back of the model fully shades all faces, and moving the light in front of the model fully shades all faces.

That last sentence suggests to me that you are setting your light position (glLightfv) before applying modelview transformations. Try setting the light just before your draw calls, after you've positioned your camera, and see what that does.

  Showing entry "2 solutions".
Pandemonium 2009


These pages are the intellectual property of the Pandemonium crew.
Welcome to Pandemonium.be - Antwerp based development studio providing development and administration services. Developers and maintainers of the Pyromasaur 3D framework.
Secure
"