## 10/15/10

### Simple Code to Calculate normals for a Quad Mesh

I'm ripping iq here. However, I've fiound a lot of people haven't read his brilliant normals idea buried deep in one of his presentations, plus I add face normals and a subtle way to rewrite crossproduct that shortens code again.

Here is some tiny  pseudo-code to calculate both the vertex and face normals of a  mesh made of quads. It is pseudo-code because your mesh structure will not be the same as mine.  The idea is based on iqs idea in his Breakpoint presentation.
for ( i=0; i<numberofquads; i++) {
for (uint k=0; k<4; k++)  cross ( &quad[i].v[k], &quad[i].v[(k+1)&3], &facenormals[i] );
for (uint k=0; k<4; k++)  add ( &facenormals[i], &vertexnormals[&quad[i].v[k]]);
}
Now this neat and easy to read and incredibly small for such power. Essentially it is:
for each edge in quad
accumulate crossproduct of edge vertices into face normal
for each vertex in quad

One subtle trick here in the code is that cross product is redefined thus:
cross(v1,v2,v3) ::  v3 = v3 + crossproduct (v1,v2)
Add is as you expect::
1. 