哇!到了传说中的3D摄像机啦!
之前我们写的东东,都是观察点不动,通过世界变换让东西动,今天,通过三维摄像机我们就可以改变我们的观察点,观察方向,随意在三维空间中驰骋。之前我们所设定的视角都是通过D3DXMatrixLookAtLH这个函数,通过几个设定好的向量,将视角初始化的,而在程序真正运行时,视角就不再改变了,而这次,我们要实时的生成视角变换矩阵,实时的改变我们的视角,我们所谓的摄像机就是这个原理。
我们通过一个类来封装摄像机,这个类主要的字段就是用四个分量:右分量(rightvector)、上分量(up vector)、观察分量(lookvector)和位置分量(position vector),来确定一个摄像机相对于世界坐标系的位置和朝向。并根据这四个分量计算出一个取景变换矩阵,然后通过取景变换(VIEWTRANSFORM)实现改变视角。
通过这些分量,我们可以确定摄像机的位置和朝向。操作有平移和旋转,那么一共就有6中操作:
(1)沿着观察分量平移(前进后退)
(2)沿着观察分量旋转(左右翻滚)
(3)沿着右分量平移(左右移动)
(4)沿着右分量旋转(朝上下看)
(5)沿着上分量平移(上下移动)
(6)沿着上分量旋转(朝左右看)
void CCamera::CalculateViewMatrix(D3DXMATRIX *pMatrix) { //规范化三个向量使之成为正交矩阵 //规范化观察向量 D3DXVec3Normalize(&m_vLookVector, &m_vLookVector); //使上向量与观察向量垂直 D3DXVec3Cross(&m_vUpVector, &m_vLookVector, &m_vRightVector); //规范化上向量 D3DXVec3Normalize(&m_vUpVector, &m_vUpVector); //右向量与上向量垂直 D3DXVec3Cross(&m_vRightVector, &m_vUpVector, &m_vLookVector); //规范化右向量 D3DXVec3Normalize(&m_vRightVector, &m_vRightVector); //创建取景变换矩阵 pMatrix->_11 = m_vRightVector.x; pMatrix->_12 = m_vUpVector.x; pMatrix->_13 = m_vLookVector.x; pMatrix->_14 = 0.0f; pMatrix->_21 = m_vRightVector.y; pMatrix->_22 = m_vUpVector.y; pMatrix->_23 = m_vLookVector.y; pMatrix->_24 = 0.0f; pMatrix->_31 = m_vRightVector.z; pMatrix->_32 = m_vUpVector.z; pMatrix->_33 = m_vLookVector.z; pMatrix->_34 = 0.0f; pMatrix->_41 = -D3DXVec3Dot(&m_vRightVector, &m_vCameraPosition); pMatrix->_42 = -D3DXVec3Dot(&m_vUpVector, &m_vCameraPosition); pMatrix->_43 = -D3DXVec3Dot(&m_vLookVector, &m_vCameraPosition); pMatrix->_44 = 1.0f; }这样,我们就可以根据摄像机类中的四个向量(相机位置,上分量,右分量,观察分量)获得取景变换的矩阵,通过指针传递参数。这样,我们实时的根据这个矩阵进行取景变换,就可以得到实时的画面情况。而我们在逻辑部分要做的就是改变摄像机类中的各个分量即可。
/************************************************************************/ /* 封装一个摄像机类 */ /************************************************************************/ #ifndef __CCAMERA_H_ #define __CCAMERA_H_ #include "stdafx.h" #pragma once class CCamera { private: //成员变量 IDirect3DDevice9* m_pDevice; //D3D设备对象 D3DXVECTOR3 m_vRightVector; //右分量向量 D3DXVECTOR3 m_vUpVector; //上分量向量 D3DXVECTOR3 m_vLookVector; //观察分量向量 D3DXVECTOR3 m_vCameraPosition; //摄像机位置 D3DXVECTOR3 m_vTargetPosition; //观察目标位置 D3DXMATRIX m_matView; //取景变换矩阵 D3DXMATRIX m_matProj; //投影变换矩阵 public: CCamera(IDirect3DDevice9* pDevice); ~CCamera(void); //计算取景变换矩阵 void CalculateViewMatrix(D3DXMATRIX *pMatrix); //获得投影矩阵 void GetProjectionMatrix(D3DXMATRIX* pMatrix){*pMatrix = m_matProj;} //获得当前摄像机位置向量 void GetCameraPosition(D3DXVECTOR3* pVector){*pVector = m_vCameraPosition;} //返回当前观察向量 void GetLookVector(D3DXVECTOR3* pVector){*pVector = m_vLookVector;} //设置摄像机观察位置向量(默认参数为NULL) void SetTargetPosition(D3DXVECTOR3* pVector = NULL); //设置摄像机所在位置向量(默认参数为NULL) void SetCameraPosition(D3DXVECTOR3* pVector = NULL); //设置取景变换矩阵(默认参数为NULL) void SetViewMatrix(D3DXMATRIX* pMatrix = NULL); //设置投影变换矩阵(默认参数为NULL) void SetProjectionMartix(D3DMATRIX* pMatrix = NULL); //沿着right分量平移 void MoveAlongRightVec(float fUnits); //沿着up分量平移 void MoveAlongUpVec(float fUnits); //沿着look分量平移 void MoveAlongLookVec(float fUnits); //沿着right分量旋转 void RotationRightVec(float fAngle); //沿着up分量旋转 void RotationUpVec(float fAngle); //沿着look分量旋转 void RotationLookVec(float fAngle); }; #endif // !__CCAMERA_H_
#include "stdafx.h" #include "Camera.h" CCamera::CCamera(IDirect3DDevice9* pDevice) { m_pDevice = pDevice; //各个向量或坐标的初始值 m_vRightVector = D3DXVECTOR3(1.0f, 0.0f, 0.0f); m_vUpVector = D3DXVECTOR3(0.0f, 1.0f, 0.0f); m_vLookVector = D3DXVECTOR3(0.0f, 0.0f, 1.0f); m_vCameraPosition = D3DXVECTOR3(0.0f, 0.0f, -200.0f); m_vTargetPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f); } CCamera::~CCamera(void) { } void CCamera::CalculateViewMatrix(D3DXMATRIX *pMatrix) { //规范化三个向量使之成为正交矩阵 //规范化观察向量 D3DXVec3Normalize(&m_vLookVector, &m_vLookVector); //使上向量与观察向量垂直 D3DXVec3Cross(&m_vUpVector, &m_vLookVector, &m_vRightVector); //规范化上向量 D3DXVec3Normalize(&m_vUpVector, &m_vUpVector); //右向量与上向量垂直 D3DXVec3Cross(&m_vRightVector, &m_vUpVector, &m_vLookVector); //规范化右向量 D3DXVec3Normalize(&m_vRightVector, &m_vRightVector); //创建取景变换矩阵 pMatrix->_11 = m_vRightVector.x; pMatrix->_12 = m_vUpVector.x; pMatrix->_13 = m_vLookVector.x; pMatrix->_14 = 0.0f; pMatrix->_21 = m_vRightVector.y; pMatrix->_22 = m_vUpVector.y; pMatrix->_23 = m_vLookVector.y; pMatrix->_24 = 0.0f; pMatrix->_31 = m_vRightVector.z; pMatrix->_32 = m_vUpVector.z; pMatrix->_33 = m_vLookVector.z; pMatrix->_34 = 0.0f; pMatrix->_41 = -D3DXVec3Dot(&m_vRightVector, &m_vCameraPosition); pMatrix->_42 = -D3DXVec3Dot(&m_vUpVector, &m_vCameraPosition); pMatrix->_43 = -D3DXVec3Dot(&m_vLookVector, &m_vCameraPosition); pMatrix->_44 = 1.0f; } void CCamera::SetTargetPosition(D3DXVECTOR3* pVector) { if (pVector) m_vTargetPosition = (*pVector); else m_vTargetPosition = D3DXVECTOR3(0.0f, 0.0f, 1.0f); //观察点位置减去摄像机位置得到坐标向量 m_vLookVector = m_vTargetPosition - m_vCameraPosition; D3DXVec3Normalize(&m_vLookVector, &m_vLookVector); //正交并规范化upV和rightV D3DXVec3Cross(&m_vUpVector, &m_vLookVector, &m_vRightVector); D3DXVec3Normalize(&m_vUpVector, &m_vUpVector); D3DXVec3Cross(&m_vRightVector, &m_vUpVector, &m_vLookVector); D3DXVec3Normalize(&m_vRightVector, &m_vRightVector); } void CCamera::SetCameraPosition(D3DXVECTOR3* pVector /* = NULL */) { D3DXVECTOR3 v = D3DXVECTOR3(0.0f, 0.0f, -250.0f); m_vCameraPosition = pVector ? (*pVector) : v; } void CCamera::SetViewMatrix(D3DXMATRIX* pMatrix /* = NULL */) { if (pMatrix) m_matView = *pMatrix; else CalculateViewMatrix(&m_matView); m_pDevice->SetTransform(D3DTS_VIEW, &m_matView); m_vRightVector = D3DXVECTOR3(m_matView._11, m_matView._12, m_matView._13); m_vUpVector = D3DXVECTOR3(m_matView._21, m_matView._22, m_matView._23); m_vLookVector = D3DXVECTOR3(m_matView._31, m_matView._32, m_matView._33); } void CCamera::SetProjectionMartix(D3DMATRIX* pMatrix /* = NULL */) { if (pMatrix) m_matProj = *pMatrix; else D3DXMatrixPerspectiveFovLH(&m_matProj, D3DX_PI / 4.0F, 800 / 600, 1.0f, 30000.0f); m_pDevice->SetTransform(D3DTS_PROJECTION, &m_matProj); } //平移操作 void CCamera::MoveAlongRightVec(float fUnits) { m_vCameraPosition += m_vRightVector * fUnits; m_vTargetPosition += m_vRightVector * fUnits; } void CCamera::MoveAlongUpVec(float fUnits) { m_vCameraPosition += m_vUpVector * fUnits; m_vTargetPosition += m_vUpVector * fUnits; } void CCamera::MoveAlongLookVec(float fUnits) { m_vCameraPosition += m_vLookVector * fUnits; m_vTargetPosition += m_vLookVector * fUnits; } //旋转操作 void CCamera::RotationRightVec(float fAngle) { //生成旋转之后的矩阵 D3DXMATRIX r; D3DXMatrixRotationAxis(&r, &m_vRightVector, fAngle); //让m_vUp向量与m_vLook向量绕m_vRight向量旋转fAngle角度 D3DXVec3TransformCoord(&m_vUpVector, &m_vUpVector, &r); D3DXVec3TransformCoord(&m_vLookVector, &m_vLookVector, &r); //更新观察点位置 m_vTargetPosition = m_vLookVector * D3DXVec3Length(&m_vCameraPosition); } void CCamera::RotationUpVec(float fAngle) { //生成旋转之后的矩阵 D3DXMATRIX r; D3DXMatrixRotationAxis(&r, &m_vUpVector, fAngle); //让m_vRight向量与m_vLook向量绕m_vUp向量旋转fAngle角度 D3DXVec3TransformCoord(&m_vRightVector, &m_vRightVector, &r); D3DXVec3TransformCoord(&m_vLookVector, &m_vLookVector, &r); //更新观察点位置 m_vTargetPosition = m_vLookVector * D3DXVec3Length(&m_vCameraPosition); } void CCamera::RotationLookVec(float fAngle) { //生成旋转之后的矩阵 D3DXMATRIX r; D3DXMatrixRotationAxis(&r, &m_vLookVector, fAngle); //让m_vUp向量与m_vRight向量绕m_vLook向量旋转fAngle角度 D3DXVec3TransformCoord(&m_vUpVector, &m_vUpVector, &r); D3DXVec3TransformCoord(&m_vRightVector, &m_vRightVector, &r); //更新观察点位置 m_vTargetPosition = m_vLookVector * D3DXVec3Length(&m_vCameraPosition); }
g_pCamera = new CCamera(g_pDevice); g_pCamera->SetCameraPosition(&D3DXVECTOR3(1.0f, 100.0f, -300.0f)); g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 0.0f, 0.0f)); g_pCamera->SetViewMatrix(); g_pCamera->SetProjectionMartix();
<span style="white-space:pre"> </span>D3DXMATRIX matView; g_pCamera->CalculateViewMatrix(&matView); g_pDevice->SetTransform(D3DTS_VIEW, &matView);
<span style="white-space:pre"> </span>// 沿摄像机各分量移动视角 if (g_pDirectInput->IsKeyDown(DIK_A)) g_pCamera->MoveAlongRightVec(-1.0f); if (g_pDirectInput->IsKeyDown(DIK_D)) g_pCamera->MoveAlongRightVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLookVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_S)) g_pCamera->MoveAlongLookVec(-1.0f); if (g_pDirectInput->IsKeyDown(DIK_I)) g_pCamera->MoveAlongUpVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_K)) g_pCamera->MoveAlongUpVec(-1.0f); //沿摄像机各分量旋转视角 if (g_pDirectInput->IsKeyDown(DIK_LEFT)) g_pCamera->RotationUpVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_RIGHT)) g_pCamera->RotationUpVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_UP)) g_pCamera->RotationRightVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_DOWN)) g_pCamera->RotationRightVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_J)) g_pCamera->RotationLookVec(-0.001f); if (g_pDirectInput->IsKeyDown(DIK_L)) g_pCamera->RotationLookVec( 0.001f);
// D3DDemo.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "D3DDemo.h" #include "DirectInput.h" #include "Camera.h" #define MAX_LOADSTRING 100 // 全局变量: HINSTANCE hInst; // 当前实例 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 // 此代码模块中包含的函数的前向声明: HWND g_hWnd; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //---------改造3D窗口需要的内容------------ LPDIRECT3D9 g_pD3D = NULL; //D3D接口指针 LPDIRECT3DDEVICE9 g_pDevice = NULL; //D3D设备指针 CDirectInput* g_pDirectInput = NULL; //控制指针 CCamera* g_pCamera = NULL; //摄像机指针 //创建网格对象所需内容 LPD3DXMESH g_pMesh = NULL; //网格对象 LPDIRECT3DTEXTURE9* g_pTextures = NULL; //网格纹理信息 D3DMATERIAL9* g_pMaterials = NULL; //网格材质信息 DWORD g_dwNumMtrls = 0; //网格材质数目 LPDIRECT3DTEXTURE9 g_pGroundTexture = NULL; //地面纹理 //------------绘制图形步骤1.定义灵活顶点格式 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)//坐标为经过变换的屏幕坐标,顶点的颜色 //------------绘制图形步骤2.根据上面定义的顶点格式,创建一个顶点的结构体 struct stVertex { float _x, _y, _z; //位置坐标 float _u, _v; //纹理坐标 stVertex(float x, float y, float z, float u, float v) : _x(x), _y(y), _z(z), _u(u), _v(v){} stVertex(){} }; //----------绘制图形步骤3.声明一个顶点缓冲区指针&一个索引缓冲区指针 LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; LPDIRECT3DINDEXBUFFER9 g_pIB = NULL; //初始化顶点缓冲区 void initVB() { //----------绘制图形步骤4.定义一个结构体数组用来给每个顶点赋值 //数组中存储当前程序中顶点的数据 stVertex vertex[4]; // 正面顶点数据 vertex[0] = stVertex(-500.0f, 0.0f, -500.0f, 0.0f, 1.0f); vertex[1] = stVertex(-500.0f, 0.0f, 500.0f, 0.0f, 0.0f); vertex[2] = stVertex( 500.0f, 0.0f, -500.0f, 1.0f, 1.0f); vertex[3] = stVertex( 500.0f, 0.0f, 500.0f, 1.0f, 0.0f); //----------绘制图形步骤5.为定点缓冲区分配内存,并将数组中的顶点值拷贝到顶点缓冲区中 //通过设备指针来创建顶点缓冲区,用来存储顶点数据 g_pDevice->CreateVertexBuffer( sizeof(vertex), //顶点缓冲区大小 D3DUSAGE_WRITEONLY, //顶点缓冲区作用 D3DFVF_CUSTOMVERTEX, //通知系统顶点格式 D3DPOOL_MANAGED, //顶点缓冲区存储位置,此处表示由系统处理 &g_pVB, //返回顶点缓冲区指针 NULL //系统保留参数,NULL ); void* pVertices = NULL; //锁定顶点缓冲区,向其中拷贝数据 g_pVB->Lock( 0, //锁定的偏移量 sizeof(vertex), //锁定的大小 &pVertices, //锁定之后存储空间 0 //锁定的标识,0 ); //将数组中的内容拷贝到缓冲区中 memcpy(pVertices, vertex, sizeof(vertex)); //解锁 g_pVB->Unlock(); //创建纹理 D3DXCreateTextureFromFile(g_pDevice, TEXT("texture.png"), &g_pGroundTexture); } void onCreatD3D() { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (!g_pD3D) return; //检测硬件设备能力的方法 /*D3DCAPS9 caps; ZeroMemory(&caps, sizeof(caps)); g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);*/ //获得相关信息,屏幕大小,像素点属性 D3DDISPLAYMODE d3ddm; ZeroMemory(&d3ddm, sizeof(d3ddm)); g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); //设置全屏模式 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); /*d3dpp.Windowed = false; d3dpp.BackBufferWidth = d3ddm.Width; d3dpp.BackBufferHeight = d3ddm.Height;*/ d3dpp.Windowed = true; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.BackBufferCount = 1; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//交换后原缓冲区数据丢弃 //是否开启自动深度模板缓冲 d3dpp.EnableAutoDepthStencil = true; //当前自动深度模板缓冲的格式 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//每个像素点有16位的存储空间,存储离摄像机的距离 g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pDevice); if (!g_pDevice) return; //设置渲染状态,设置启用深度值 g_pDevice->SetRenderState(D3DRS_ZENABLE, true); //设置渲染状态,关闭灯光 g_pDevice->SetRenderState(D3DRS_LIGHTING, false); //设置渲染状态,裁剪模式 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) ; } void CreateMesh() { LPD3DXBUFFER pAdjBuffer = NULL; LPD3DXBUFFER pMtrlBuffer = NULL; D3DXLoadMeshFromX(TEXT("miki.X"), D3DXMESH_MANAGED, g_pDevice, &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh); //读取材质和纹理数据 D3DXMATERIAL *pMtrl = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer(); g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls]; g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls]; for (int i = 0; i < g_dwNumMtrls; i++) { g_pMaterials[i] = pMtrl[i].MatD3D; g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse; g_pTextures[i] = NULL; D3DXCreateTextureFromFileA(g_pDevice, pMtrl[i].pTextureFilename, &g_pTextures[i]); } SAFE_RELEASE(pAdjBuffer); SAFE_RELEASE(pMtrlBuffer); } void CreateCamera() { g_pCamera = new CCamera(g_pDevice); g_pCamera->SetCameraPosition(&D3DXVECTOR3(1.0f, 100.0f, -300.0f)); g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 0.0f, 0.0f)); g_pCamera->SetViewMatrix(); g_pCamera->SetProjectionMartix(); } void onInit() { //初始化D3D onCreatD3D(); //初始化顶点缓冲区 initVB(); //创建Mesh模型 CreateMesh(); //创建摄像机 CreateCamera(); } void onDestroy() { if (!g_pDevice) g_pDevice->Release(); g_pDevice = NULL; } void onLogic(float fElapsedTime) { //使用DirectInput类读取数据 g_pDirectInput->GetInput(); // 沿摄像机各分量移动视角 if (g_pDirectInput->IsKeyDown(DIK_A)) g_pCamera->MoveAlongRightVec(-1.0f); if (g_pDirectInput->IsKeyDown(DIK_D)) g_pCamera->MoveAlongRightVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLookVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_S)) g_pCamera->MoveAlongLookVec(-1.0f); if (g_pDirectInput->IsKeyDown(DIK_I)) g_pCamera->MoveAlongUpVec( 1.0f); if (g_pDirectInput->IsKeyDown(DIK_K)) g_pCamera->MoveAlongUpVec(-1.0f); //沿摄像机各分量旋转视角 if (g_pDirectInput->IsKeyDown(DIK_LEFT)) g_pCamera->RotationUpVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_RIGHT)) g_pCamera->RotationUpVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_UP)) g_pCamera->RotationRightVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_DOWN)) g_pCamera->RotationRightVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_J)) g_pCamera->RotationLookVec(-0.001f); if (g_pDirectInput->IsKeyDown(DIK_L)) g_pCamera->RotationLookVec( 0.001f); //鼠标控制右向量和上向量的旋转 g_pCamera->RotationUpVec(g_pDirectInput->MouseDX()* 0.001f); g_pCamera->RotationRightVec(g_pDirectInput->MouseDY() * 0.001f); //鼠标滚轮控制观察点收缩操作 static FLOAT fPosZ=0.0f; fPosZ += g_pDirectInput->MouseDZ()*0.03f; //计算并设置取景变换矩阵 D3DXMATRIX matView; g_pCamera->CalculateViewMatrix(&matView); g_pDevice->SetTransform(D3DTS_VIEW, &matView); //把正确的世界变换矩阵存到g_matWorld中 //D3DXMatrixTranslation(&g_matWorld, 0.0f, 0.0f, fPosZ); } void Transform() { //WorldTransform:世界变换 D3DXMATRIXA16 matWorld; D3DXMATRIXA16 matScaling; //生成缩放矩阵 D3DXMatrixScaling(&matScaling, 0.8f, 0.8f, 0.8f); //生成绕Y轴旋转矩阵,存储于矩阵中 D3DXMatrixRotationY( &matWorld, //输出矩阵 10.0f //角度 ); matWorld = matScaling * matWorld; g_pDevice->SetTransform(D3DTS_WORLD, &matWorld); //ViewTransform:取景变换 D3DXVECTOR3 vEyePt(0.0f, 0.0f, -500.0f); //摄像机世界坐标 D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); //观察点世界坐标 D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); //摄像机的上向量,通常为(0.0f, 1.0f, 0.0f) D3DXMATRIXA16 matView; //View变换的矩阵 //根据上面的结果计算出矩阵,存入矩阵中 D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); //进行取景变换 g_pDevice->SetTransform(D3DTS_VIEW, &matView); //ProjectionTransform:投影变换 D3DXMATRIXA16 matProj; //投影变换矩阵 //生成投影变换矩阵,存入上面的矩阵中 D3DXMatrixPerspectiveFovLH( &matProj, //输出结果矩阵 D3DX_PI / 4, //视域角度,一般为PI/4 1.0f, //显示屏的长宽比 1.0f, //视截体中近截面距离摄像机的位置 1000.0f //视截体中远截面距离摄像机的位置 ); //进行投影变换 g_pDevice->SetTransform(D3DTS_PROJECTION, &matProj); } void onRender(float fElasedTime) { //前两个参数是0和NULL时,清空整个游戏窗口的内容(清的是后台) //第三个是清除的对象:前面表示清除颜色缓冲区,后面表示清除深度缓冲区,D3DCLEAR_STENCIL清空模板缓冲区 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,100,100), 1.0f, 0); g_pDevice->BeginScene(); //Transform(); ////----------绘制图形步骤6.设置数据源,设置灵活顶点格式,绘制图元 //设置数据流来源 g_pDevice->SetStreamSource( 0, //数据流管道号(0-15) g_pVB, //数据来源 0, //数据流偏移量 sizeof(stVertex) //每个数据的字节数大小 ); //通知系统数据格式,以便解析数据 g_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX); D3DXMATRIX matWorld; D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 200.0f); g_pDevice->SetTransform(D3DTS_WORLD, &matWorld); g_pDevice->SetTexture(0, g_pGroundTexture); //绘制图元 g_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, //三角形列 0, //起始点编号 2 //图元数量 ); D3DXMATRIX matWorld1; D3DXMatrixTranslation(&matWorld1, 0.0f, 100.0f, 200.0f); g_pDevice->SetTransform(D3DTS_WORLD, &matWorld1); for (int i = 0; i < g_dwNumMtrls; i++) { g_pDevice->SetMaterial(&g_pMaterials[i]); g_pDevice->SetTexture(0, g_pTextures[i]); g_pMesh->DrawSubset(i); } g_pDevice->EndScene(); g_pDevice->Present(NULL, NULL, NULL, NULL); } int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_D3DDEMO, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_D3DDEMO)); ZeroMemory(&msg, sizeof(msg)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { static DWORD dwTime = timeGetTime(); DWORD dwCurrentTime = timeGetTime(); DWORD dwElapsedTime = dwCurrentTime - dwTime; float fElapsedTime = dwElapsedTime * 0.001f; //------------渲染和逻辑部分代码---------- onLogic(fElapsedTime); onRender(fElapsedTime); //----------------------------------------- if (dwElapsedTime < 1000 / 60) { Sleep(1000/ 60 - dwElapsedTime); } dwTime = dwCurrentTime; } } onDestroy(); return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_D3DDEMO)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_D3DDEMO); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!g_hWnd) { return FALSE; } //初始化DirectInput类 g_pDirectInput = new CDirectInput(); g_pDirectInput->Init(g_hWnd, hInst, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); SetMenu(g_hWnd, NULL); ShowWindow(g_hWnd, nCmdShow); UpdateWindow(g_hWnd); onInit(); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND g_hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: if (wParam == VK_ESCAPE) PostQuitMessage(0); break; case WM_CLOSE: DestroyWindow(g_hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(g_hWnd, message, wParam, lParam); } return 0; }
从摄像机roll,yaw,pitch的示意图中,我们看到并不是说 roll:就是绕x轴,yaw就是绕y轴,pitch就是绕z轴。
比如一般情况下,摄像机的镜头朝向一般都被定为z轴。
那到底是怎么回事呢?
我目测要从roll,yaw,pitch,这三个词的含义来看。
roll: vt. 卷;滚动,转动;辗
yaw: v.(火箭、飞机、宇宙飞船等)偏航
pitch:vi. 倾斜;投掷;搭帐篷;坠落
为了不再将这三个词和x,y,z牵扯起来,我们只使用描述性的语言。
roll的意思是翻滚,中文中飞机的翻滚是什么,就是绕着机身所在的那个轴。
yaw:是偏航的意思,如果要改变航向,飞机必定是绕着重力方向为轴。
pitch:有倾斜、坠落的意思。飞机在坠落时,必定会一头栽下去,以翅膀所在的直线为轴。
现在把摄像机看成一个飞机,镜头朝向就是飞机头的朝向,是不是一样?