作者:zy7ume | 来源:互联网 | 2023-06-26 13:08
本文主要讲茶壶是如何创建的,其余的内容可参考http:blog.csdn.netzero_wittyarticledetails51637459d3d自带的几何体可通过如下几步便可以简单
本文主要讲茶壶是如何创建的,其余的内容可参考 http://blog.csdn.net/zero_witty/article/details/51637459
d3d自带的几何体可通过如下几步便可以简单完成
想要通过这几个函数快捷绘制出一个几何体,需要以下几步:(以本例程的茶壶为例)
1.定义一个ID3DXMesh接口类型的对象。
ID3DXMesh* Teapot = 0;
2.接着调用相对应的创建函数如:
D3DXCreateTeapot(Device, &Teapot, 0);
/**HRESULT D3DXCreateTeapot(
LPDIRECT3DDEVICE9 pDevice, //Direct3D设备对象
LPD3DXMESH *ppMesh, //存储着我们创建的形状的指针的地址,即为输出项
LPD3DXBUFFER *ppAdjacency ); //它存储着我们在这里绘制的网格的三角形索引的指针//一般可不管
D3DXCreateTeapot()可用来创建一个茶壶
3.在Direct3D渲染后,也就是在BeginScene之后直接调用DrawSubset(0)就可以进行网格图形的绘制。
Teapot->DrawSubset(0);
4.绘制完成之后,像其他一样,Release便可以。
d3d::Release(Teapot);
teapot.cpp
#include "d3dUtility.h"
IDirect3DDevice9* Device=0;
const int Width = 640;
const int Height = 480;
ID3DXMesh* Teapot = 0;
bool Setup()
{
D3DXCreateTeapot(Device, &Teapot, 0);
D3DXVECTOR3 position(0.0f, 0.0f, -3.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);
//设置投影矩阵
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()
{
d3d::Release(Teapot);
}
bool Display(float timeDelta)
{
if (Device)
{
D3DXMATRIX Ry;
static float y = 0.0f;
D3DXMatrixRotationY(&Ry,y);
y += timeDelta;
if (y >= 6.28f)
y = 0.0f;
Device->SetTransform(D3DTS_WORLD, &Ry);
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Teapot->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;
}