这个demo主要演示了用平面着色模式与Gouraud着色模式对三角形进行着色的不同
首先添加了全局变量与其他demo不同的是多了个世界变化矩阵
IDirect3DDevice9* Device;
D3DXMATRIX WorldMatrix;//所存储三角形的世界变化矩阵
IDirect3DVertexBuffer9* Triangle = 0;
const int Width = 640;
const int Height = 480;
首先我们需定义一个结构体ColorVertex,包含着顶点位置,顶点类型,以及颜色,与其的构造方法。
struct ColorVertex{
ColorVertex(){};
ColorVertex(float x, float y, float z, D3DCOLOR color)
{
_x = x;
_y = y;
_z = z;
_color = color;
}
float _x, _y, _z;
D3DCOLOR _color;
static const DWORD FVF;
};
const DWORD ColorVertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
接着便是Setup()方法,先创建了顶点缓存,并用所要绘制的三角形中带有颜色信息的顶点对该缓存进行填充。并设置投影矩阵,并将光照禁用(不然会使三角形都是黑色。)
bool Setup()
{
Device->CreateVertexBuffer(3 * sizeof(ColorVertex), D3DUSAGE_WRITEONLY, ColorVertex::FVF, D3DPOOL_MANAGED, &Triangle, 0);
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();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f/*近裁面*/, 1000.f/*远裁面*/);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_LIGHTING, false);//将光照禁用
return true;
}
最后需要改动的方法便是Display(),它在本例中绘制了2个不同着色模式下的三角形。(三角形的位置是通过世界矩阵确定的)
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));
D3DXMatrixTranslation(&WorldMatrix, 1.25f, 0.0f, 0.0f);
Device->SetTransform(D3DTS_WORLD, &WorldMatrix);
//平滑着色
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
D3DXMatrixTranslation(&WorldMatrix, -1.25f, 0.0f, 0.0f);
Device->SetTransform(D3DTS_WORLD, &WorldMatrix);
//Gougraud着色
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
corlortriangle.cpp
//平面着色模式和Gouraud着色模式对三角形着色
#include"d3dUtility.h"
IDirect3DDevice9* Device;
D3DXMATRIX WorldMatrix;//所存储三角形的世界变化矩阵
IDirect3DVertexBuffer9* Triangle = 0;
const int Width = 640;
const int Height = 480;
struct ColorVertex{
ColorVertex(){};
ColorVertex(float x, float y, float z, D3DCOLOR color)
{
_x = x;
_y = y;
_z = z;
_color = color;
}
float _x, _y, _z;
D3DCOLOR _color;
static const DWORD FVF;
};
const DWORD ColorVertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
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));
D3DXMatrixTranslation(&WorldMatrix, 1.25f, 0.0f, 0.0f);
Device->SetTransform(D3DTS_WORLD, &WorldMatrix);
//平滑着色
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
D3DXMatrixTranslation(&WorldMatrix, -1.25f, 0.0f, 0.0f);
Device->SetTransform(D3DTS_WORLD, &WorldMatrix);
//Gougraud着色
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
}
bool Setup()
{
Device->CreateVertexBuffer(3 * sizeof(ColorVertex), D3DUSAGE_WRITEONLY, ColorVertex::FVF, D3DPOOL_MANAGED, &Triangle, 0);
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();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f/*近裁面*/, 1000.f/*远裁面*/);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_LIGHTING, false);//将光照禁用
return true;
}
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
{
if (!d3d::InitD3D(hinstance, Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if (!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop(Display);
Cleanup();
Device->Release();
return 0;
}
至于d3dUtility.cpp和d3dUtili.h可以去此链接领取http://blog.csdn.net/zero_witty/article/details/51627916
例程图片: