可参考之前博客写的一个demo
http://blog.csdn.net/zero_witty/article/details/51651162
demo的主要内容在框架函数Setup(),Display()两个上,其余的函数跟其他demo的函数一致,故不在细究。
首先设置全局变量,设置5个对象去代表每个几何体的网格元素,定义个世界矩阵,去代表其各自的在世界坐标系的坐标。
ID3DXMesh* Objects[5] = { 0, 0, 0, 0, 0 };
D3DXMATRIX ObjWorldMatrices[5];
Setup()函数主要完成了
通过自定的D3DXCreateXXX()函数(在代码注释里解释了每个函数的参数意思)去创建自带的几何体,并为每个几何体的世界矩阵进行填充。并设置投影矩阵,并选择线框模式渲染。
bool Setup()
{
D3DXCreateTeapot(Device, &Objects[0], 0);
D3DXCreateBox(Device, 2.0f, 2.0f, 2.0f, &Objects[1], 0);
/* HRESULT D3DXCreateBox(
__in LPDIRECT3DDEVICE9 pDevice,//Direct3D设备对象
__in FLOAT Width, //盒子的宽度
__in FLOAT Height,//盒子的高度
__in FLOAT Depth,//盒子的深度
__out LPD3DXMESH *ppMesh,//存储着盒子网格的指针
__out LPD3DXBUFFER *ppAdjacency//存储三角形索引的指针
);
*/
// cylinder is built aligned on z-axis
D3DXCreateCylinder(Device, 1.0f, 1.0f, 3.0f, 10, 10, &Objects[2], 0);
/*
HRESULT D3DXCreateCylinder(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT Radius1, //创建的柱体沿Z轴负方向半径的大小
__in FLOAT Radius2, //创建的柱体沿Z轴正方向半径的大小
__in FLOAT Length, //创建的柱体沿Z轴方向的长度。
__in UINT Slices, //表示柱体外围有几个面
__in UINT Stacks, //柱体两端间共有几段
__out LPD3DXMESH *ppMesh, //存储柱体的指针
__out LPD3DXBUFFER *ppAdjacency//绘制 网格存储的三角形索引的指针
);
*/
//创建圆环
D3DXCreateTorus(Device, 1.0f, 3.0f, 10, 10, &Objects[3], 0);
/*
HRESULT D3DXCreateTorus(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT InnerRadius, //内圈半径
__in FLOAT OuterRadius, //外圈半径
__in UINT Sides, //外圈有几个面
__in UINT Rings, //内圈与外圈之间共有几个面
__out LPD3DXMESH *ppMesh,
__out LPD3DXBUFFER *ppAdjacency
);
*/
D3DXCreateSphere(Device,1.0f,10,10, &Objects[4],0);
/*
HRESULT D3DXCreateSphere(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT Radius, //球面半径
__in UINT Slices, //用几条经线绘制
__in UINT Stacks, //用几条纬线绘制
__out LPD3DXMESH *ppMesh,
__out LPD3DXBUFFER *ppAdjacency
);
*/
D3DXMatrixTranslation(&ObjWorldMatrices[0], 0.0f, 0.0f, 0.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[2], 5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[4], 5.0f, 0.0f, -5.0f);
//设置投影矩阵
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f,(float)Width / (float)Height,1.0f,1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
Display()函数设置了一个动画镜头。摄像机将会绕着场景中心环绕。我们使用sin,cos去生成圆上的一点,将其扩大十倍去延展到环绕圈的半径上。此外,相机会向上或向下移动,因为相机环绕着场景。
bool Display(float timeDelta)
{
if (Device)
{
static float angle = (3.0f*D3DX_PI) / 2.0f;
static float cameraHeight = 0.0f;
static float cameraHeightDirection = 2.0f;
D3DXVECTOR3 position(cosf(angle)*10.0f, cameraHeight, sinf(angle)*10.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
angle += timeDelta;
if (angle >= 6.28f)
angle = 0.0f;
cameraHeight += cameraHeightDirection*timeDelta;
if (cameraHeight >= 10.0f)
cameraHeightDirection = -2.0f;
if (cameraHeight <= -10.0f)
cameraHeightDirection = 2.0f;
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
for (int i = 0; i < 5; i++)
{
Device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[i]);
Objects[i]->DrawSubset(0);
}
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
完整代码,其中不包含d3dUtility.cpp,d3dUtility.h(可以去此链接自行获取 http://blog.csdn.net/zero_witty/article/details/51627916)
d3dcreate.cpp
#include "d3dUtility.h"
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
ID3DXMesh* Objects[5] = { 0, 0, 0, 0, 0 };
D3DXMATRIX ObjWorldMatrices[5];
//每个对象的世界矩阵,表明他们在世界坐标系的坐标
bool Setup()
{
D3DXCreateTeapot(Device, &Objects[0], 0);
D3DXCreateBox(Device, 2.0f, 2.0f, 2.0f, &Objects[1], 0);
/* HRESULT D3DXCreateBox(
__in LPDIRECT3DDEVICE9 pDevice,//Direct3D设备对象
__in FLOAT Width, //盒子的宽度
__in FLOAT Height,//盒子的高度
__in FLOAT Depth,//盒子的深度
__out LPD3DXMESH *ppMesh,//存储着盒子网格的指针
__out LPD3DXBUFFER *ppAdjacency//存储三角形索引的指针
);
*/
// cylinder is built aligned on z-axis
D3DXCreateCylinder(Device, 1.0f, 1.0f, 3.0f, 10, 10, &Objects[2], 0);
/*
HRESULT D3DXCreateCylinder(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT Radius1, //创建的柱体沿Z轴负方向半径的大小
__in FLOAT Radius2, //创建的柱体沿Z轴正方向半径的大小
__in FLOAT Length, //创建的柱体沿Z轴方向的长度。
__in UINT Slices, //表示柱体外围有几个面
__in UINT Stacks, //柱体两端间共有几段
__out LPD3DXMESH *ppMesh, //存储柱体的指针
__out LPD3DXBUFFER *ppAdjacency//绘制 网格存储的三角形索引的指针
);
*/
//创建圆环
D3DXCreateTorus(Device, 1.0f, 3.0f, 10, 10, &Objects[3], 0);
/*
HRESULT D3DXCreateTorus(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT InnerRadius, //内圈半径
__in FLOAT OuterRadius, //外圈半径
__in UINT Sides, //外圈有几个面
__in UINT Rings, //内圈与外圈之间共有几个面
__out LPD3DXMESH *ppMesh,
__out LPD3DXBUFFER *ppAdjacency
);
*/
D3DXCreateSphere(Device,1.0f,10,10, &Objects[4],0);
/*
HRESULT D3DXCreateSphere(
__in LPDIRECT3DDEVICE9 pDevice,
__in FLOAT Radius, //球面半径
__in UINT Slices, //用几条经线绘制
__in UINT Stacks, //用几条纬线绘制
__out LPD3DXMESH *ppMesh,
__out LPD3DXBUFFER *ppAdjacency
);
*/
D3DXMatrixTranslation(&ObjWorldMatrices[0], 0.0f, 0.0f, 0.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[2], 5.0f, 0.0f, 5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f);
D3DXMatrixTranslation(&ObjWorldMatrices[4], 5.0f, 0.0f, -5.0f);
//设置投影矩阵
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f,(float)Width / (float)Height,1.0f,1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
void Cleanup()
{
for (int i = 0; i < 5; i++)
d3d::Release<ID3DXMesh*>(Objects[i]);
}
bool Display(float timeDelta)
{
if (Device)
{
static float angle = (3.0f*D3DX_PI) / 2.0f;
static float cameraHeight = 0.0f;
static float cameraHeightDirection = 2.0f;
D3DXVECTOR3 position(cosf(angle)*10.0f, cameraHeight, sinf(angle)*10.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
angle += timeDelta;
if (angle >= 6.28f)
angle = 0.0f;
cameraHeight += cameraHeightDirection*timeDelta;
if (cameraHeight >= 10.0f)
cameraHeightDirection = -2.0f;
if (cameraHeight <= -10.0f)
cameraHeightDirection = 2.0f;
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
for (int i = 0; i < 5; i++)
{
Device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[i]);
Objects[i]->DrawSubset(0);
}
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
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;
}