Jump to content
  • Advertisement
Sign in to follow this  
Ross_K

Calculating Normals

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

I have had a lot of trouble calculating normals in C++. I have written a header file containing a function that calculates normals depending on the arguments, and it works on some faces, but not others. Any help would be greatly appreciated. this is the source: normals.h #include <math.h> float normal[3], v[3], v1[3], v2[3], v3[3], length; float calcNormal( float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3 ){ //Calculating vectors. v1[0] = x2 - x1; v1[1] = y2 - y1; v1[2] = z2 - z1; v2[0] = x3 - x1; v2[1] = y3 - y1; v2[2] = z3 - z1; //Calculates normal vectors co-ordinates. v[0] = v1[1] * v2[2] - v1[2] * v2[1]; v[1] = v1[2] * v2[0] - v1[0] * v2[2]; v[2] = v1[0] * v2[1] - v1[1] * v2[0]; //Calculates length of normal vector length = sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] ); //normalizes the normal vector normal[0] = v[0] / length; normal[1] = v[1] / length; normal[2] = v[2] / length; return 0; } Thanks, Ross.

Share this post


Link to post
Share on other sites
Advertisement
In order for this to work all of your faces must be "wound" in the same direction: clockwise or counter-clockwise. You can tell the direction that a face winds by looking at it dead on and seeing which direction the verts are defined in.

1
|\
2-3 -- Counter-clockwise

1
|\
3-2 -- Clockwise

If all of your faces are wound the same direction, there may be other issues, but check that first.

Share this post


Link to post
Share on other sites
Thanks. that worked perfectly the first time. You have saved me a lot of time, so thanks again :)

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!