camera.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #include "camera.h"
  2. CCamera::CCamera(D3DXVECTOR3 pos, float rotx, float roty, float rotz, float fov, float aspect, float nearz, float farz)
  3. {
  4. this->position = pos;
  5. this->fov = fov;
  6. this->aspect = aspect;
  7. this->znear = nearz;
  8. this->zfar = farz;
  9. this->rotx = rotx;
  10. this->roty = roty;
  11. this->rotz = rotz;
  12. ZeroMemory(&keys, sizeof(keys));
  13. this->Update();
  14. }
  15. CCamera::CCamera(const CCamera *src)
  16. {
  17. this->position = src->position;
  18. this->fov = src->fov;
  19. this->aspect = src->aspect;
  20. this->znear = src->znear;
  21. this->zfar = src->zfar;
  22. this->forward = src->forward;
  23. this->right = src->right;
  24. this->up = src->up;
  25. //this->rotx = src->rotx;
  26. //this->roty = src->roty;
  27. //this->rotz = src->rotz;
  28. ZeroMemory(&keys, sizeof(keys));
  29. this->UpdateLookAt();
  30. }
  31. CCamera::~CCamera()
  32. {
  33. }
  34. void CCamera::Update(){
  35. D3DXMATRIXA16 rotatex,rotatey,rotatez,translation;
  36. // 投影矩阵
  37. D3DXMatrixPerspectiveFovLH(&proj, fov, aspect, znear, zfar);
  38. // 观察矩阵
  39. D3DXMatrixRotationX(&rotatex,rotx);
  40. D3DXMatrixRotationY(&rotatey,roty);
  41. D3DXMatrixRotationZ(&rotatez,rotz);
  42. D3DXMatrixTranslation(&translation,-position.x,-position.y,-position.z);
  43. view = translation * rotatey*rotatex*rotatez;
  44. // 投影观察矩阵
  45. viewproj = view*proj;
  46. // 求逆
  47. D3DXMatrixInverse(&invproj,NULL,&proj);
  48. D3DXMatrixInverse(&invview,NULL,&view);
  49. D3DXMatrixInverse(&invviewproj,NULL,&viewproj);
  50. // 方向向量
  51. D3DXVec3TransformNormal(&forward,&D3DXVECTOR3(0,0,1),&invview);
  52. D3DXVec3TransformNormal(&up,&D3DXVECTOR3(0,1,0),&invview);
  53. D3DXVec3TransformNormal(&right,&D3DXVECTOR3(1,0,0),&invview);
  54. }
  55. void CCamera::UpdateLookAt()
  56. {
  57. // 投影矩阵
  58. D3DXMatrixPerspectiveFovLH(&proj, fov, aspect, znear, zfar);
  59. // 观察矩阵
  60. D3DXMatrixLookAtLH( &view, &position,&(position+forward), &D3DXVECTOR3(0,1,0));
  61. // 投影观察矩阵
  62. viewproj = view*proj;
  63. // 求逆
  64. D3DXMatrixInverse(&invproj,NULL,&proj);
  65. D3DXMatrixInverse(&invview,NULL,&view);
  66. D3DXMatrixInverse(&invviewproj,NULL,&viewproj);
  67. D3DXVec3TransformNormal(&right,&D3DXVECTOR3(1,0,0),&invview);
  68. }
  69. void CCamera::GetKey(int k)
  70. {
  71. keys[k]=true;
  72. }
  73. void CCamera::LostKey(int k)
  74. {
  75. keys[k]=false;
  76. }
  77. void CCamera::UpdataKey()
  78. {
  79. float speed;
  80. if( GetAsyncKeyState(VK_LCONTROL) != 0)
  81. {
  82. speed = 0.03f;
  83. else if( GetAsyncKeyState(VK_LSHIFT) != 0)
  84. {
  85. speed = 5.0f;
  86. else
  87. {
  88. speed = 0.3f;
  89. }
  90. if(keys['A'])
  91. position -= speed*right;
  92. if(keys['D'])
  93. position += speed*right;
  94. if(keys['W'])
  95. position += speed*forward;
  96. if(keys['S'])
  97. position -= speed*forward;
  98. if(keys['Q'])
  99. position += speed*up;
  100. if(keys['Z'])
  101. position -= speed*up;
  102. if(position.y < 6.0f)
  103. position.y = 6.0f;
  104. if(position.y > 300.0f)
  105. position.y = 300.0f;
  106. Update();
  107. }
  108. void CCamera::UpdataMouse(dxmouse *pM)
  109. {
  110. if(pM->mousedown(MOUSE_LEFT))
  111. {
  112. roty -= 0.005 * pM->x;
  113. rotx -= 0.005 * pM->y;
  114. Update();
  115. }
  116. void CCamera::UpdateBoat(dxmouse *pM, CMeshObj *pMesh)
  117. {
  118. D3DXVECTOR3 v = D3DXVECTOR3(pMesh->world._11,pMesh->world._12,pMesh->world._13);
  119. static float fi = 0.99;
  120. position.x = position.x*fi+ (1-fi)*(pMesh->position.x + 10 * v.x);
  121. position.z = position.z*fi+ (1-fi)*(pMesh->position.z + 10 * v.z);
  122. position.y = position.y * 0.9 + 0.1 * 20;
  123. if(pM->mousedown(MOUSE_LEFT))
  124. {
  125. rotx -= 0.005 * pM->y;
  126. roty -= pMesh->fYaw;
  127. Update();
  128. fi = fi * 0.99 + 0.7 * 0.01;
  129. }