From 04039b4e0c9e2a78888ca14a65aebf888b527cd7 Mon Sep 17 00:00:00 2001 From: Evert Date: Sun, 15 Apr 2018 15:54:33 +0300 Subject: [PATCH] Don't create new projection matrix each frame --- src/Application.cpp | 4 +--- src/Application.h | 2 ++ src/Camera.cpp | 20 +++++++++++++++++++- src/Camera.h | 15 +++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index d6a5203..102ec4b 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -241,12 +241,10 @@ void Application::run() glm::vec3 cameraPos = m_camera->getPosition(); - glm::mat4 projection = glm::perspective(m_camera->getFOV(), (GLfloat)m_width/(GLfloat)m_height, 0.1f, 100.0f); glm::mat4 model = glm::scale(glm::translate(glm::mat4(1.0f), cameraPos), glm::vec3(50.0f, 50.0f, 50.0f)); skyboxShader.use(); - skyboxShader.setUniform("projectionMatrix", projection); - skyboxShader.setUniform("viewMatrix", m_camera->getViewMatrix()); skyboxShader.setUniform("modelMatrix", model); + m_camera->shaderViewProjection(skyboxShader); // Load view and projection matrices glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles -> 6 squares diff --git a/src/Application.h b/src/Application.h index 8a4ed67..2acfde3 100644 --- a/src/Application.h +++ b/src/Application.h @@ -30,6 +30,8 @@ class Application : public Singleton void initialize(); void exit() { m_run = false; } + inline glm::vec2 getScreenDimensions() const { return glm::vec2(m_width, m_height); } + friend class Singleton; private: int m_width, m_height; diff --git a/src/Camera.cpp b/src/Camera.cpp index 302aa5e..2ee6987 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -17,6 +17,7 @@ #include "Camera.h" #include "util/Log.h" +#include "Application.h" // TODO: use Roll @@ -32,6 +33,7 @@ Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw, GLfloat pitch, GLf m_pitch = pitch; m_roll = roll; updateCameraVectors(); + updateProjection(); } Camera::Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch, GLfloat roll) : @@ -46,6 +48,7 @@ Camera::Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat up m_pitch = pitch; m_roll = roll; updateCameraVectors(); + updateProjection(); } Camera::~Camera() @@ -58,6 +61,12 @@ glm::mat4 Camera::getViewMatrix() return glm::lookAt(m_position, m_position + m_front, m_up); } +void Camera::shaderViewProjection(Shader& shader) +{ + shader.setUniform("viewMatrix", getViewMatrix()); + shader.setUniform("projectionMatrix", m_projection); +} + void Camera::processKeyboard(Camera_Movement direction, GLfloat deltaTime) { GLfloat velocity = m_movementSpeed * deltaTime; @@ -114,9 +123,18 @@ void Camera::processMouseScroll(GLfloat yoffset) { m_zoom = 45.0f; } + + updateProjection(); } -void Camera::updateCameraVectors() +void Camera::updateProjection(void) +{ + // Recalculate the projection matrix + glm::vec2 screenDims = Application::getInstance().getScreenDimensions(); + m_projection = glm::perspective(getFOV(), (GLfloat)screenDims.x/(GLfloat)screenDims.y, 0.1f, 100.0f); +} + +void Camera::updateCameraVectors(void) { // Calculate the new Front vector glm::vec3 front; diff --git a/src/Camera.h b/src/Camera.h index e83ba01..579a283 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -18,6 +18,7 @@ #define __CAMERA_H__ #include "util/Common.h" +#include "Shader.h" // Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods enum Camera_Movement { @@ -42,13 +43,15 @@ public: Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch, GLfloat roll); ~Camera(); - glm::mat4 getViewMatrix(); - glm::vec3 getPosition() const { return m_position; } + glm::mat4 getViewMatrix(void); void processKeyboard(Camera_Movement direction, GLfloat deltaTime); void processMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch); void processMouseScroll(GLfloat yoffset); - GLfloat getFOV() const { return m_zoom; } + void shaderViewProjection(Shader& shader); + + inline GLfloat getFOV() const { return m_zoom; } + inline glm::vec3 getPosition(void) const { return m_position; } private: glm::vec3 m_position; glm::vec3 m_front; @@ -66,6 +69,10 @@ private: GLfloat m_mouseSensitivity; GLfloat m_zoom; - void updateCameraVectors(); + // The Projection Matrix + glm::mat4 m_projection; + + void updateCameraVectors(void); + void updateProjection(void); }; #endif // __CAMERA_H__