Game Development Reference
In-Depth Information
Vec3 edge1 = triangle[1]-triangle[0];
Vec3 edge2 = triangle[2]-triangle[0];
Vec3 normal = edge1.Cross(edge2);
normal.Normalize();
Our polygon is defined with three positions in 3D space. These positions are used to
construct two edge vectors, both pointing away from the same vertex. The two edges
are sent into the cross product function, which returns a vector that is pointing in the
right direction but is the wrong size. All normal vectors must be exactly one unit in
length to be useful in other calculations, such as the dot product. The Vec3::Nor-
malize() function calculates the unit vector by dividing the temp vector by its
length. The result is a normal vector you can apply to a vertex.
If you take a closer look at the teapot figure, you
ll notice that the normal vectors are
really the normals of multiple triangles, not just a single triangle. You calculate this
by averaging the normals of each triangle that shares your vertex. Calculate the aver-
age of multiple vectors by adding them together and dividing by the number of vec-
tors, exactly as you would calculate the average of any other
number.
'
Calculate Your Normals Ahead of Time
Calculating a normal is a somewhat expensive operation. Each triangle will
require two subtractions, a cross product, a square root, and three divisions. If
you create 3D meshes at runtime, try to calculate your normals once, store them
in object space, and use transforms to reorient them.
a color value that is universally applied to every vertex in the scene. This has the effect
of making an object glow like a light bulb, and it isn
'
You might be wondering why I didn
t mention ambient lighting
'
t very realistic. Ambient lighting
values are a necessary evil in today
s 3D games because they simulate low-light levels
on the back or underside of objects due to light reflecting all about the scene. In the
next few years, I expect this light hack to be discarded completely in favor of more
advanced techniques with pixel shaders using environment-based lighting effects.
'
Materials
When light hits something, the light color is added to the color of the object. This
color is typically defined by a material
basically a fancy way of describing how an
object reflects light. DirectX 9 defined this with a structure still useful in Direct3D 11:
D3DMATERIAL9 .
 
 
Search WWH ::




Custom Search