Camera::WorldToScreen helper function

This commit is contained in:
2016-01-21 10:29:36 +01:00
parent 8854794cbc
commit 4aa9100c69
2 changed files with 16 additions and 1 deletions
+3 -1
View File
@@ -2,6 +2,7 @@
#define Camera_h__
#include "../GLM.h"
#include "../Core/Util/Rectangle.h"
class Camera
{
@@ -32,7 +33,6 @@ public:
glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
void SetViewMatrix(glm::mat4 val);
float AspectRatio() const { return m_AspectRatio; }
void SetAspectRatio(float val);
@@ -48,6 +48,8 @@ public:
void UpdateViewMatrix();
void UpdateProjectionMatrix();
glm::vec2 WorldToScreen(glm::vec3 worldCoord, Rectangle resolution);
private:
glm::vec3 m_Position;
+13
View File
@@ -79,6 +79,19 @@ void Camera::UpdateProjectionMatrix()
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()
{
m_ViewMatrix = glm::toMat4(glm::inverse(m_Orientation))