Direct3D成长日记(03):模型视图投影变换
源码:
//////////////////////////////////////////////////////////////////////////
//作者 :shenzi
//完成时间 :2009.05.07
//说明 :Direct3D成长日记(03):模型视图投影变换。
//////////////////////////////////////////////////////////////////////////
//包含头文件,相应库:
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
//全局变量声明:
LPDIRECT3D9 g_pD3D = NULL; //Direct3D接口指针
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;//Direct3D设备指针
LPDIRECT3DVERTEXBUFFER9 g_pVBuffer = NULL; //顶点缓存
#define WINDOW_CLASS_NAME L"Direct3D"
#define WINDOW_NAME L"Direct3DBasics Tutorial"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
//顶点结构
struct CUSTOMVERTEX
{
FLOAT X, Y ,Z;
DWORD COLOR;
};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
//函数声明:
HRESULT InitD3D( HWND); //初始化Direct3D
void RenderFrame( void); //场景渲染
void CleanD3D( void); //释放资源
HRESULT InitGraphics(void); //初始化图形
void SetupMatrices(void); //模型视图投影矩阵设置
//WINDOWS 消息处理函数:
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);
//WinMain函数:
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hprevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//变量声明:
HWND hwnd;
MSG msg;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
//创建窗口类
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor( NULL, IDC_ARROW);
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = WINDOW_CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
//注册窗口类
RegisterClassEx(&wc);
//初始化窗口
hwnd = CreateWindowEx( NULL,
WINDOW_CLASS_NAME,
WINDOW_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
SCREEN_WIDTH,
SCREEN_HEIGHT,
NULL, NULL, hInstance, NULL);
//显示,更新窗口:
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
InitD3D(hwnd);
ZeroMemory(&msg, sizeof(MSG));
//消息循环:
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
RenderFrame();
}
UnregisterClass( WINDOW_CLASS_NAME, wc.hInstance );
return 0;
}
LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
CleanD3D();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
HRESULT InitD3D(HWND hwnd)
{
//创建D3D对象:
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
}
//定义窗口的显示信息:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
//创建D3D设备:
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3DDevice)))
{
return E_FAIL;
}
InitGraphics();
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
return S_OK;
}
void RenderFrame(void)
{
if(NULL == g_pD3DDevice)
return;
//清空背景缓冲区:
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
if(SUCCEEDED(g_pD3DDevice->BeginScene()))
{
//渲染场景:
//设置顶点结构:
g_pD3DDevice->SetFVF(CUSTOMFVF);
SetupMatrices();
//选取要显示的顶点
g_pD3DDevice->SetStreamSource(0, g_pVBuffer, 0, sizeof(CUSTOMVERTEX));
//绘制图元
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
g_pD3DDevice->EndScene();
}
//把渲染的场景显示在窗口:
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
void CleanD3D(void)
{
//释放资源
if (NULL != g_pVBuffer)
{
g_pVBuffer->Release();
}
if(NULL != g_pD3DDevice)
g_pD3DDevice->Release();
if(NULL != g_pD3D)
g_pD3D->Release();
g_pVBuffer = NULL;
g_pD3DDevice = NULL;
g_pD3D = NULL;
}
HRESULT InitGraphics(void)
{
//定义顶点数组
CUSTOMVERTEX vertices[] =
{
{ -2.0f, -2.0f, 0.0f, 0xffff0000,},
{ 0.0f, 2.0f, 0.0f, 0xff00ff00,},
{ 2.0f, -2.0f, 0.0f, 0xff0000ff,},
};
//建立顶点缓存
if (FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(vertices),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&g_pVBuffer,
NULL)))
{
return E_FAIL;
}
//通过Lock,Unlock 为顶点缓存填充数据(顶点信息):
VOID * pV;
if(FAILED(g_pVBuffer->Lock(0,
sizeof(vertices),
(void**)&pV,
0)))
{
return E_FAIL;
}
memcpy(pV, vertices, sizeof(vertices));
g_pVBuffer->Unlock();
}
void SetupMatrices(void)
{
//投影变换
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DX_PI/4,
(FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT,
0.1f,
100.0f);
//别忘了把变换后的矩阵应用于相应设备对应变换。
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProjection);
//视图变换
D3DXMATRIX matView;
D3DXVECTOR3 pEye(0.0f, 0.0f, -10.0f);
D3DXVECTOR3 pAt (0.0f, 0.0f, 0.0f);
D3DXVECTOR3 pUp (0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView,
&pEye,
&pAt,
&pUp);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
//模型变换
D3DXMATRIX matWorld;
D3DXMATRIX matRotX, matRotY, matRotZ;
D3DXMATRIX matTanslate;
D3DXMATRIX matScale;
static FLOAT angle = 0.0, scale = 1.0, distance = 0.0;
angle += 0.05;
distance += 0.005;
scale += 0.0005;
if (distance >= 5.0)
{
distance = 0.0f;
scale = 1.0;
}
//绕相应轴做旋转变换。
D3DXMatrixRotationY(&matRotX, angle);
D3DXMatrixRotationX(&matRotY, angle);
D3DXMatrixRotationZ(&matRotZ, angle);
//坐标平移操作。
D3DXMatrixTranslation(&matTanslate, 0.0f, 0.0f, distance );
//相应轴的放大缩小。
D3DXMatrixScaling(&matScale, scale, scale, scale);
//注意矩阵相乘的顺序!!!
matWorld = matRotX * matRotY * matRotZ * matTanslate * matScale ;
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
结果: