Don't create new projection matrix each frame

This commit is contained in:
Evert Prants 2018-04-15 15:54:33 +03:00
parent c08d1e8e5e
commit 04039b4e0c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 33 additions and 8 deletions

View File

@ -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

View File

@ -30,6 +30,8 @@ class Application : public Singleton<Application>
void initialize();
void exit() { m_run = false; }
inline glm::vec2 getScreenDimensions() const { return glm::vec2(m_width, m_height); }
friend class Singleton<Application>;
private:
int m_width, m_height;

View File

@ -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;

View File

@ -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__