5#include "glm/gtc/matrix_transform.hpp"
26 m_aspectRatio = aspectRatio;
28 m_canBeMainCamera = overrideCurrent;
33 UpdateProjectionMatrix();
34 UpdateCameraVectors();
36 if (m_canBeMainCamera)
52 UpdateCameraVectors();
57 if (m_aspectRatio != aspectRatio)
59 m_aspectRatio = aspectRatio;
60 UpdateProjectionMatrix();
66 return m_projectionMatrix * GenViewMatrix();
71 return glm::lookAt(gameObject->GetPosition(), gameObject->GetPosition() + m_front, m_up);
79 UpdateProjectionMatrix();
85 if (m_near != nearPlane)
88 UpdateProjectionMatrix();
94 if (m_far != farPlane)
97 UpdateProjectionMatrix();
101glm::mat4 Elevate::Camera::GenProjectionMatrix()
103 return glm::perspective(glm::radians(m_FOV), m_aspectRatio, m_near, m_far);
106void Elevate::Camera::UpdateProjectionMatrix()
108 m_projectionMatrix = GenProjectionMatrix();
113 float pitch = glm::radians(gameObject->GetRotation().x);
114 float yaw = glm::radians(gameObject->GetRotation().y);
117 front.x = cos(yaw) * cos(pitch);
118 front.y = sin(pitch);
119 front.z = sin(yaw) * cos(pitch);
120 m_front = glm::normalize(front);
122 m_right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), m_front));
124 m_up = glm::normalize(glm::cross(m_front, m_right));
128#ifdef EE_EDITOR_BUILD
133 UpdateCameraVectors();
137void Elevate::Camera::DrawDebugFrustum()
139 auto corners = CalculateFrustumCorners();
141 glm::vec4 color = glm::vec4(0.96f, 0.47f, 0.12f, 1.0f);
144 for (
int i = 0; i < 4; i++)
162std::array<glm::vec3, 8> Elevate::Camera::CalculateFrustumCorners(
float visualFarScale)
164 std::array<glm::vec3, 8> corners;
166 glm::vec3 position = gameObject->GetGlobalPosition();
167 glm::vec3 front = GetFrontVec();
168 glm::vec3 right = GetRightVec();
169 glm::vec3 up = GetUpVec();
171 float tanHalfFOV = tan(glm::radians(m_FOV * 0.5f));
173 float visualFar = m_far * visualFarScale;
174 float nearHeight = 2.0f * tanHalfFOV * m_near;
175 float nearWidth = nearHeight * m_aspectRatio;
177 float farHeight = 2.0f * tanHalfFOV * visualFar;
178 float farWidth = farHeight * m_aspectRatio;
180 glm::vec3 nearCenter = position + front * m_near;
181 glm::vec3 farCenter = position + front * visualFar;
184 corners[0] = nearCenter - (up * (nearHeight * 0.5f)) - (right * (nearWidth * 0.5f));
185 corners[1] = nearCenter - (up * (nearHeight * 0.5f)) + (right * (nearWidth * 0.5f));
186 corners[2] = nearCenter + (up * (nearHeight * 0.5f)) + (right * (nearWidth * 0.5f));
187 corners[3] = nearCenter + (up * (nearHeight * 0.5f)) - (right * (nearWidth * 0.5f));
190 corners[4] = farCenter - (up * (farHeight * 0.5f)) - (right * (farWidth * 0.5f));
191 corners[5] = farCenter - (up * (farHeight * 0.5f)) + (right * (farWidth * 0.5f));
192 corners[6] = farCenter + (up * (farHeight * 0.5f)) + (right * (farWidth * 0.5f));
193 corners[7] = farCenter + (up * (farHeight * 0.5f)) - (right * (farWidth * 0.5f));
static Application & Get()
static void NotifyDestruction(Camera *camera)
static void SetCurrent(Camera *current)
Camera(float fov, float aspectRatio, bool overrideCurrent=true)
void UpdateCameraVectors()
void OnSetRotation() override
void SetNear(float nearPlane)
const void UpdateAspectRatio(float aspectRatio)
void SetFar(float farPlane)
glm::mat4 GenViewMatrix() const
glm::mat4 GenViewProjectionMatrix() const
void RenderWhenSelected() override
static void AddDebugLine(DebugLineData line)
static void SetDefaultListener(GameObject *obj)
virtual unsigned int GetWidth() const =0
virtual unsigned int GetHeight() const =0