_PROGRAMMING WITH OPENGL PRIMITIVES_ by Ron Fosner Listing One // Pass in three points, and a vector to be filled void NormalVector( GLdouble p1[3], GLdouble p2[3],GLdouble p3[3], GLdouble n[3] ) { GLdouble v1[3], v2[3], d; // calculate two vectors, using the middle point // as the common origin v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2]; v2[0] = p2[0] - p0[0]; v2[1] = p2[1] - p0[1]; v2[2] = p2[2] - p0[2]; // calculate the crossproduct of the two vectors n[0] = v1[1]*v2[2] - v2[1]*v1[2]; n[1] = v1[2]*v2[0] - v2[2]*v1[0]; n[2] = v1[0]*v2[1] - v2[0]*v1[1]; // normalize the vector d = ( n[0]*n[0] + n[1]*n[1] + n[2]*n[2] ); // try to catch very small vectors if ( d < (Gldouble)0.00000001) { // error, near zero length vector // do our best to recover d = (GLdouble)100000000.0; } else // take the square root { // multiplication is faster than division // so use reciprocal of the length d = (GLdouble)1.0/ sqrt( d ); } n[0] *= d; n[1] *= d; n[2] *= d; }