Camera::WorldToScreen helper function
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#define Camera_h__
|
#define Camera_h__
|
||||||
|
|
||||||
#include "../GLM.h"
|
#include "../GLM.h"
|
||||||
|
#include "../Core/Util/Rectangle.h"
|
||||||
|
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
@@ -32,7 +33,6 @@ public:
|
|||||||
glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
|
glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
|
||||||
void SetViewMatrix(glm::mat4 val);
|
void SetViewMatrix(glm::mat4 val);
|
||||||
|
|
||||||
|
|
||||||
float AspectRatio() const { return m_AspectRatio; }
|
float AspectRatio() const { return m_AspectRatio; }
|
||||||
void SetAspectRatio(float val);
|
void SetAspectRatio(float val);
|
||||||
|
|
||||||
@@ -48,6 +48,8 @@ public:
|
|||||||
void UpdateViewMatrix();
|
void UpdateViewMatrix();
|
||||||
void UpdateProjectionMatrix();
|
void UpdateProjectionMatrix();
|
||||||
|
|
||||||
|
glm::vec2 WorldToScreen(glm::vec3 worldCoord, Rectangle resolution);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
glm::vec3 m_Position;
|
glm::vec3 m_Position;
|
||||||
|
|||||||
@@ -79,6 +79,19 @@ void Camera::UpdateProjectionMatrix()
|
|||||||
m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip);
|
m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec2 Camera::WorldToScreen(glm::vec3 worldCoord, Rectangle resolution)
|
||||||
|
{
|
||||||
|
glm::vec4 screenCoord = m_ProjectionMatrix * m_ViewMatrix * glm::vec4(worldCoord, 1.f);
|
||||||
|
if (screenCoord.w != 0) {
|
||||||
|
screenCoord.x /= screenCoord.w;
|
||||||
|
screenCoord.y /= screenCoord.w;
|
||||||
|
screenCoord.z /= screenCoord.w;
|
||||||
|
}
|
||||||
|
screenCoord.x = screenCoord.x * (resolution.Width / 2.f);
|
||||||
|
screenCoord.y = screenCoord.y * (resolution.Height / 2.f);
|
||||||
|
return glm::vec2(screenCoord);
|
||||||
|
}
|
||||||
|
|
||||||
void Camera::UpdateViewMatrix()
|
void Camera::UpdateViewMatrix()
|
||||||
{
|
{
|
||||||
m_ViewMatrix = glm::toMat4(glm::inverse(m_Orientation))
|
m_ViewMatrix = glm::toMat4(glm::inverse(m_Orientation))
|
||||||
|
|||||||
Reference in New Issue
Block a user