Guest User

Untitled

a guest
Sep 2nd, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include "FpsCamera.h"
  2.  
  3. namespace Minecraft
  4. {
  5. FPSCamera::FPSCamera(float fov, float aspect, float zNear, float zFar, float sensitivity)
  6. {
  7. m_Fov = fov;
  8. m_Aspect = aspect;
  9. m_zNear = zNear;
  10. m_zFar = zFar;
  11.  
  12. m_Rotation = 0.0f;
  13. m_Position = glm::vec3(0.0f, 0.0f, 0.0f);
  14. m_Front = glm::vec3(0.0f, 0.0f, -1.0f);
  15. m_Up = glm::vec3(0.0f, 1.0f, 0.0f);
  16. m_Acceleration = glm::vec3(0.0f);
  17. m_Velocity = glm::vec3(0.0f);
  18.  
  19. _Pitch = 0.0f;
  20. _Yaw = 0.0f;
  21. _Sensitivity = sensitivity;
  22.  
  23. m_ViewMatrix = glm::lookAt(m_Position, m_Front + m_Position, m_Up);
  24. m_ProjectionMatrix = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
  25. m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
  26. }
  27.  
  28. FPSCamera::~FPSCamera()
  29. {
  30. // Nothing to do here
  31. }
  32.  
  33. void FPSCamera::UpdateOnMouseMovement(double xpos, double ypos)
  34. {
  35. ypos = -ypos;
  36.  
  37. float x_diff = xpos - _PrevMx;
  38. float y_diff = ypos - _PrevMy;
  39.  
  40. if (_FirstMove == false)
  41. {
  42. _FirstMove = true;
  43. _PrevMx = xpos;
  44. _PrevMy = ypos;
  45. }
  46.  
  47. x_diff = x_diff * _Sensitivity;
  48. y_diff = y_diff * _Sensitivity;
  49.  
  50. _PrevMx = xpos;
  51. _PrevMy = ypos;
  52.  
  53. _Yaw = _Yaw + x_diff;
  54. _Pitch = _Pitch + y_diff;
  55.  
  56. if (_Pitch > 89.0f)
  57. {
  58. _Pitch = 89.0f;
  59. }
  60.  
  61. if (_Pitch < -89.0f)
  62. {
  63. _Pitch = -89.0f;
  64. }
  65.  
  66. glm::vec3 front;
  67.  
  68. front.x = cos(glm::radians(_Pitch)) * cos(glm::radians(_Yaw));
  69. front.y = sin(glm::radians(_Pitch));
  70. front.z = cos(glm::radians(_Pitch)) * sin(glm::radians(_Yaw));
  71.  
  72. this->SetFront(front);
  73. }
  74.  
  75. void FPSCamera::SetPosition(const glm::vec3& position)
  76. {
  77. m_Position = position;
  78.  
  79. RecalculateViewMatrix();
  80. }
  81.  
  82. void FPSCamera::ChangePosition(const glm::vec3& position_increment)
  83. {
  84. m_Position = m_Position + position_increment;
  85.  
  86. RecalculateViewMatrix();
  87. }
  88.  
  89. void FPSCamera::SetFront(const glm::vec3& front)
  90. {
  91. m_Front = front;
  92. RecalculateViewMatrix();
  93. }
  94.  
  95. void FPSCamera::SetRotation(float angle)
  96. {
  97. m_Rotation = angle;
  98.  
  99. RecalculateViewMatrix();
  100. }
  101.  
  102. void FPSCamera::SetFov(float fov)
  103. {
  104. m_Fov = fov;
  105.  
  106. RecalculateProjectionMatrix();
  107. }
  108.  
  109. void FPSCamera::SetAspect(float aspect)
  110. {
  111. m_Aspect = aspect;
  112.  
  113. RecalculateProjectionMatrix();
  114. }
  115.  
  116. void FPSCamera::SetNearAndFarPlane(float zNear, float zFar)
  117. {
  118. m_zNear = zNear;
  119. m_zFar = zFar;
  120.  
  121. RecalculateProjectionMatrix();
  122. }
  123.  
  124. void FPSCamera::SetPerspectiveMatrix(float fov, float aspect_ratio, float zNear, float zFar)
  125. {
  126. m_Fov = fov;
  127. m_Aspect = aspect_ratio;
  128. m_zNear = zNear;
  129. m_zFar = zFar;
  130.  
  131. RecalculateProjectionMatrix();
  132. }
  133.  
  134. void FPSCamera::OnUpdate()
  135. {
  136. constexpr float drag = 0.90f;
  137.  
  138. m_Velocity += m_Acceleration;
  139. m_Velocity *= drag;
  140.  
  141. if (glm::length(m_Velocity) < 0.001)
  142. {
  143. m_Velocity = glm::vec3(0.0f);
  144. }
  145.  
  146. m_Position += m_Velocity;
  147. RecalculateViewMatrix();
  148. }
  149.  
  150. void FPSCamera::RecalculateViewMatrix()
  151. {
  152. m_ViewMatrix = glm::lookAt(m_Position, m_Front + m_Position, m_Up);
  153. m_ViewMatrix = glm::rotate(m_ViewMatrix, glm::radians(m_Rotation), glm::vec3(1.0f, 0.5f, 0.5f));
  154.  
  155. m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
  156. }
  157.  
  158. void FPSCamera::RecalculateProjectionMatrix()
  159. {
  160. m_ProjectionMatrix = glm::perspective(m_Fov, m_Aspect, m_zNear, m_zFar);
  161.  
  162. m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
  163. }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment