# Calculating Normals

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

## 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 on other sites
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 on other sites
Thanks. that worked perfectly the first time. You have saved me a lot of time, so thanks again :)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×