+// warning: this is an expensive function!
+void Mod_BuildNormals(int numverts, int numtriangles, const float *vertex3f, const int *elements, float *normal3f)
+{
+ int i, tnum;
+ float normal[3], *v;
+ const int *e;
+ // clear the vectors
+ memset(normal3f, 0, numverts * sizeof(float[3]));
+ // process each vertex of each triangle and accumulate the results
+ for (tnum = 0, e = elements;tnum < numtriangles;tnum++, e += 3)
+ {
+ TriangleNormal(vertex3f + e[0] * 3, vertex3f + e[1] * 3, vertex3f + e[2] * 3, normal);
+ VectorNormalize(normal);
+ v = normal3f + e[0] * 3;
+ v[0] += normal[0];
+ v[1] += normal[1];
+ v[2] += normal[2];
+ v = normal3f + e[1] * 3;
+ v[0] += normal[0];
+ v[1] += normal[1];
+ v[2] += normal[2];
+ v = normal3f + e[2] * 3;
+ v[0] += normal[0];
+ v[1] += normal[1];
+ v[2] += normal[2];
+ }
+ // now we could divide the vectors by the number of averaged values on
+ // each vertex... but instead normalize them
+ for (i = 0, v = normal3f;i < numverts;i++, v += 3)
+ VectorNormalize(v);
+}
+