Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "World/Simple/SimpleCamera.h"
- #include "Math/Mathf.h"
- #include <3rd Party\imgui\imgui.h>
- SimpleCamera::SimpleCamera()
- {
- SetFrustrum(60, 1.0, 1.0, 1000);
- }
- SimpleCamera::~SimpleCamera()
- {
- }
- Vector3 SimpleCamera::GetPosition() const
- {
- return m_Transform.Position();
- }
- void SimpleCamera::SetPosition(const Vector3& v)
- {
- m_Transform.SetPosition(v);
- m_ViewDirty = true;
- }
- Vector3 SimpleCamera::GetRight() const
- {
- return m_Transform.Right();
- }
- Vector3 SimpleCamera::GetUp() const
- {
- return m_Transform.Up();
- }
- Vector3 SimpleCamera::GetForward() const
- {
- return m_Transform.Forward();
- }
- float SimpleCamera::GetNearZ() const
- {
- return m_ZNear;
- }
- float SimpleCamera::GetFarZ() const
- {
- return m_ZFar;
- }
- float SimpleCamera::GetAspect() const
- {
- return m_Aspect;
- }
- float SimpleCamera::GetFovX() const
- {
- return 2.0f * Mathf::Atan((0.5f * GetNearWindowWidth()) / m_ZNear);
- }
- float SimpleCamera::GetFoxY() const
- {
- return m_FovY;
- }
- float SimpleCamera::GetNearWindowWidth() const
- {
- return m_Aspect * GetNearWindowHeight();
- }
- float SimpleCamera::GetNearWindowHeight() const
- {
- return m_NearFrustrumHeight;
- }
- float SimpleCamera::GetFarWindowWidth() const
- {
- return m_Aspect * GetNearWindowHeight();
- }
- float SimpleCamera::GetFarWindowHeight() const
- {
- return m_FarFrustrumHeight;
- }
- float SimpleCamera::GetFocalLength() const
- {
- return Mathf::Abs(1.0f / Mathf::Tan(m_FovY * 0.5f));
- }
- void SimpleCamera::SetFrustrum(float fov, float aspect, float zNear, float zFar)
- {
- m_FovY = fov * Mathf::DEG_TO_RAD;
- m_Aspect = aspect;
- m_ZNear = zNear;
- m_ZFar = zFar;
- m_NearFrustrumHeight = 2.0f * m_ZNear * Mathf::Tan(0.5f * m_FovY);
- m_FarFrustrumHeight = 2.0f * m_ZFar * Mathf::Tan(0.5f * m_FovY);
- m_Proj = Matrix4::Perspective(m_FovY, m_Aspect, m_ZNear, m_ZFar);
- }
- void SimpleCamera::SetFOV(float fovY)
- {
- SetFrustrum(fovY, m_Aspect, m_ZNear, m_ZFar);
- }
- Matrix4 SimpleCamera::GetView() const
- {
- return m_View;
- }
- Matrix4 SimpleCamera::GetProj() const
- {
- return m_Proj;
- }
- Matrix4 SimpleCamera::GetViewProject() const
- {
- return m_View * m_Proj;
- }
- void SimpleCamera::Move(const Vector3& translate)
- {
- if (Mathf::IsZero(translate.SqrMagnitude())) {return;}
- m_Transform.Translate(translate);
- m_ViewDirty = true;
- }
- void SimpleCamera::Rotation(const Vector3& euler)
- {
- m_Transform.Rotate(euler);
- m_ViewDirty = true;
- }
- void SimpleCamera::Rotate(float yaw, float pitch)
- {
- // Rule, pitch locally, yaw globally, prevents converion of yaw,pitch into roll
- // Rotation is hack to fix the roll drift :/
- m_Transform.Rotate(Vector3(pitch, 0, 0), Space::Self);
- m_Transform.Rotate(Vector3(0, yaw, 0), Space::World);
- m_ViewDirty = true;
- }
- void SimpleCamera::LookAt(Vector3 target)
- {
- Transform transform;
- transform.SetPosition(target);
- m_Transform.LookAt(transform, Vector3::Up);
- m_ViewDirty = true;
- }
- void SimpleCamera::Update()
- {
- if (m_ViewDirty)
- {
- m_View = Matrix4::LookAt(m_Transform.Position(), m_Transform.Position() + m_Transform.Forward(), m_Transform.Up());
- }
- m_ViewDirty = false;
- }
- void SimpleCamera::OnGui()
- {
- if (ImGui::CollapsingHeader("Camera: "))
- {
- m_Transform.OnGui();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement