1. 首先知道渲染代码放置位置。渲染代码放在WinMain消息循环中
while(msg.message!=WM_QUIT)
{
if(PeekMessage(****)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderScene();
}
}
RenderScene函数中是绘制场景。
//在WinMain函数中,调用渲染场景前,要先创建D3D设备,步骤如下:
1. 创建D3D object对象
LPDIRECT3D g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
2. 获取桌面窗口显示模式
D3DISPLAYMODE displayMode;
g_D3D->GetAdpterDisplayMode(D3DADPTER_DEFAULT,&displayMode)));
//创建用于设置d3d设备的数据结构
D3DPRENT_PARAMETERS d3dpp;
d3dpp.Windowed = FALSE; //是窗口还是全屏
d3dpp.BackBufferWidth = 600;
d3dpp.BackBufferHeight = 800; // 后备缓冲区宽度和高度
//还有其他一些函数,采样类型,设置是否使用深度缓冲等。 ???深度缓冲
3. 创建设备
g_D3D->CreateDevice(****);
//创建完成设备后,设置渲染模式
g_D3D->SetRenderState(*,*);
SetRenderState如下:
HRESULT SetRenderState(
[in] D3DRENDERSTATETYPE State,
[in] DWORD Value
); 其中第一个参数如下:
typedef enum D3DRENDERSTATETYPE {
D3DRS_ZENABLE = 7,// z缓存
D3DRS_FILLMODE = 8,
D3DRS_SHADEMODE = 9,
D3DRS_ZWRITEENABLE = 14,
D3DRS_ALPHATESTENABLE = 15,
D3DRS_LASTPIXEL = 16,
D3DRS_SRCBLEND = 19,
D3DRS_DESTBLEND = 20,
D3DRS_CULLMODE = 22,
D3DRS_ZFUNC = 23,
D3DRS_ALPHAREF = 24,
D3DRS_ALPHAFUNC = 25,
D3DRS_DITHERENABLE = 26,
D3DRS_ALPHABLENDENABLE = 27,
D3DRS_FOGENABLE = 28,
D3DRS_SPECULARENABLE = 29,
D3DRS_FOGCOLOR = 34,
D3DRS_FOGTABLEMODE = 35,
D3DRS_FOGSTART = 36,
D3DRS_FOGEND = 37,
D3DRS_FOGDENSITY = 38,
D3DRS_RANGEFOGENABLE = 48,
D3DRS_STENCILENABLE = 52,
D3DRS_STENCILFAIL = 53,
D3DRS_STENCILZFAIL = 54,
D3DRS_STENCILPASS = 55,
D3DRS_STENCILFUNC = 56,
D3DRS_STENCILREF = 57,
D3DRS_STENCILMASK = 58,
D3DRS_STENCILWRITEMASK = 59,
D3DRS_TEXTUREFACTOR = 60,
D3DRS_WRAP0 = 128,
D3DRS_WRAP1 = 129,
D3DRS_WRAP2 = 130,
D3DRS_WRAP3 = 131,
D3DRS_WRAP4 = 132,
D3DRS_WRAP5 = 133,
D3DRS_WRAP6 = 134,
D3DRS_WRAP7 = 135,
D3DRS_CLIPPING = 136,
D3DRS_LIGHTING = 137,
D3DRS_AMBIENT = 139,
D3DRS_FOGVERTEXMODE = 140,
D3DRS_COLORVERTEX = 141,
D3DRS_LOCALVIEWER = 142,
D3DRS_NORMALIZENORMALS = 143,
D3DRS_DIFFUSEMATERIALSOURCE = 145,
D3DRS_SPECULARMATERIALSOURCE = 146,
D3DRS_AMBIENTMATERIALSOURCE = 147,
D3DRS_EMISSIVEMATERIALSOURCE = 148,
D3DRS_VERTEXBLEND = 151,
D3DRS_CLIPPLANEENABLE = 152,
D3DRS_POINTSIZE = 154,
D3DRS_POINTSIZE_MIN = 155,
D3DRS_POINTSPRITEENABLE = 156,
D3DRS_POINTSCALEENABLE = 157,
D3DRS_POINTSCALE_A = 158,
D3DRS_POINTSCALE_B = 159,
D3DRS_POINTSCALE_C = 160,
D3DRS_MULTISAMPLEANTIALIAS = 161,
D3DRS_MULTISAMPLEMASK = 162,
D3DRS_PATCHEDGESTYLE = 163,
D3DRS_DEBUGMONITORTOKEN = 165,
D3DRS_POINTSIZE_MAX = 166,
D3DRS_INDEXEDVERTEXBLENDENABLE = 167,
D3DRS_COLORWRITEENABLE = 168,
D3DRS_TWEENFACTOR = 170,
D3DRS_BLENDOP = 171,
D3DRS_POSITIONDEGREE = 172,
D3DRS_NORMALDEGREE = 173,
D3DRS_SCISSORTESTENABLE = 174,
D3DRS_SLOPESCALEDEPTHBIAS = 175,
D3DRS_ANTIALIASEDLINEENABLE = 176,
D3DRS_MINTESSELLATIONLEVEL = 178,
D3DRS_MAXTESSELLATIONLEVEL = 179,
D3DRS_ADAPTIVETESS_X = 180,
D3DRS_ADAPTIVETESS_Y = 181,
D3DRS_ADAPTIVETESS_Z = 182,
D3DRS_ADAPTIVETESS_W = 183,
D3DRS_ENABLEADAPTIVETESSELLATION = 184,
D3DRS_TWOSIDEDSTENCILMODE = 185,
D3DRS_CCW_STENCILFAIL = 186,
D3DRS_CCW_STENCILZFAIL = 187,
D3DRS_CCW_STENCILPASS = 188,
D3DRS_CCW_STENCILFUNC = 189,
D3DRS_COLORWRITEENABLE1 = 190,
D3DRS_COLORWRITEENABLE2 = 191,
D3DRS_COLORWRITEENABLE3 = 192,
D3DRS_BLENDFACTOR = 193,
D3DRS_SRGBWRITEENABLE = 194,
D3DRS_DEPTHBIAS = 195,
D3DRS_WRAP8 = 198,
D3DRS_WRAP9 = 199,
D3DRS_WRAP10 = 200,
D3DRS_WRAP11 = 201,
D3DRS_WRAP12 = 202,
D3DRS_WRAP13 = 203,
D3DRS_WRAP14 = 204,
D3DRS_WRAP15 = 205,
D3DRS_SEPARATEALPHABLENDENABLE = 206,
D3DRS_SRCBLENDALPHA = 207,
D3DRS_DESTBLENDALPHA = 208,
D3DRS_BLENDOPALPHA = 209,
D3DRS_FORCE_DWORD = 0x7fffffff
} D3DRENDERSTATETYPE, *LPD3DRENDERSTATETYPE;
参数太多,自己看了下directx api文档,http://www.google.com.hk/#newwindow=1&q=directx+api&safe=strict,主要设置渲染状态,但对这些状态了解太少了,对有些状态设置后,场景中要显示的物体没有显示。
设置完成渲染状态后,设置要渲染的对象。
//设置透视投影矩阵,设置观察矩阵,世界矩阵
g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);
g_D3DDevice->SetTransform(D3DTS_VIEW, &g_pview);
g_D3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)g_bones[i].absolute.matrix);调试代码,无法将这些矩阵和在窗口显示的关联在一起,不明白。
//设置要渲染的数据
g_D3DDevice->DrawPrimitiveUP(D3DPT_LINELIST, 4,
baseLines, sizeof(Vertex));
//将数据显示窗口中
g_D3Device->EndScene();
g_D3Device->Present(NULL,NULL,NULL,NULL);
这样在窗口中可以看到渲染的物体对象。