There are two shading modes that are presently used: flat shading and Gouraud shading.
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);
bool Setup() { // create vertex buffer Device->CreateVertexBuffer( 3 * sizeof(ColorVertex), D3DUSAGE WRITEONLY, ColorVertex::FVF, D3DPOOL MANAGED, &Triangle, 0); // fill the buffers with the triangle data ColorVertex* v; Triangle->Lock(0, 0, (void**)&v, 0); v[0] = ColorVertex(-1.0f, 0.0f, 2.0f, D3DCOLOR XRGB(255, 0, 0)); v[1] = ColorVertex( 0.0f, 1.0f, 2.0f, D3DCOLOR XRGB( 0, 255, 0)); v[2] = ColorVertex( 1.0f, 0.0f, 2.0f, D3DCOLOR XRGB( 0, 0, 255)); Triangle->Unlock(); // set projection matrix D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX PI * 0.5f, // 90 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS PROJECTION, &proj); // set the render states Device->SetRenderState(D3DRS LIGHTING, false); return true; }Then, the Display function draws Triangle twice in two different positions and with different shade modes. The position of each triangle is controlled with the world matrix—World.
bool Display(float timeDelta) { if( Device ) { Device->Clear(0, 0, D3DCLEAR TARGET | D3DCLEAR ZBUFFER, 0xffffffff, 1.0f, 0); Device->BeginScene(); Device->SetFVF(ColorVertex::FVF); Device->SetStreamSource(0, Triangle, 0, sizeof(ColorVertex)); // draw the triangle to the left with flat shading D3DXMatrixTranslation(&World, -1.25f, 0.0f, 0.0f); Device->SetTransform(D3DTS WORLD, &World); Device->SetRenderState(D3DRS SHADEMODE, D3DSHADE FLAT); Device->DrawPrimitive(D3DPT TRIANGLELIST, 0, 1); // draw the triangle to the right with gouraud shading D3DXMatrixTranslation(&World, 1.25f, 0.0f, 0.0f); Device->SetTransform(D3DTS WORLD, &World); Device->SetRenderState(D3DRS SHADEMODE, D3DSHADE GOURAUD); Device->DrawPrimitive(D3DPT TRIANGLELIST, 0, 1); Device->EndScene(); Device->Present(0, 0, 0, 0); } return true; }flat 和 GOURAUD 两种方式到是好理解,主要是我想看看,他程序的结构,是在setup把vertex的模型写好,然后在display(){Device->BeginScene();}里面渲染。