LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL; // 顶点buffer
struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD colour;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
HRESULT InitialiseD3D(HWND hWnd)
{
//首先创建主要的D3D对象.如果创建成功我们将会得到一个指向IDirect3D9接口的指针
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
//获取当前的显示模式
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//创建一个结构来保存我们设备的设置
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//填充结构体
//我们想让我们的程序窗口化,并且设置了后缓冲的格式以匹配我们当前的显示模式
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.BackBufferFormat = d3ddm.Format;
//创建一个Direct3D设备.
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
{
return E_FAIL;
}
//开启背面拣选.因为我们想要隐藏多边形的背面
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
//关闭灯光因为我们指定我们的顶点具有颜色
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
HRESULT InitialiseVertexBuffer()
{
VOID* pVertices;
//存储立方体的每一个顶点和它的颜色
//确保多边形的顶点是按顺时针方向给定的
//这是因为逆时针的面将要被拣选
//我们将使用1个三角形带来渲染这些多边形(顶,边,底)
CUSTOMVERTEX cvVertices[] =
{
//顶面
{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 - Blue
{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red
{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Red
{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Green
//面 1
{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 - Red
{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 - Blue
{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 - Green
{5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 - Red
//面 2
{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 - Blue
{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 - Green
//面 3
{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 - Green
{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 - Red
//面 4
{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 - Red
{-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 - Blue
//底面
{5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 - Green
{5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 - Blue
{-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 - Red
{-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 - Green
};
//由我们的设备创建顶点buffer
if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVertexBuffer,NULL)))
{
return E_FAIL;
}
//得到一个指向顶点缓冲顶点的指针并锁定顶点缓冲
if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), ( void**)&pVertices, 0)))
{
return E_FAIL;
}
//将我们存储的顶点值拷贝到顶点缓冲
memcpy(pVertices, cvVertices, sizeof(cvVertices));
//解锁顶点缓冲
g_pVertexBuffer->Unlock();
return S_OK;
}
void SetupRotation()
{
//这里我们将要沿着x,y,z轴选择我们的世界
D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;
//创建变换矩阵
D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);
D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);
D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);
//通过相乘合并变换
D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);
//应用变换
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void SetupCamera()
{
//这里我们设置摄影机
//摄影机有3个设置: "摄影机位置", "观察点位置" and "Up Direction"
//我们设置如下:
//摄影机位置: (0, 0, -30)
//观察点位置: (0, 0, 0)
//Up direction: Y-Axis.
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-30.0f), //摄影机位置
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //观察点位置
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
void SetupPerspective()
{
//这里我们设置视野,纵横比例,远近截面
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//清除后缓冲为黑色
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//场景开始
g_pD3DDevice->BeginScene();
//设置旋转,摄影机和透视矩阵
SetupRotation();
SetupCamera();
SetupPerspective();
//渲染我们的对象
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer,0, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //顶
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //侧
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //底
//场景结束
g_pD3DDevice->EndScene();
//翻动前后缓冲以便后缓冲中渲染的一切能够显示在屏幕(前缓冲)上。
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
void CleanUp()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
void GameLoop()
{
//进入游戏循环
MSG msg;
BOOL fMessage;
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
if(fMessage)
{
//处理消息
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//没有要处理的消息,所以渲染当前场景
Render();
}
}
}
//窗口消息处理
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
//用户已经按了ESC键,所以退出
DestroyWindow(hWnd);
return 0;
break;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//应用程序入口点
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
//注册窗口类
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"DX Project 3", NULL};
RegisterClassEx(&wc);
//创建应用程序的窗口
HWND hWnd = CreateWindow( "DX Project 3", "www.andypike.com: Tutorial 3",
WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
//初始化 Direct3D
if(SUCCEEDED(InitialiseD3D(hWnd)))
{
//显示我们的窗体
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//初始化顶点缓冲
if(SUCCEEDED(InitialiseVertexBuffer()))
{
//启动游戏运行:进入游戏循环
GameLoop();
}
}
CleanUp();
UnregisterClass( "DX Project 3", wc.hInstance);
return 0;
}
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/326106
,如需转载请自行联系原作者