Calculating normal vectors
We're given an array of vertex positions and an array of triangles.
We want to apply the lighting equation to shade that object. Lighting requires that each vertex have a normal attribute, so we must calculate that normal.
Assume we have an array of vertex positions called positions and an array of triangles called triangles. We will generate a new array of normals, called normals, like so:
- Initialize a zero-valued normal for each vertex position.
- Find the normal for each triangle and add it to each adjacent vertex.
- Renormalize to ensure that all new normal vectors have unit length.