Jump to content
  • Advertisement
Sign in to follow this  
soilentHagebuttentee

lighting: polygons vs spheres

This topic is 4978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!