Original Post
I wrote a function for my mesh class to compute all of the face normals, here is the code: And here are the functions of vector3df: But, when I render my object, all I get is a solid black model! (yes, GL_LIGHTING is enables, and I have a light at a good position, and I am rending the mesh with glNormal3f()) Any ideas why it is not computing them correctly?
void CMesh::computeNormals() {
vector3df vect1, vect2, vect3, nVect1, nVect2;
for(int i = 0; i < numGroups; i++) { //! Loop for every group (Rhymage!!!)
for(int j = 0; j < groups.numTriangles; j++) {
vect1 = groups.vertices[groups.triangles[j].indices[0]].pos;
vect2 = groups.vertices[groups.triangles[j].indices[1]].pos;
vect3 = groups.vertices[groups.triangles[j].indices[2]].pos;
nVect1 = vect1 - vect3;
nVect2 = vect3 - vect2;
groups.triangles[j].normal = nVect1.crossProduct(nVect2);
groups.triangles[j].normal.normalize();
}
}
}
float getLength() const { return sqrt(x*x + y*y + z*z); }
//! Returns the cross product of 2 vectors
vector3d<type> crossProduct(const vector3d<type>& p) const {
return vector3d<type>(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
}
//! Normalizes the vector
vector3d<type>& normalize() {
type inv = (type)1.0 / (type)getLength();
x *= inv;
y *= inv;
z *= inv;
return *this;
}