camera.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
- #include "camera.h"
- CCamera::CCamera(D3DXVECTOR3 pos, float rotx, float roty, float rotz, float fov, float aspect, float nearz, float farz)
- {
- this->position = pos;
- this->fov = fov;
- this->aspect = aspect;
- this->znear = nearz;
- this->zfar = farz;
- this->rotx = rotx;
- this->roty = roty;
- this->rotz = rotz;
- ZeroMemory(&keys, sizeof(keys));
- this->Update();
- }
- CCamera::CCamera(const CCamera *src)
- {
- this->position = src->position;
- this->fov = src->fov;
- this->aspect = src->aspect;
- this->znear = src->znear;
- this->zfar = src->zfar;
- this->forward = src->forward;
- this->right = src->right;
- this->up = src->up;
- //this->rotx = src->rotx;
- //this->roty = src->roty;
- //this->rotz = src->rotz;
- ZeroMemory(&keys, sizeof(keys));
- this->UpdateLookAt();
- }
- CCamera::~CCamera()
- {
- }
- void CCamera::Update(){
- D3DXMATRIXA16 rotatex,rotatey,rotatez,translation;
- // 投影矩阵
- D3DXMatrixPerspectiveFovLH(&proj, fov, aspect, znear, zfar);
- // 观察矩阵
- D3DXMatrixRotationX(&rotatex,rotx);
- D3DXMatrixRotationY(&rotatey,roty);
- D3DXMatrixRotationZ(&rotatez,rotz);
- D3DXMatrixTranslation(&translation,-position.x,-position.y,-position.z);
- view = translation * rotatey*rotatex*rotatez;
- // 投影观察矩阵
- viewproj = view*proj;
- // 求逆
- D3DXMatrixInverse(&invproj,NULL,&proj);
- D3DXMatrixInverse(&invview,NULL,&view);
- D3DXMatrixInverse(&invviewproj,NULL,&viewproj);
- // 方向向量
- D3DXVec3TransformNormal(&forward,&D3DXVECTOR3(0,0,1),&invview);
- D3DXVec3TransformNormal(&up,&D3DXVECTOR3(0,1,0),&invview);
- D3DXVec3TransformNormal(&right,&D3DXVECTOR3(1,0,0),&invview);
- }
- void CCamera::UpdateLookAt()
- {
- // 投影矩阵
- D3DXMatrixPerspectiveFovLH(&proj, fov, aspect, znear, zfar);
- // 观察矩阵
- D3DXMatrixLookAtLH( &view, &position,&(position+forward), &D3DXVECTOR3(0,1,0));
- // 投影观察矩阵
- viewproj = view*proj;
- // 求逆
- D3DXMatrixInverse(&invproj,NULL,&proj);
- D3DXMatrixInverse(&invview,NULL,&view);
- D3DXMatrixInverse(&invviewproj,NULL,&viewproj);
- D3DXVec3TransformNormal(&right,&D3DXVECTOR3(1,0,0),&invview);
- }
- void CCamera::GetKey(int k)
- {
- keys[k]=true;
- }
- void CCamera::LostKey(int k)
- {
- keys[k]=false;
- }
- void CCamera::UpdataKey()
- {
- float speed;
- if( GetAsyncKeyState(VK_LCONTROL) != 0)
- {
- speed = 0.03f;
- }
- else if( GetAsyncKeyState(VK_LSHIFT) != 0)
- {
- speed = 5.0f;
- }
- else
- {
- speed = 0.3f;
- }
- if(keys['A'])
- position -= speed*right;
- if(keys['D'])
- position += speed*right;
- if(keys['W'])
- position += speed*forward;
- if(keys['S'])
- position -= speed*forward;
- if(keys['Q'])
- position += speed*up;
- if(keys['Z'])
- position -= speed*up;
- if(position.y < 6.0f)
- position.y = 6.0f;
- if(position.y > 300.0f)
- position.y = 300.0f;
- Update();
- }
- void CCamera::UpdataMouse(dxmouse *pM)
- {
- if(pM->mousedown(MOUSE_LEFT))
- {
- roty -= 0.005 * pM->x;
- rotx -= 0.005 * pM->y;
- Update();
- }
- }
- void CCamera::UpdateBoat(dxmouse *pM, CMeshObj *pMesh)
- {
- D3DXVECTOR3 v = D3DXVECTOR3(pMesh->world._11,pMesh->world._12,pMesh->world._13);
-
- static float fi = 0.99;
-
- position.x = position.x*fi+ (1-fi)*(pMesh->position.x + 10 * v.x);
- position.z = position.z*fi+ (1-fi)*(pMesh->position.z + 10 * v.z);
- position.y = position.y * 0.9 + 0.1 * 20;
- if(pM->mousedown(MOUSE_LEFT))
- {
- rotx -= 0.005 * pM->y;
-
- }
- roty -= pMesh->fYaw;
- Update();
- fi = fi * 0.99 + 0.7 * 0.01;
- }