Advertisement
Guest User

SimpleCamera.cpp

a guest
Apr 8th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include "World/Simple/SimpleCamera.h"
  2. #include "Math/Mathf.h"
  3. #include <3rd Party\imgui\imgui.h>
  4.  
  5. SimpleCamera::SimpleCamera()
  6. {
  7.     SetFrustrum(60, 1.0, 1.0, 1000);
  8. }
  9.  
  10. SimpleCamera::~SimpleCamera()
  11. {
  12. }
  13.  
  14. Vector3 SimpleCamera::GetPosition() const
  15. {
  16.     return m_Transform.Position();
  17. }
  18.  
  19. void SimpleCamera::SetPosition(const Vector3& v)
  20. {
  21.     m_Transform.SetPosition(v);
  22.     m_ViewDirty = true;
  23. }
  24.  
  25. Vector3 SimpleCamera::GetRight() const
  26. {
  27.     return m_Transform.Right();
  28. }
  29.  
  30. Vector3 SimpleCamera::GetUp() const
  31. {
  32.     return m_Transform.Up();
  33. }
  34.  
  35. Vector3 SimpleCamera::GetForward() const
  36. {
  37.     return m_Transform.Forward();
  38. }
  39.  
  40. float SimpleCamera::GetNearZ() const
  41. {
  42.     return m_ZNear;
  43. }
  44.  
  45. float SimpleCamera::GetFarZ() const
  46. {
  47.     return m_ZFar;
  48. }
  49.  
  50. float SimpleCamera::GetAspect() const
  51. {
  52.     return m_Aspect;
  53. }
  54.  
  55. float SimpleCamera::GetFovX() const
  56. {
  57.     return 2.0f * Mathf::Atan((0.5f * GetNearWindowWidth()) / m_ZNear);
  58. }
  59.  
  60. float SimpleCamera::GetFoxY() const
  61. {
  62.     return m_FovY;
  63. }
  64.  
  65. float SimpleCamera::GetNearWindowWidth() const
  66. {
  67.     return m_Aspect * GetNearWindowHeight();
  68. }
  69.  
  70. float SimpleCamera::GetNearWindowHeight() const
  71. {
  72.     return m_NearFrustrumHeight;
  73. }
  74.  
  75. float SimpleCamera::GetFarWindowWidth() const
  76. {
  77.     return m_Aspect * GetNearWindowHeight();
  78. }
  79.  
  80. float SimpleCamera::GetFarWindowHeight() const
  81. {
  82.     return m_FarFrustrumHeight;
  83. }
  84.  
  85. float SimpleCamera::GetFocalLength() const
  86. {
  87.     return Mathf::Abs(1.0f / Mathf::Tan(m_FovY * 0.5f));
  88. }
  89.  
  90. void SimpleCamera::SetFrustrum(float fov, float aspect, float zNear, float zFar)
  91. {
  92.     m_FovY      = fov * Mathf::DEG_TO_RAD;
  93.     m_Aspect    = aspect;
  94.     m_ZNear     = zNear;
  95.     m_ZFar      = zFar;
  96.  
  97.     m_NearFrustrumHeight = 2.0f * m_ZNear * Mathf::Tan(0.5f * m_FovY);
  98.     m_FarFrustrumHeight  = 2.0f * m_ZFar * Mathf::Tan(0.5f * m_FovY);
  99.  
  100.     m_Proj = Matrix4::Perspective(m_FovY, m_Aspect, m_ZNear, m_ZFar);
  101. }
  102.  
  103. void SimpleCamera::SetFOV(float fovY)
  104. {
  105.     SetFrustrum(fovY, m_Aspect, m_ZNear, m_ZFar);
  106. }
  107.  
  108. Matrix4 SimpleCamera::GetView() const
  109. {
  110.     return m_View;
  111. }
  112.  
  113. Matrix4 SimpleCamera::GetProj() const
  114. {
  115.     return m_Proj;
  116. }
  117.  
  118. Matrix4 SimpleCamera::GetViewProject() const
  119. {
  120.     return m_View * m_Proj;
  121. }
  122.  
  123. void SimpleCamera::Move(const Vector3& translate)
  124. {
  125.     if (Mathf::IsZero(translate.SqrMagnitude())) {return;}
  126.     m_Transform.Translate(translate);
  127.     m_ViewDirty = true;
  128. }
  129.  
  130. void SimpleCamera::Rotation(const Vector3& euler)
  131. {
  132.     m_Transform.Rotate(euler);
  133.     m_ViewDirty = true;
  134. }
  135.  
  136. void SimpleCamera::Rotate(float yaw, float pitch)
  137. {
  138.     // Rule, pitch locally, yaw globally, prevents converion of yaw,pitch into roll
  139.     // Rotation is  hack to fix the roll drift :/
  140.     m_Transform.Rotate(Vector3(pitch, 0, 0), Space::Self); 
  141.     m_Transform.Rotate(Vector3(0, yaw, 0), Space::World);
  142.     m_ViewDirty = true;
  143. }
  144.  
  145. void SimpleCamera::LookAt(Vector3 target)
  146. {
  147.     Transform transform;
  148.     transform.SetPosition(target);
  149.  
  150.     m_Transform.LookAt(transform, Vector3::Up);
  151.     m_ViewDirty = true;
  152. }
  153.  
  154. void SimpleCamera::Update()
  155. {
  156.     if (m_ViewDirty)
  157.     {
  158.         m_View = Matrix4::LookAt(m_Transform.Position(), m_Transform.Position() + m_Transform.Forward(), m_Transform.Up());
  159.     }
  160.  
  161.     m_ViewDirty = false;
  162. }
  163.  
  164. void SimpleCamera::OnGui()
  165. {
  166.     if (ImGui::CollapsingHeader("Camera: "))
  167.     {
  168.         m_Transform.OnGui();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement