#### Archived

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

# Should this work?

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

## 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 on other sites
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 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 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!

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 12
• 16
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632657
• Total Posts
3007680
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!