- 用矩阵实现移动摄像机、物体。
本次依旧使用之前的DX模板作为初始代码,我们首先要实现的是利用平移矩阵实现平移。
D3DXMATRIX g_Translation;
//声明一个矩阵
然后我们在初始化函数内生成这个矩阵的数据
D3DXMatrixTranslation(&g_Translation,0,0,10);
/*
参数一:是输出计算结果的目标矩阵。
参数二:x坐标平移距离。
参数三:y坐标平移距离。
参数四:z坐标平移距离。
*/
最后,在渲染时设定矩阵
g_D3DDevice->SetTransform(D3DTS_PROJECTION,&g_Translation)
若有多个矩阵要同时使用,我们可以用矩阵相乘将矩阵复合成一个矩阵。
用这个矩阵转换的效果,等同于用用于生成这个矩阵的所有矩阵依次转换。
如果是方阵,那么相乘顺序没有特定要求,若不是方阵则需要遵守相应规则。详细可以百度矩阵乘法,这里不细说。下面是同时平移并绕Y轴旋转的例子。
D3DXMATRIX g_Translation;
//声明平移矩阵
D3DXMATRIX g_Rotation;
//声明旋转矩阵
D3DXMatrixTranslation(&g_Translation,0,0,10);
//计算平移矩阵
D3DXMatrixRotationY(&g_Rotation,10);
//计算旋转矩阵
g_Projection = g_Translation*g_Rotation;
//计算复合矩阵
g_D3DDevice->SetTransForm(D3DTS_PROCJECTION,&g_Projection)
//设定最终矩阵
若我们循环改变旋转的角度或者平移的距离那么我们就可以实现缓速或快速连续的移动图形,这个效果有点像动画一样。这里我给出代码,有兴趣的可以试一试。
#include "d3d9.h"
#include "d3dx9.h"
#define ClassName "UGPDX"
#define WindowName "Demo_Window"
#define MenuName NULL
#define WINDOW_WIDTH 640
#define WINDOW_HIGHT 480
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
D3DXMATRIX g_projection;
D3DXMATRIX g_worldMatrix;
D3DXMATRIX g_translation;
D3DXMATRIX g_rotation;
float g_angle = 0.0f;
//把角度定义成变量,方便我们循环更改它。
bool InitializeD3D(HWND hWnd);
bool InitializeObjects();
void RenderScene();
void Shutdown();
struct stD3DVertex
{
float x, y, z;
unsigned long color;
};
#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
LRESULT WINAPI WndPro(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC,WndPro,NULL,NULL,hInstance,NULL,NULL,NULL
,MenuName,ClassName };
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(NULL, ClassName, WindowName, WS_OVERLAPPEDWINDOW, 100, 100, WINDOW_WIDTH, WINDOW_HIGHT, NULL, NULL, hInstance, NULL);
if (InitializeD3D(hWnd))
{
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderScene();
}
}
Shutdown();
UnregisterClass(ClassName, hInstance);
}
return 0;
}
bool InitializeD3D(HWND hWnd)
{
D3DDISPLAYMODE displaymode;
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!g_D3D) return false;
if (g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displaymode)) return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = displaymode.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
{
return false;
}
if (!InitializeObjects())
{
return false;
}
return true;
}
void Shutdown()
{
if (g_D3DDevice != NULL) g_D3DDevice->Release();
if (g_D3D != NULL) g_D3D->Release();
if (g_VertexBuffer != NULL) g_VertexBuffer->Release();
g_D3DDevice = NULL;
g_D3D = NULL;
g_VertexBuffer = NULL;
}
void RenderScene()
{
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
g_D3DDevice->BeginScene();
D3DXMatrixTranslation(&g_translation, 0.0f, 0.0f, 3.0f);
//计算平移矩阵 x->x, y->y, z->z+3
D3DXMatrixRotationY(&g_rotation, g_angle);
//设置旋转角度 此处为0 即不旋转
g_worldMatrix = g_rotation * g_translation;
//生成世界矩阵
g_angle += 0.01f;
if (g_angle>=360)
{
g_angle = 0.0f;
}
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_worldMatrix);
g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,sizeof(stD3DVertex));
//设置数据来源
g_D3DDevice->SetFVF(D3DFVF_VERTEX);
//设置顶点格式
/*g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0,1);*/
//绘制图元 线列表,从第0索引开始,绘制2个
g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_D3DDevice->EndScene();
//结束场景渲染
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
//显示渲染
}
bool InitializeObjects()
{
D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f, WINDOW_WIDTH / WINDOW_HIGHT,0.1,1000);
g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, false);
//关闭光照
g_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
//禁用背面消隐
stD3DVertex objData[] =
{
{-0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,255,0) },
{ 0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,0,0) },
{ 0.0f, 0.3f, 1.0f, D3DCOLOR_XRGB(0,0,255) },
};
if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL)))
return false;
void *ptr;
//定义一个指针 用来操作缓存区
if (FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0))) return false;
//在复制数据之前一定要将缓存区锁定 否则会出错
memcpy(ptr, objData, sizeof(objData));
//复制数据
g_VertexBuffer->Unlock();
//复制完之后解锁缓存区
return true;
}
这里提醒下步骤,首先把传入计算矩阵函数的角度或平移距离定义为变量,方便我们修改,然后把函数参数用定义的变量替换,在每次计算完矩阵后改变它们的值,下一次渲染时再计算就会使用新的值,要注意的是记得判断是否转了一圈,在转了一圈的时候我们应该把角度归零,这样方便我们直观的了解物体旋转的位置数据,如果你每次改变的值大,那么物体动的就很快,反之亦然。由于我们在渲染时是很快的,每秒都有成百上千次,所以每次改变的角度不应太大,否则就快的看不清了,建议刚开始用0.01左右。