CAMERA.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 4k
Category:

OpenCV

Development Platform:

Visual C++

  1. #include "camera.h"
  2. #include "object.h"
  3. CCamera::CCamera()
  4. {
  5. position = CVector(0.0, 0.0, 0.0);
  6. lookAt = CVector(0.0, 0.0, 1.0);
  7. forward = lookAt;
  8. up = CVector(0.0, 1.0, 0.0);
  9. right = CVector(1.0, 0.0, 0.0);
  10. velocity = CVector(0.0, 0.0, 0.0);
  11. acceleration = CVector(0.0, 0.0, 0.0);
  12. yaw = 0.0;
  13. pitch = 0.0;
  14. }
  15. CCamera::CCamera(CVector *look)
  16. {
  17. position = CVector(0.0, 0.0, 0.0);
  18. lookAt = look->UnitVector();
  19. forward = lookAt;
  20. up = CVector(0.0, 1.0, 0.0);
  21. right = forward.CrossProduct(up); //CVector(1.0, 0.0, 0.0);
  22. velocity = CVector(0.0, 0.0, 0.0);
  23. acceleration = CVector(0.0, 0.0, 0.0);
  24. yaw = 0.0;
  25. pitch = 0.0;
  26. }
  27. CCamera::CCamera(CVector *pos, CVector *look)
  28. {
  29. position = *pos;
  30. lookAt = look->UnitVector();
  31. forward = lookAt;
  32. up = CVector(0.0, 1.0, 0.0);
  33. right = CVector(1.0, 0.0, 0.0);
  34. velocity = CVector(0.0, 0.0, 0.0);
  35. acceleration = CVector(0.0, 0.0, 0.0);
  36. yaw = 0.0;
  37. pitch = 0.0;
  38. }
  39. CCamera::~CCamera()
  40. {
  41. }
  42. void CCamera::UpdateLookAt()
  43. {
  44. CVector look = CVector(finalLookAt.x - lookAt.x,
  45.    finalLookAt.y - lookAt.y,
  46.    finalLookAt.z - lookAt.z);
  47. lookAtVel = look * 0.5;
  48. }
  49. void CCamera::UpdateMoveTo()
  50. {
  51. CVector pos = CVector(finalPosition.x - position.x,
  52.   finalPosition.y - position.y,
  53.   finalPosition.z - position.z);
  54. velocity = pos * 0.5;
  55. }
  56. void CCamera::RotatePitch(scalar_t radians)
  57. {
  58. float sine = sinf(radians);
  59. float cosine = cosf(radians);
  60. up.y = cosine * up.Length();
  61. up.z = sine * up.Length();
  62. forward.y = -sine * forward.Length();
  63. forward.z = cosine * forward.Length();
  64. }
  65. void CCamera::RotateYaw(scalar_t radians)
  66. {
  67. float sine = sinf(radians);
  68. float cosine = cosf(radians);
  69. right.x = cosine * right.Length();
  70. right.z = sine * right.Length();
  71. forward.x = -sine * forward.Length();
  72. forward.z = cosine * forward.Length();
  73. }
  74. void CCamera::RotateRoll(scalar_t radians)
  75. {
  76. float sine = sinf(radians);
  77. float cosine = cosf(radians);
  78. right.x = cosine * right.Length();
  79. right.y = sine * right.Length();
  80. up.x = -sine * forward.Length();
  81. up.y = cosine * forward.Length();
  82. }
  83. void CCamera::LookAtNow(CObject *object)
  84. {
  85. lookAt = object->position;
  86. }
  87. void CCamera::MoveToNow(scalar_t x, scalar_t y, scalar_t z)
  88. {
  89. position.x = x;
  90. position.y = y;
  91. position.z = z;
  92. }
  93. void CCamera::MoveToNow(CObject *object)
  94. {
  95. position = object->position;
  96. }
  97. void CCamera::LookAt(CObject *object)
  98. {
  99. CVector v = CVector(object->position - lookAt);
  100. initLookAt = lookAt;
  101. finalLookAt = object->position;
  102. lookAtAccel = -lookAt * 0.25f;
  103. UpdateLookAt();
  104. }
  105. void CCamera::MoveTo(CObject *object)
  106. {
  107. CVector v = CVector(object->position - position);
  108. initPosition = position;
  109. finalPosition = object->position;
  110. acceleration = -position * 0.25f;
  111. UpdateMoveTo();
  112. }
  113. void CCamera::Animate(scalar_t deltaTime)
  114. {
  115.      if ((yaw >= 360.0f) || (yaw <= -360.0f))
  116.           yaw = 0.0f;
  117.      if (pitch > 60.0f)
  118.           pitch = 60.0f;
  119.      if (pitch < -60.0f)
  120.           pitch = -60.0f;
  121.      float cosYaw = (scalar_t)cos(DEG2RAD(yaw));
  122.      float sinYaw = (scalar_t)sin(DEG2RAD(yaw));
  123.      float sinPitch = (scalar_t)sin(DEG2RAD(pitch));
  124.      float speed = velocity.z * deltaTime;
  125.      float strafeSpeed = velocity.x * deltaTime;
  126.      if (speed > 15.0)
  127.           speed = 15.0;
  128.      if (strafeSpeed > 15.0)
  129.           strafeSpeed = 15.0;
  130.      if (speed < -15.0)
  131.           speed = -15.0;
  132.      if (strafeSpeed < -15.0)
  133.           strafeSpeed = -15.0;
  134.      if (velocity.Length() > 0.0)
  135.           acceleration = -velocity * 1.5f;
  136.      velocity += acceleration*deltaTime;
  137.      position.x += float(cos(DEG2RAD(yaw + 90.0)))*strafeSpeed;
  138.      position.z += float(sin(DEG2RAD(yaw + 90.0)))*strafeSpeed;
  139.      position.x += float(cosYaw)*speed;
  140.      position.z += float(sinYaw)*speed;
  141.      lookAt.x = float(position.x + cosYaw);
  142.      lookAt.y = float(position.y + sinPitch);
  143.      lookAt.z = float(position.z + sinYaw);
  144.      gluLookAt(position.x, position.y, position.z,
  145.                lookAt.x, lookAt.y, lookAt.z,
  146.                0.0, 1.0, 0.0);
  147. }