Are your triangles defined with indices to an array of vertices or is each triangle a set of three explicit points ???
In the first case, you can speed up the smoothing algorithm a bit…
This assumes that you will need only ONE normal per vertex. I say that because you sometimes want more than one normal per vertex (i.e. if there is a break in the geometry, you do not always want to smooth-shade).
Now, to the optimization:
Calculate all the faces normals (which you already do as far as I understand !). OPTIONAL: You might as well calculate the area of each face if you want to take into account the triangle surface when averaging the normals.
Create an array of normals of size No vertices i.e.:
C_Normal *Normals=new C_Normal[iNoVertices];
where C_Normal is a vector…
Initialize all the normals to (0,0,0)
Loop through all triangles and do:
IF YOU USE THE TRIANGLES AREA TO AVERAGE THE NORMALS:
IF YOU DO NOT USE THE TRIANGLES AREA TO AVERAGE THE NORMALS:
Where pTriangle is a pointer to the current triangle and V1, V2, V3 are the indices of the vertices that make the triangle.
And Normal is the triangle normal you calculated in 1), Area is the triangle area you calculated in 1).
- Loop through the normals array and normalize each normal.
You now have your normals for each vertex…
This is much faster than looping through each vertex for each vertex (i.e. sqr(No Vertices)) but, as I said, it assumes one normal per vertex only…
You can also modify this algorithm to have more than one normal per vertex but it could lead to a deep structure change…
In the second case, I have an (optimized, I think !) algorithm to bring you back to the first case… As it is a bit long to explain, I will wait until I know whether it would be useful to you or not !
Hope this helps !
[This message has been edited by Eric (edited 01-25-2001).]