Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WoolyUK

Should this work?

This topic is 5681 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

Hiya Im trying to calculate a normal of a triangle with random vertices. I found this routine in some code on the opengl game programming CD: // CalculateNormal() // desc: given 3 points, calculates the normal to the points void CalculateNormal( float *p1, float *p2, float *p3 ) { float a[3], b[3], result[3]; float length; a[0] = p1[0] - p2[0]; a[1] = p1[1] - p2[1]; a[2] = p1[2] - p2[2]; b[0] = p1[0] - p3[0]; b[1] = p1[1] - p3[1]; b[2] = p1[2] - p3[2]; result[0] = a[1] * b[2] - b[1] * a[2]; result[1] = b[0] * a[2] - a[0] * b[2]; result[2] = a[0] * b[1] - b[0] * a[1]; // calculate the length of the normal length = (float)sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]); // normalize and specify the normal glNormal3f(result[0]/length, result[1]/length, result[2]/length); } Is that all that's needed? Does it seem right to you guys? Because my normals seem messed up when I apply lighting... Also, when drawing an object such as a cube, do I draw the backface in the opposite winding direction to the front face? or do I draw them both the same direction? Im trying to find my lighting problem Thanks! Adam [edited by - WoolyUK on December 4, 2002 7:57:11 AM]

Share this post


Link to post
Share on other sites
Advertisement
Yes, that is the same kind of routine I use. You are calculating the cross product of two vectors. These vectors make up two of the sides of the triangle, and a cross product of two vectors will result in a vector normal to both of the original two, thus, your normal. You might be running into problems if the order of your points are incorrect. I THINK it should be like this for a normal out of the screen:

p1------------>p3
|
|
|
|
|
V
p2

Right hand rule, right?

Share this post


Link to post
Share on other sites
It looks right. If it doesn''t work, try passing you points in the opposite order. Since you''re using an array, it might be easier to alter the code like this:


  
a[0] = p1[0] - p3[0];
a[1] = p1[1] - p3[1];
a[2] = p1[2] - p3[2];

b[0] = p1[0] - p2[0];
b[1] = p1[1] - p2[1];
b[2] = p1[2] - p2[2];


If you add ''glEnable(GL_NORMALIZE);'' to your code somewhere you can skip the last part where you calculate the length and divide by it, and your normals will survive being stretched or scaled if you plan on doing that to your geometry.

Share this post


Link to post
Share on other sites
I''ve fixed it now thanks! The code was right, it was me making a mistake elsewhere in my code! Doh!

Thanks anyway!

Adam

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!