Sign in to follow this  
soilentHagebuttentee

lighting: polygons vs spheres

Recommended Posts

Hi, a beginner's problem: my spheres are illuminated well, but my polygons are always dark on one side - no matter if I draw them clockwise or counterclockwise. I tried to place lights all around, but that didn't work. Maybe I have to define normal vectors for the polygons? If yes: how?

Share this post


Link to post
Share on other sites
The problem is that standard OpenGL lighting is per-vertex. So unless you have a highly-tessellated model (such as a sphere), you won't get accurate lighting results. I'm assuming that the polygons you're drawing are single triangles or quads, of course.

If you're looking for a way to define polygon normals, just take three points on the polygon and take the cross product of them to get a face normal, normalize it, and pass it to glNormal3f().


// Assuming vec3_t is a 3D vector object with overloaded operators.
void GetFaceNormal(const vec3_t& p0, const vec3_t& p1, const vec3_t& p2)
{
vec3_t v0 = p1-p0;
vec3_t v1 = p2-p0;
vec3_t cross = CrossProduct(v0, v1);
cross.Normalize();
glNormal3f(cross.x, cross.y, cross.z);
}


The function CrossProduct() takes two vector objects as arguments and returns the cross product of them (another vector).

-david-

Share this post


Link to post
Share on other sites
If I'm understanding you correctly then the problem is nothing to do with per-vertex lighting (although that may be another problem you have) but the fact that OpenGL lighting by default is one-sided. If you want to draw a two sided polygon, either draw it twice with back face culling enabled, one face pointing each way, or call glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);.

Enigma

Share this post


Link to post
Share on other sites
I think the problem is made pretty clear from the outset.

Quote:
Original post by soilentHagebuttentee
Maybe I have to define normal vectors for the polygons? If yes: how?


Yes, you do have to define normal vectors.

You do this with the glNormal*() functions, for example glNormal3f(0.0f, 0.0f, 1.0f);

Hope this helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this