// Points p1, p2, & p3 specified in counter clock-wise order void calcNormal(float v[3][3], float out[3]) { float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; // Calculate two vectors from the three points v1[x] = v[0][x] - v[1][x]; v1[y] = v[0][y] - v[1][y]; v1[z] = v[0][z] - v[1][z]; v2[x] = v[1][x] - v[2][x]; v2[y] = v[1][y] - v[2][y]; v2[z] = v[1][z] - v[2][z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Normalize the vector (shorten length to one) ReduceToUnit(out); }
void TriObject::applyNormals() { if (numfaces == 0) return; delete[]nx; delete[]ny; delete[]nz; nx = new float[numfaces / 3]; ny = new float[numfaces / 3]; nz = new float[numfaces / 3]; if (nx == NULL || ny == NULL || nz == NULL) { delete[]nx; delete[]ny; delete[]nz; normalapplied = FALSE; return; } float normal[3]; for (int i = 0; i < numfaces / 3; i++) { CalcNormal(3 * i, normal); ReduceToUnit(normal); nx[i] = normal[0]; ny[i] = normal[1]; nz[i] = normal[2]; } normalapplied = TRUE; }
void calcNormal(float v[3][3], float out[3]) /* Calculates Normal For A Quad Using 3 Points */ { float v1[3],v2[3]; /* Vector 1 (x,y,z) & Vector 2 (x,y,z) */ static const int x = 0; /* Define X Coord */ static const int y = 1; /* Define Y Coord */ static const int z = 2; /* Define Z Coord */ /* Finds The Vector Between 2 Points By Subtracting * The x,y,z Coordinates From One Point To Another. * Calculate The Vector From Point 1 To Point 0 */ v1[x] = v[0][x] - v[1][x]; /* Vector 1.x=Vertex[0].x-Vertex[1].x */ v1[y] = v[0][y] - v[1][y]; /* Vector 1.y=Vertex[0].y-Vertex[1].y */ v1[z] = v[0][z] - v[1][z]; /* Vector 1.z=Vertex[0].y-Vertex[1].z */ /* Calculate The Vector From Point 2 To Point 1 */ v2[x] = v[1][x] - v[2][x]; /* Vector 2.x=Vertex[0].x-Vertex[1].x */ v2[y] = v[1][y] - v[2][y]; /* Vector 2.y=Vertex[0].y-Vertex[1].y */ v2[z] = v[1][z] - v[2][z]; /* Vector 2.z=Vertex[0].z-Vertex[1].z */ /* Compute The Cross Product To Give Us A Surface Normal */ out[x] = v1[y]*v2[z] - v1[z]*v2[y]; /* Cross Product For Y - Z */ out[y] = v1[z]*v2[x] - v1[x]*v2[z]; /* Cross Product For X - Z */ out[z] = v1[x]*v2[y] - v1[y]*v2[x]; /* Cross Product For X - Y */ ReduceToUnit(out); /* Normalize The Vectors */ }
void calcNormal(float v[3][3], float out[3]) // Calculates Normal For A Quad Using 3 Points { float v1[3],v2[3]; // Vector 1 (x,y,z) & Vector 2 (x,y,z) static const int x = 0; // Define X Coord static const int y = 1; // Define Y Coord static const int z = 2; // Define Z Coord // Finds The Vector Between 2 Points By Subtracting // The x,y,z Coordinates From One Point To Another. // Calculate The Vector From Point 1 To Point 0 v1[x] = v[0][x] - v[1][x]; // Vector 1.x=Vertex[0].x-Vertex[1].x v1[y] = v[0][y] - v[1][y]; // Vector 1.y=Vertex[0].y-Vertex[1].y v1[z] = v[0][z] - v[1][z]; // Vector 1.z=Vertex[0].y-Vertex[1].z // Calculate The Vector From Point 2 To Point 1 v2[x] = v[1][x] - v[2][x]; // Vector 2.x=Vertex[0].x-Vertex[1].x v2[y] = v[1][y] - v[2][y]; // Vector 2.y=Vertex[0].y-Vertex[1].y v2[z] = v[1][z] - v[2][z]; // Vector 2.z=Vertex[0].z-Vertex[1].z // Compute The Cross Product To Give Us A Surface Normal out[x] = v1[y]*v2[z] - v1[z]*v2[y]; // Cross Product For Y - Z out[y] = v1[z]*v2[x] - v1[x]*v2[z]; // Cross Product For X - Z out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Cross Product For X - Y ReduceToUnit(out); // Normalize The Vectors }
void calcNormal(float v[3][3], float out[3]) { float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; v1[x] = v[0][x] - v[1][x]; v1[y] = v[0][y] - v[1][y]; v1[z] = v[0][z] - v[1][z]; v2[x] = v[1][x] - v[2][x]; v2[y] = v[1][y] - v[2][y]; v2[z] = v[1][z] - v[2][z]; out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; ReduceToUnit(out); }
//From OpenGL superbible void CalcNormal(float const *v1,float const *v2,float const *v3,float * out) { float vt1[3],vt2[3]; static const int x=0; static const int y=1; static const int z=2; //calculate 2 vectors from 3 points vt1[x]=v1[x]-v2[x]; vt1[y]=v1[y]-v2[y]; vt1[z]=v1[z]-v2[z]; vt2[x]=v2[x]-v3[x]; vt2[y]=v2[y]-v3[y]; vt2[z]=v2[z]-v3[z]; //Take the cross product out[x]=vt1[y]*vt2[z]-vt1[z]*vt2[y]; out[y]=vt1[z]*vt2[x]-vt1[x]*vt2[z]; out[z]=vt1[x]*vt2[y]-vt1[y]*vt2[x]; ReduceToUnit(out); }