【Windows】
window class - WNDCLASS:wndclass
1.initialize
2.register
3.create window
……
4.show window
5.update window
【Direct3DX9】
1.initDirect3D
a.pD3D = Direct3DCreate
b.Create DXDevice interface - Set its parameters:D3DPRESENT_PARAMETERS d3dpp;
c.CreateDevice:pD3D->CreateDevice
2.MeshModel
D3DXCreateMeshFVF(12, 24, D3DXMESH_MANAGED, D3DFVF_CUSTOMVERTEX, g_pd3dDevice, &g_pCubeMesh);
//[1]Vertices
CUSTOMVERTEX *pVertices = NULL;
g_pCubeMesh->LockVertexBuffer(0, (void**)&pVertices);
pVertices[0] = CUSTOMVERTEX(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
…
g_pCubeMesh->UnlockVertexBuffer();
//[2]Indices
WORD *pIndices = NULL;
g_pCubeMesh->LockIndexBuffer(0, (void**)&pIndices);
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
…
g_pCubeMesh->UnlockIndexBuffer();
//[3]attribute
DWORD* pAttrib = NULL;
g_pCubeMesh->LockAttributeBuffer(0, &pAttrib);
for(int a = 0; a < 4; a++) pAttrib[a] = 0;
…
g_pCubeMesh->UnlockAttributeBuffer();
//[4]CreateTexture
D3DXCreateTextureFromFile(g_pd3dDevice, L"crate.jpg", &g_pTextures[0]);
…
//[5]Set filter model
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
…
3.View and Project matrix
//[1]view matrix
D3DXMATRIX matView;
D3DXVECTOR3 vEye(0.0f, 2.0, -8.0f);
D3DXVECTOR3 vAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &vEye, &vAt, &vUp);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
//[2]project matrix
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f, 1.0f, 1.0f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
4.Initial Light
//SetRenderState
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
【消息循环】
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message!=WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Direct3DRender(); // 绘制3D场景
}
}
【Render Direct3D Scene】
//[1]Clear
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f,
0);
//[2]Begin Scene
g_pd3dDevice->BeginScene();
//[3]Set world view project Matrix
D3DXMATRIX matWorld, Rx, Ry, Rz;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationX(&Rx, ::timeGetTime() / 1000.0f); // 绕X轴旋转
D3DXMatrixRotationY(&Ry, ::timeGetTime() / 1000.0f); // 绕Y轴旋转
D3DXMatrixRotationZ(&Rz, ::timeGetTime() / 1000.0f); // 绕Z轴旋转
matWorld = Rx * Ry * Rz * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
//[4]End Scene
g_pd3dDevice->EndScene(); // 结束绘制
//[5]Present the scene you want to draw
g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转
【Set light parameters】
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
light.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
light.Direction = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
g_pd3dDevice->SetLight(0, &light);
g_pd3dDevice->LightEnable(0, true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
【Load .x file in DirectX】
//Load mesh data from .x file
LPD3DXBUFFER pAdjBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Direct3D设备接口
LPD3DXMESH g_pMeshTiny = NULL; // 网格对象
D3DMATERIAL9* g_pMaterials = NULL; // 网格的材质信息
LPDIRECT3DTEXTURE9* g_pTextures = NULL; // 网格的纹理信息
DWORD g_dwNumMtrls = 0; // 材质的数目
// 从X文件中加载网格数据
D3DXLoadMeshFromX(L"tiny.x", D3DXMESH_MANAGED, g_pd3dDevice,
&pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMeshTiny);
// 读取材质和纹理数据
D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls];
g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls];
for (DWORD i=0; i<g_dwNumMtrls; i++)
{
g_pMaterials[i] = pMtrls[i].MatD3D;
g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse;
g_pTextures[i] = NULL;
D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrls[i].pTextureFilename, &g_pTextures[i]);
}
pAdjBuffer->Release();
pMtrlBuffer->Release();
【Projector effect】
【Keyboard & Mouse Event】
// 创建DirectInput设备
LPDIRECTINPUT8 g_pDirectInput = NULL;
DirectInput8Create(hInstance, 0x0800, IID_IDirectInput8, (void**)&g_pDirectInput, NULL);
LPDIRECTINPUTDEVICE8 g_pKeyboardDevice = NULL;
g_pDirectInput->CreateDevice(GUID_SysKeyboard, &g_pKeyboardDevice, NULL);
// 设置数据格式和协作级别
g_pKeyboardDevice->SetDataFormat(&c_dfDIKeyboard);
g_pKeyboardDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
g_pKeyboardDevice->Acquire();
【读取键盘输入】 in VOID Direct3DRender()
::ZeroMemory(g_pKeyStateBuffer, sizeof(g_pKeyStateBuffer));
ReadDevice(g_pKeyboardDevice, (LPVOID)g_pKeyStateBuffer, sizeof(g_pKeyStateBuffer));
// 平移物体
static FLOAT fPosX = 0.0f, fPosY = 0.0f;
if (g_pKeyStateBuffer[DIK_A] & 0x80) fPosX -= 0.01f;
if (g_pKeyStateBuffer[DIK_S] & 0x80) fPosY -= 0.01f;
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, fPosX, fPosY, 0.0f);
// 旋转物体
static float fAngleX = 0.0f, fAngleY = 0.0f;
if (g_pKeyStateBuffer[DIK_UP] & 0x80) fAngleX += 0.01f;
if (g_pKeyStateBuffer[DIK_RIGHT] & 0x80) fAngleY += 0.01f;
D3DXMATRIX Rx, Ry;
D3DXMatrixRotationX(&Rx, fAngleX);
D3DXMatrixRotationY(&Ry, fAngleY);
matWorld = Rx * Ry * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
【读取鼠标输入】
DIMOUSESTATE g_diMouseState = {0};
::ZeroMemory(&g_diMouseState, sizeof(g_diMouseState));
ReadDevice(g_pMouseDevice, (LPVOID)&g_diMouseState, sizeof(g_diMouseState));
// 鼠标左键控制平移
static FLOAT fPosX = 0.0f, fPosY = 0.0f, fPosZ = 0.0f;
if (g_diMouseState.rgbButtons[0] & 0x80)
{
fPosX += g_diMouseState.lX * 0.01f;
fPosY += g_diMouseState.lY * -0.01f;
}
fPosZ += g_diMouseState.lZ * 0.005f;
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, fPosX, fPosY, fPosZ);
// 鼠标右键旋转
static float fAngleX = 0.0f, fAngleY = 0.0f;
if (g_diMouseState.rgbButtons[1] & 0x80)
{
fAngleX += g_diMouseState.lY * -0.01f;
fAngleY += g_diMouseState.lX * -0.01f;
}
D3DXMATRIX Rx, Ry;
D3DXMatrixRotationX(&Rx, fAngleX);
D3DXMatrixRotationY(&Ry, fAngleY);
matWorld = Rx * Ry * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
【设置材质】
D3DMATERIAL9 mtrl;
::ZeroMemory(&mtrl, sizeof(mtrl));
mtrl.Ambient = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
mtrl.Diffuse = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);
//Specular & Emissive
g_pd3dDevice->SetMaterial(&mtrl);
【libraries】
winmm.lib;d3d9.lib;d3dx9d.lib;comctl32.lib;strmiids.lib;quartz.lib;
window class - WNDCLASS:wndclass
1.initialize
2.register
3.create window
……
4.show window
5.update window
【Direct3DX9】
1.initDirect3D
a.pD3D = Direct3DCreate
b.Create DXDevice interface - Set its parameters:D3DPRESENT_PARAMETERS d3dpp;
c.CreateDevice:pD3D->CreateDevice
2.MeshModel
D3DXCreateMeshFVF(12, 24, D3DXMESH_MANAGED, D3DFVF_CUSTOMVERTEX, g_pd3dDevice, &g_pCubeMesh);
//[1]Vertices
CUSTOMVERTEX *pVertices = NULL;
g_pCubeMesh->LockVertexBuffer(0, (void**)&pVertices);
pVertices[0] = CUSTOMVERTEX(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
…
g_pCubeMesh->UnlockVertexBuffer();
//[2]Indices
WORD *pIndices = NULL;
g_pCubeMesh->LockIndexBuffer(0, (void**)&pIndices);
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
…
g_pCubeMesh->UnlockIndexBuffer();
//[3]attribute
DWORD* pAttrib = NULL;
g_pCubeMesh->LockAttributeBuffer(0, &pAttrib);
for(int a = 0; a < 4; a++) pAttrib[a] = 0;
…
g_pCubeMesh->UnlockAttributeBuffer();
//[4]CreateTexture
D3DXCreateTextureFromFile(g_pd3dDevice, L"crate.jpg", &g_pTextures[0]);
…
//[5]Set filter model
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
…
3.View and Project matrix
//[1]view matrix
D3DXMATRIX matView;
D3DXVECTOR3 vEye(0.0f, 2.0, -8.0f);
D3DXVECTOR3 vAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &vEye, &vAt, &vUp);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
//[2]project matrix
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f, 1.0f, 1.0f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
4.Initial Light
//SetRenderState
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
【消息循环】
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message!=WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Direct3DRender(); // 绘制3D场景
}
}
【Render Direct3D Scene】
//[1]Clear
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f,
0);
//[2]Begin Scene
g_pd3dDevice->BeginScene();
//[3]Set world view project Matrix
D3DXMATRIX matWorld, Rx, Ry, Rz;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationX(&Rx, ::timeGetTime() / 1000.0f); // 绕X轴旋转
D3DXMatrixRotationY(&Ry, ::timeGetTime() / 1000.0f); // 绕Y轴旋转
D3DXMatrixRotationZ(&Rz, ::timeGetTime() / 1000.0f); // 绕Z轴旋转
matWorld = Rx * Ry * Rz * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
//[4]End Scene
g_pd3dDevice->EndScene(); // 结束绘制
//[5]Present the scene you want to draw
g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // 翻转
【Set light parameters】
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
light.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
light.Direction = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
g_pd3dDevice->SetLight(0, &light);
g_pd3dDevice->LightEnable(0, true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
【Load .x file in DirectX】
//Load mesh data from .x file
LPD3DXBUFFER pAdjBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Direct3D设备接口
LPD3DXMESH g_pMeshTiny = NULL; // 网格对象
D3DMATERIAL9* g_pMaterials = NULL; // 网格的材质信息
LPDIRECT3DTEXTURE9* g_pTextures = NULL; // 网格的纹理信息
DWORD g_dwNumMtrls = 0; // 材质的数目
// 从X文件中加载网格数据
D3DXLoadMeshFromX(L"tiny.x", D3DXMESH_MANAGED, g_pd3dDevice,
&pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMeshTiny);
// 读取材质和纹理数据
D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls];
g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls];
for (DWORD i=0; i<g_dwNumMtrls; i++)
{
g_pMaterials[i] = pMtrls[i].MatD3D;
g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse;
g_pTextures[i] = NULL;
D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrls[i].pTextureFilename, &g_pTextures[i]);
}
pAdjBuffer->Release();
pMtrlBuffer->Release();
【Projector effect】
【Keyboard & Mouse Event】
// 创建DirectInput设备
LPDIRECTINPUT8 g_pDirectInput = NULL;
DirectInput8Create(hInstance, 0x0800, IID_IDirectInput8, (void**)&g_pDirectInput, NULL);
LPDIRECTINPUTDEVICE8 g_pKeyboardDevice = NULL;
g_pDirectInput->CreateDevice(GUID_SysKeyboard, &g_pKeyboardDevice, NULL);
// 设置数据格式和协作级别
g_pKeyboardDevice->SetDataFormat(&c_dfDIKeyboard);
g_pKeyboardDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
g_pKeyboardDevice->Acquire();
【读取键盘输入】 in VOID Direct3DRender()
::ZeroMemory(g_pKeyStateBuffer, sizeof(g_pKeyStateBuffer));
ReadDevice(g_pKeyboardDevice, (LPVOID)g_pKeyStateBuffer, sizeof(g_pKeyStateBuffer));
// 平移物体
static FLOAT fPosX = 0.0f, fPosY = 0.0f;
if (g_pKeyStateBuffer[DIK_A] & 0x80) fPosX -= 0.01f;
if (g_pKeyStateBuffer[DIK_S] & 0x80) fPosY -= 0.01f;
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, fPosX, fPosY, 0.0f);
// 旋转物体
static float fAngleX = 0.0f, fAngleY = 0.0f;
if (g_pKeyStateBuffer[DIK_UP] & 0x80) fAngleX += 0.01f;
if (g_pKeyStateBuffer[DIK_RIGHT] & 0x80) fAngleY += 0.01f;
D3DXMATRIX Rx, Ry;
D3DXMatrixRotationX(&Rx, fAngleX);
D3DXMatrixRotationY(&Ry, fAngleY);
matWorld = Rx * Ry * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
【读取鼠标输入】
DIMOUSESTATE g_diMouseState = {0};
::ZeroMemory(&g_diMouseState, sizeof(g_diMouseState));
ReadDevice(g_pMouseDevice, (LPVOID)&g_diMouseState, sizeof(g_diMouseState));
// 鼠标左键控制平移
static FLOAT fPosX = 0.0f, fPosY = 0.0f, fPosZ = 0.0f;
if (g_diMouseState.rgbButtons[0] & 0x80)
{
fPosX += g_diMouseState.lX * 0.01f;
fPosY += g_diMouseState.lY * -0.01f;
}
fPosZ += g_diMouseState.lZ * 0.005f;
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, fPosX, fPosY, fPosZ);
// 鼠标右键旋转
static float fAngleX = 0.0f, fAngleY = 0.0f;
if (g_diMouseState.rgbButtons[1] & 0x80)
{
fAngleX += g_diMouseState.lY * -0.01f;
fAngleY += g_diMouseState.lX * -0.01f;
}
D3DXMATRIX Rx, Ry;
D3DXMatrixRotationX(&Rx, fAngleX);
D3DXMatrixRotationY(&Ry, fAngleY);
matWorld = Rx * Ry * matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
【设置材质】
D3DMATERIAL9 mtrl;
::ZeroMemory(&mtrl, sizeof(mtrl));
mtrl.Ambient = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
mtrl.Diffuse = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);
//Specular & Emissive
g_pd3dDevice->SetMaterial(&mtrl);
【libraries】
winmm.lib;d3d9.lib;d3dx9d.lib;comctl32.lib;strmiids.lib;quartz.lib;