Game Development Reference
In-Depth Information
const D3DXCOLOR
YELLOW( D3DCOLOR_XRGB(255, 255,
0) );
const D3DXCOLOR
CYAN( D3DCOLOR_XRGB( 0, 255, 255) );
const D3DXCOLOR
MAGENTA( D3DCOLOR_XRGB(255,
0, 255) );
}
4.2 Vertex Colors
The color of a primitive is determined by the color of the vertices that
make it up. Therefore, we must add a color member to our vertex data
structure. Note that a D3DCOLORVALUE type cannot be used here
because Direct3D expects a 32-bit value to describe the color of a ver-
tex. (Acually, by using a vertex shader we could use 4D color vectors
for the vertex color, and thereby gain 128-bit color, but that is getting
ahead of ourselves for now. Vertex shaders are covered in Chapter 17.)
struct ColorVertex
{
float _x, _y, _z;
D3DCOLOR _color;
static const DWORD FVF;
}
const DWORD ColorVertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
4.3 Shading
Shading occurs during rasterization and specifies how the vertex colors
are used to compute the pixel colors that make up the primitive. There
are two shading modes that are presently used: flat shading and
Gouraud shading.
With flat shading, the pixels of a primitive are uniformly colored by
the color specified in the first vertex of the primitive. So the triangle
formed by the following three vertices would be red, since the first ver-
tex color is red. The colors of the second and third vertices are ignored
with flat shading.
ColorVertex t[3];
t[0]._color = D3DCOLOR_XRGB(255, 0, 0);
t[1]._color = D3DCOLOR_XRGB(0, 255, 0);
t[2]._color = D3DCOLOR_XRGB(0, 0, 255);
Flat shading tends to make objects appear blocky because there is no
smooth transition from one color to the next. A much better form of
shading is called Gouraud shading (also called smooth shading). With
Gouraud shading, the colors at each vertex are interpolated linearly
across the face of the primitive. Figure 4.2 shows a red flat shaded tri-
angle and a triangle colored using Gouraud shading.
Team-Fly ®
Search WWH ::




Custom Search