Vince Molnár
Vince Molnár

Reputation: 1062

OpenGL lighting appears on the opposite side

I have a very disturbing problem with OpenGL.

My homework is to create scene with a human and a sword and some other objects, put a point light is each "palm" of the guy (green in the right, red in the left), and have a directional light source as the sun.

Here's a zip with two versions of my program: http://www.woofiles.com/dl-245221-OdPUtOaW-Release.zip

(Unfortunately the names are hungarian, "szép_de_rossz_oldalon_a_fény" means "nice but lights are on the opposite side", and "fény_jó_oldalon_de_ronda" means "lights on the right side but ugly".)

My problem is that the effect of the lights appears on the opposite side of my tesselated parametric surfaces.

A logical explanation would be that the surface normals are incorrect, but multiplying them with -1 give an even stranger result. Normal vector interpolation seems to get broken, and what remains is a rough surface with bad lighting. All that by reversing the normals. I'm sure that the triangles are facing the right direction, that the vector arithmetic is flawless, and that everything is placd in the scene where it should be (one can see that the sword is green, but that side of the other surfaces is red).

Upvotes: 1

Views: 3580

Answers (2)

Drew Hall
Drew Hall

Reputation: 29055

Sounds like bad normals to me--remember there are a lot of ways to screw up normal vectors other than sign errors:

  • Your normals might not have unit length (you can verify by trying glEnable(GL_NORMALIZE) and seeing if it makes a difference).
  • They might not truly be normal to the surface (check by comparing cross products of the triangle edges to your normal vectors)
  • You might have inconsistent wrapping (CW/CCW) of your triangle vertices, effectively flipping the sign of the normal (unless culling is enabled--then you might not see some triangles at all)

Good luck!

Upvotes: 3

teenan
teenan

Reputation: 159

Unfortunately the zip you linked to didn't have the code...

A one shot thing to try that I've had to use before when I got my polygon facings mixed up is to use GL_LIGHT_MODEL_TWO_SIDE as the glLightModel which causes both sides of polygons to be lit rather than just the front face. from memory the useage is

glLightModel(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)

(or something lile that) make the call before any lighting takes place.

this is a hacky solution but I can't see the code and I guess you're on a deadline.. something to try at least until someone comes along with a better solution :)

Upvotes: 2

Related Questions