Alexander Rafferty
Alexander Rafferty

Reputation: 6233

OpenGL lighting question

I have got all my OpenGL lighting working properly, with the normals, materials, ect...

My problem is the way in which OpenGL renders the faces when the light is behind the face. If the light is behind a face, it should render a black face, but instead it renders what the backface of the face would look like. How do I stop this strange behaviour. I find it hard to describe what is happening, but hopefully you know how to solve this.

Thanks in advance.

EDIT: Okay, say I have a flat square polygon to reperesnt a wall. Whichever side I have the light on, both get lit up even though only one side should be lit and the other should be black (or very dark because of other/ambient light). See the problem? It is like paper because the light is just going through and showing up on the wrong side of the face.

Upvotes: 1

Views: 1632

Answers (1)

CiscoIPPhone
CiscoIPPhone

Reputation: 9477

The problem you are seeing occurs because both sides of the polygon use the same normals in their lighting calculations.

The solution to this is to set:

float modelTwoside[] = {GL_TRUE};
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, modelTwoside);

This causes OpenGL to reverse the normals before rendering the back side. OpenGL will also use the back material.

Upvotes: 2

Related Questions