Game Development Reference
In-Depth Information
and p 2 , and we need to compute the vertex normal for each of the verti-
ces n 0 , n 1 , and n 2 .
The simplest approach, and the approach we illustrate, is to find the
face normal of the triangle that the three vertices form and use the face
normal as the vertex normals. First compute two vectors that lie on the
triangle:
p
p
u
1
0
p
p
v
2
0
Then the face normal is:
n
u
v
Since each vertex normal is the same as the face normal:
n
n
n
n
0
1
2
Below is a C function that computes the face normal of a triangle from
three vertex points on the triangle. Note that this function assumes
that the vertices are specified in a clockwise winding order. If they are
not, the normal will point in the opposite direction.
void ComputeNormal(D3DXVECTOR3* p0,
D3DXVECTOR3* p1,
D3DXVECTOR3* p2,
D3DXVECTOR3* out)
{
D3DXVECTOR3 u = *p1 - *p0;
D3DXVECTOR3 v = *p2 - *p0;
D3DXVec3Cross(out, &u, &v);
D3DXVec3Normalize(out, out);
}
Using face normals as vertex normals does not produce smooth results
when approximating curved surfaces with triangles. A better method
for finding a vertex normal is normal averaging . To find the vertex nor-
mal v n of a vertex v , we find the face normals for all the triangles in the
mesh that share vertex v . Then v n is given by averaging all of these
face normals. Here's an example to illustrate. Suppose three triangles,
whose face normals are given by n 0 , n 1 , and n 2 , share the vertex v .
Then v n is given by averaging the face normals:
1
v
n
n
n
n
0
1
2
3
During the transformation stages, it is possible for vertex normals to
become non-normal. Therefore, it is best to be safe and have Direct3D
Search WWH ::




Custom Search