void CalculateNormals() {
unsigned int numPolygons = (unsigned int)ceil(double(numIndex / 3.0));
Vector3d *FaceNormal = new Vector3d[numPolygons + 1];
unsigned int Counter = 0;
for (unsigned int i = 0; i < numIndex; i += 3) { // Calculate the face normals
FaceNormal[Counter++] = Cross(Vertex[Index[i]] - Vertex[Index[i + 1]],
Vertex[Index[i]] - Vertex[Index[i + 2]]);
}
Normal = new Vector3d[numVertex];
for (i = 0; i < numVertex; i++) { // Loop through every vertex
for (unsigned int j = 0; j < numIndex; j += 3) { // Loop through every face
if (Index[j] == i || Index[j + 1] == i || Index[j + 2] == i) {
Normal[i] += FaceNormal[j / 3]; // If the face indexes the vertex, add the face's normal to the vertex normal
}
}
Normal[i].Normalize();
}
delete[] FaceNormal;
}
This loop is the bottleneck:
for (i = 0; i < numVertex; i++) { // Loop through every vertex
for (unsigned int j = 0; j < numIndex; j += 3) { // Loop through every face
if (Index[j] == i || Index[j + 1] == i || Index[j + 2] == i) {
Normal[i] += FaceNormal[j / 3]; // If the face indexes the vertex, add the face's normal to the vertex normal
}
}
Normal[i].Normalize();
}
I'm hoping to get some tips or ideas on how to optimize it.
Thanks!
[Edit: Fixed source tags]
[edited by - James Trotter on June 6, 2004 3:29:13 PM]