You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.4 KiB

5 years ago
  1. #include "camera.h"
  2. Camera::Camera() : mPos(0.0f,0.0f,0.0f),mViewCenter(0.0f,0.0f,-1.0f),mUp(0.0f,1.0f,0.0f)
  3. {
  4. mbMoveLeft = false;
  5. mbMoveRight = false;
  6. mbMoveForward = false;
  7. mbMoveBack = false;
  8. }
  9. void Camera::Update(float deltaTime)
  10. {
  11. float moveSpeed = 10.0f;
  12. //ǰ��������
  13. Vector3f forwardDirection = mViewCenter - mPos;
  14. forwardDirection.Normalize();
  15. //�ҷ�������
  16. Vector3f rightDirection = Cross(forwardDirection, mUp);
  17. rightDirection.Normalize();
  18. if (mbMoveLeft) {
  19. Vector3f delta = rightDirection * deltaTime * moveSpeed;
  20. mPos = mPos - delta;
  21. mViewCenter = mViewCenter - delta;
  22. }
  23. if (mbMoveRight) {
  24. Vector3f delta = rightDirection * deltaTime * moveSpeed;
  25. mPos = mPos + delta;
  26. mViewCenter = mViewCenter + delta;
  27. }
  28. if (mbMoveForward) {
  29. Vector3f delta = forwardDirection * deltaTime * moveSpeed;
  30. mPos = mPos + delta;
  31. mViewCenter = mViewCenter + delta;
  32. }
  33. if (mbMoveBack) {
  34. Vector3f delta = forwardDirection * deltaTime * moveSpeed;
  35. mPos = mPos - delta;
  36. mViewCenter = mViewCenter - delta;
  37. }
  38. glLoadIdentity();
  39. gluLookAt(
  40. mPos.x, mPos.y, mPos.z,
  41. mViewCenter.x, mViewCenter.y, mViewCenter.z,
  42. mUp.x, mUp.y, mUp.z
  43. );
  44. }
  45. void Camera::Picth(float angle)
  46. {
  47. Vector3f viewDirection = mViewCenter - mPos;
  48. viewDirection.Normalize();
  49. Vector3f rightDirection = Cross(viewDirection, mUp);
  50. rightDirection.Normalize();
  51. RotateView(angle, rightDirection.x, rightDirection.y, rightDirection.z);
  52. }
  53. void Camera::Yaw(float angle)
  54. {
  55. RotateView(angle, mUp.x, mUp.y, mUp.z);
  56. }
  57. void Camera::RotateView(float angle, float x, float y, float z)
  58. {
  59. Vector3f viewDirection = mViewCenter - mPos;
  60. Vector3f newDirection(0.0f, 0.0f, 0.0f);
  61. float C = cosf(angle);
  62. float S = sinf(angle);
  63. Vector3f tempX(C + x*x*(1-C), x*y*(1-C)-z*S,x*z*(1-C)+y*S);
  64. newDirection.x = tempX*viewDirection;
  65. Vector3f tempY(x*y*(1-C)+z*S, C+y*y*(1-C),y*z*(1-C)-x*S);
  66. newDirection.y = tempY * viewDirection;
  67. Vector3f tempZ(x*z*(1-C)-y*S, y*z*(1-C)+x*S, C+z*z*(1-C));
  68. newDirection.z = tempZ* viewDirection;
  69. mViewCenter = mPos + newDirection;
  70. }
  71. void Camera::SwitchTo3D()
  72. {
  73. glMatrixMode(GL_PROJECTION);
  74. glLoadIdentity();
  75. gluPerspective(50.0f, (float)mViewportWidth / (float)mViewportHeight, 0.1f, 1000.0f);
  76. glMatrixMode(GL_MODELVIEW);
  77. glLoadIdentity();
  78. }
  79. void Camera::SwitchTo2D()
  80. {
  81. glMatrixMode(GL_PROJECTION);
  82. glLoadIdentity();
  83. gluOrtho2D(-mViewportWidth / 2, mViewportWidth / 2, -mViewportHeight / 2, mViewportHeight / 2);
  84. glMatrixMode();
  85. glLoadIdentity();
  86. }