Game Development Reference
In-Depth Information
Figure 4.2: On the left is a
triangle colored red with flat
shading. On the right is a
triangle with vertex colors
red, green, and blue; notice
that with Gouraud shading,
the vertex colors are inter-
polated across the triangle.
Like many things in Direct3D, the shading mode is controlled through
the Direct3D state machine.
// set flat shading
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
// set Gouraud shading
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
4.4 Sample Application: Colored Triangle
The sample program for this chapter demonstrates a triangle colored
using flat shading and a triangle colored using Gouraud shading. It ren-
ders the image shown in Figure 4.2. First we add the following global
variables:
D3DXMATRIX World;
IDirect3DVertexBuffer9* Triangle = 0;
We include a D3DXMATRIX that is used to store the world transforma-
tion of the triangles that we are going to draw. The Triangle variable
is the vertex buffer that stores the vertex data of a triangle. Notice that
we only have to store the geometry of one triangle because we can
draw it multiple times at different positions in the world using the
world matrix.
The Setup method creates the vertex buffer and fills it with the
data of a triangle with colored vertices. The first vertex in the triangle
is full-intensity red (255), the second is full-intensity green (255), and
the third is full-intensity blue (255). Finally, we disable lighting for this
sample. Notice that this sample uses the new ColorVertex structure,
as explained in section 4.2.
Search WWH ::




Custom Search