Calculating Normals
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement