|
本帖最后由 小生怕怕啊 于 2015-6-17 11:11 编辑
这次为引擎增加了一个3D摄像机类,便于场景的游览。
本次利用摄像机类实现了简单的场景漫游自动导航。
下面是摄像机类的具体细节!
- #include "Camera.h"
- //计算取景矩阵
- void CCamera::CalcViewMatrix( D3DXMATRIX* pMatrix )
- {
- //规格化向量
- D3DXVec3Normalize(&m_vLook,&m_vLook); //规范化观察向量
- D3DXVec3Cross(&m_vUp,&m_vLook,&m_vRight); //上向量垂直观察向量
- D3DXVec3Normalize(&m_vUp,&m_vUp); //规范化上向量
- D3DXVec3Cross(&m_vRight,&m_vUp,&m_vLook); //右向量垂直上向量
- D3DXVec3Normalize(&m_vRight,&m_vRight); //规范化右向量
- //取景变换矩阵
- pMatrix->_11=m_vRight.x;
- pMatrix->_12=m_vUp.x;
- pMatrix->_13=m_vLook.x;
- pMatrix->_14=0.0f;
- pMatrix->_21=m_vRight.y;
- pMatrix->_22=m_vUp.y;
- pMatrix->_23=m_vLook.y;
- pMatrix->_24=0.0f;
- pMatrix->_31=m_vRight.z;
- pMatrix->_32=m_vUp.z;
- pMatrix->_33=m_vLook.z;
- pMatrix->_34=0.0f;
- pMatrix->_41=-D3DXVec3Dot(&m_vRight,&m_vCameraPosition);
- pMatrix->_42=-D3DXVec3Dot(&m_vUp,&m_vCameraPosition);
- pMatrix->_43=-D3DXVec3Dot(&m_vLook,&m_vCameraPosition);
- pMatrix->_44=1.0f;
- }
- //构造函数
- CCamera::CCamera( LPDIRECT3DDEVICE9 pDev )
- {
- m_Dev=pDev;
- m_vRight=D3DXVECTOR3(1.0f,0.0f,0.0f);
- m_vUp=D3DXVECTOR3(0.0f,1.0f,0.0f);
- m_vLook=D3DXVECTOR3(0.0f,0.0f,1.0f);
- m_vCameraPosition=D3DXVECTOR3(0.0f,0.0f,-300.0f);
- m_vLookPosition=D3DXVECTOR3(0.0f,0.0f,0.0f);
- }
- //设置观察位置
- void CCamera::SetLookPosition( D3DXVECTOR3* pVector/*=NULL*/ )
- {
- if (pVector!=NULL)
- {
- m_vLookPosition=*pVector;
- }
- else
- {
- m_vLookPosition=D3DXVECTOR3(0.0f,0.0f,1.0f);
- }
- m_vLook=m_vLookPosition-m_vCameraPosition;
- D3DXVec3Normalize(&m_vLook,&m_vLook);
- D3DXVec3Cross(&m_vUp,&m_vLook,&m_vRight);
- D3DXVec3Normalize(&m_vUp,&m_vUp);
- D3DXVec3Cross(&m_vRight,&m_vUp,&m_vLook);
- D3DXVec3Normalize(&m_vRight,&m_vRight);
- }
- //设置摄像机位置
- void CCamera::SetCameraPosition( D3DXVECTOR3* pVector/*=NULL*/ )
- {
- D3DXVECTOR3 camera=D3DXVECTOR3(0.0f,0.0f,-300.0f);
- m_vCameraPosition=pVector?(*pVector):camera;
- }
- void CCamera::SetViewMatrix( D3DXMATRIX* pMatrix/*=NULL*/ )
- {
- if(pMatrix) m_MatrixView=*pMatrix;
- else CalcViewMatrix(&m_MatrixView);
- m_Dev->SetTransform(D3DTS_VIEW,&m_MatrixView);
- m_vRight=D3DXVECTOR3(m_MatrixView._11,m_MatrixView._12,m_MatrixView._13);
- m_vUp=D3DXVECTOR3(m_MatrixView._21,m_MatrixView._22,m_MatrixView._23);
- m_vLook=D3DXVECTOR3(m_MatrixView._31,m_MatrixView._32,m_MatrixView._33);
- }
- void CCamera::SetProjMatrix( int nWidth,int nHeight,D3DXMATRIX* pMatrix/*=NULL*/ )
- {
- if(pMatrix!=NULL) m_Projection=*pMatrix;
- else D3DXMatrixPerspectiveFovLH(&m_Projection,45.0f,(float)nWidth/nHeight,0.1f,3000.0f);
- m_Dev->SetTransform(D3DTS_PROJECTION,&m_Projection);
- }
- //沿右向量移动
- void CCamera::MoveAlongRight( float amount )
- {
- m_vCameraPosition+=m_vRight*amount;
- m_vLookPosition+=m_vRight*amount;
- }
- //沿上向量移动
- void CCamera::MoveAlongUp( float amount )
- {
- m_vCameraPosition+=m_vUp*amount;
- m_vLookPosition+=m_vUp*amount;
- }
- //沿观察向量移动
- void CCamera::MoveAlongLook( float amount )
- {
- m_vCameraPosition+=m_vLook*amount;
- m_vLookPosition+=m_vLook*amount;
- }
- //绕右向量旋转
- void CCamera::RotationRieght( float angle )
- {
- D3DXMATRIX Rotation;
- D3DXMatrixRotationAxis(&Rotation,&m_vRight,angle); //创建绕右向量旋转angel度的矩阵
- D3DXVec3TransformCoord(&m_vUp,&m_vUp,&Rotation); //让上向量绕右向量旋转angle个角度
- D3DXVec3TransformCoord(&m_vLook,&m_vLook,&Rotation); //让观察向量绕右向量旋转angle个角度
- m_vLookPosition=m_vLook*D3DXVec3Length(&m_vCameraPosition);
- }
- //绕上向量旋转
- void CCamera::RotationUp( float angle )
- {
- D3DXMATRIX Rotation;
- D3DXMatrixRotationAxis(&Rotation,&m_vUp,angle); //创建绕上向量旋转angel度的矩阵
- D3DXVec3TransformCoord(&m_vRight,&m_vRight,&Rotation); //让右向量绕上向量旋转angle个角度
- D3DXVec3TransformCoord(&m_vLook,&m_vLook,&Rotation); //让观察向量绕上向量旋转angle个角度
- m_vLookPosition=m_vLook*D3DXVec3Length(&m_vCameraPosition);
- }
- //绕观察向量旋转
- void CCamera::RotationLook( float angle )
- {
- D3DXMATRIX Rotation;
- D3DXMatrixRotationAxis(&Rotation,&m_vLook,angle); //创建绕观察向量旋转angel度的矩阵
- D3DXVec3TransformCoord(&m_vUp,&m_vUp,&Rotation); //让上向量绕观察向量旋转angle个角度
- D3DXVec3TransformCoord(&m_vRight,&m_vRight,&Rotation); //让右向量绕观察向量旋转angle个角度
- m_vLookPosition=m_vLook*D3DXVec3Length(&m_vCameraPosition);
- }
- CCamera::~CCamera()
- {
- }
- void CCamera::GetCameraPosition( D3DXVECTOR3* pVector )
- {
- *pVector=m_vCameraPosition;
- }
复制代码
- <P> </P>
- <DIV class=blockcode>
- <BLOCKQUOTE>#include <windows.h>
- #include "Engine.h"
- #define WIN_CLASS "dx01"//窗体类名
- #define WIN_NAME "3D摄像机自动导航" //窗体标题
- #define WIN_WIDTH 800 //窗体宽度
- #define WIN_HEIGHT 600 //窗体高度
- #define WIN_FULLSCREEN false //是否全屏
- Engine engine; //引擎对象
- Model pModel; //模型
- LPDIRECT3DDEVICE9 dev=NULL;
- CCamera* g_pCamera=NULL; //摄像机
- //游戏初始化
- bool GameInit(HWND hwnd);
- //游戏循环
- void GameLoop();
- //更新
- void GameUpdate();
- //游戏结束
- void GameEnd();
- //Windows消息回调函数
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch(msg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_KEYUP:
- if (wparam==VK_ESCAPE)
- {
- PostQuitMessage(0);
- }
- break;
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }
- //Window入口函数
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
- {
- //设置窗口结构类属性
- WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,hInstance,NULL,NULL,NULL,NULL,WIN_CLASS,NULL};
- //注册窗口类型
- RegisterClassEx(&wc);
- //创建窗口
- HWND hwnd= CreateWindow(WIN_CLASS,WIN_NAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hInstance,NULL);
- if (hwnd)
- {
- ShowWindow(hwnd,nShowCmd); //显示窗口
- UpdateWindow(hwnd); //更新窗口
- if (GameInit(hwnd))
- {
- MSG msg;
- ZeroMemory(&msg,sizeof(msg));
- //窗口消息循环
- while (msg.message!=WM_QUIT)
- {
- if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
- {
- TranslateMessage(&msg); //转换消息
- DispatchMessage(&msg); //分派消息
- }
- else
- {
- GameLoop();
- }
- }
- GameEnd();
- }
- }
- UnregisterClass(WIN_CLASS,wc.hInstance);
- return 0;
- }
- //Game循环
- void GameLoop()
- {
- engine.ClearScreen();
- GameUpdate();
- engine.StartRender(); //开始渲染
- engine.DrawModel(&pModel);
- engine.EndRender(); //结束渲染
- }
- //Game开始
- bool GameInit(HWND hwnd)
- {
- if (!engine.Init(hwnd,WIN_WIDTH,WIN_HEIGHT))
- {
- return false;
- }
- if (!engine.LoadModel("edificio1.X",&pModel))
- {
- ShowMessage("载入模型失败!");
- return false;
- }
- dev= engine.GetDev();
- dev->SetRenderState(D3DRS_LIGHTING,false); //关闭光照
- dev->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); //背面消隐
- g_pCamera=new CCamera(dev);
- g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f,50.0f,-300.0f)); //设置初始摄像机位置
- g_pCamera->SetLookPosition(&D3DXVECTOR3(0.0f,50.0f,0.0f)); //设置初始观察点位置
- g_pCamera->SetViewMatrix(); //初始化取景变换矩阵
- g_pCamera->SetProjMatrix(WIN_WIDTH,WIN_HEIGHT); //初始化投影变换矩阵
- return true;
- }
- //GameOver
- void GameEnd()
- {
- //模型释放
- pModel.Release();
- //关闭引擎
- engine.Shutdown();
- SAFE_DELETE(g_pCamera);
- }
- float angle=0.0f;
- UINT uStep=1;
- float RotationSpeed=0.005f;
- float ForwardSpeed=1.0f;
- void GameUpdate()
- {
- D3DXVECTOR3 vPosition;
- g_pCamera->GetCameraPosition(&vPosition); //获取当前摄像机位置
- switch(uStep)
- {
- //1.前进
- case 1:
- if (vPosition.z<35.0f)
- {
- uStep=1;
- g_pCamera->MoveAlongLook(1.0f);
- }
- else uStep=2;
- break;
- //2.旋转
- case 2:
- if (angle<1.5f)
- {
- g_pCamera->RotationUp(RotationSpeed);
- angle+=RotationSpeed;
- }else
- {
- uStep=3;
- angle=0.0f;
- }
- break;
- //3.进入房间
- case 3:
- if (vPosition.z<45.0f)
- {
- g_pCamera->MoveAlongLook(1.0f);
- }
- else uStep=4;
- break;
- //4.左旋转
- case 4:
- if (angle<1.5f)
- {
- g_pCamera->RotationUp(-RotationSpeed);
- angle+=RotationSpeed;
- }else uStep=5;
- break;
- //5.前进
- case 5:
- if (vPosition.z<80)
- {
- g_pCamera->MoveAlongLook(ForwardSpeed);
- }else uStep=6;
- break;
- }
- D3DXMATRIX MatrixView;
- g_pCamera->CalcViewMatrix(&MatrixView);
- dev->SetTransform(D3DTS_VIEW,&MatrixView);
- }
复制代码
运行截图:
同步博客:http://blog.csdn.net/qq578023708/article/details/46521273
交流群:87341183
|
|