Don't create new projection matrix each frame
This commit is contained in:
parent
c08d1e8e5e
commit
04039b4e0c
|
@ -241,12 +241,10 @@ void Application::run()
|
||||||
|
|
||||||
glm::vec3 cameraPos = m_camera->getPosition();
|
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));
|
glm::mat4 model = glm::scale(glm::translate(glm::mat4(1.0f), cameraPos), glm::vec3(50.0f, 50.0f, 50.0f));
|
||||||
skyboxShader.use();
|
skyboxShader.use();
|
||||||
skyboxShader.setUniform("projectionMatrix", projection);
|
|
||||||
skyboxShader.setUniform("viewMatrix", m_camera->getViewMatrix());
|
|
||||||
skyboxShader.setUniform("modelMatrix", model);
|
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
|
glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles -> 6 squares
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,8 @@ class Application : public Singleton<Application>
|
||||||
void initialize();
|
void initialize();
|
||||||
void exit() { m_run = false; }
|
void exit() { m_run = false; }
|
||||||
|
|
||||||
|
inline glm::vec2 getScreenDimensions() const { return glm::vec2(m_width, m_height); }
|
||||||
|
|
||||||
friend class Singleton<Application>;
|
friend class Singleton<Application>;
|
||||||
private:
|
private:
|
||||||
int m_width, m_height;
|
int m_width, m_height;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
|
||||||
#include "util/Log.h"
|
#include "util/Log.h"
|
||||||
|
#include "Application.h"
|
||||||
|
|
||||||
// TODO: use Roll
|
// TODO: use Roll
|
||||||
|
|
||||||
|
@ -32,6 +33,7 @@ Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw, GLfloat pitch, GLf
|
||||||
m_pitch = pitch;
|
m_pitch = pitch;
|
||||||
m_roll = roll;
|
m_roll = roll;
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
|
updateProjection();
|
||||||
}
|
}
|
||||||
|
|
||||||
Camera::Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch, GLfloat roll) :
|
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_pitch = pitch;
|
||||||
m_roll = roll;
|
m_roll = roll;
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
|
updateProjection();
|
||||||
}
|
}
|
||||||
|
|
||||||
Camera::~Camera()
|
Camera::~Camera()
|
||||||
|
@ -58,6 +61,12 @@ glm::mat4 Camera::getViewMatrix()
|
||||||
return glm::lookAt(m_position, m_position + m_front, m_up);
|
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)
|
void Camera::processKeyboard(Camera_Movement direction, GLfloat deltaTime)
|
||||||
{
|
{
|
||||||
GLfloat velocity = m_movementSpeed * deltaTime;
|
GLfloat velocity = m_movementSpeed * deltaTime;
|
||||||
|
@ -114,9 +123,18 @@ void Camera::processMouseScroll(GLfloat yoffset)
|
||||||
{
|
{
|
||||||
m_zoom = 45.0f;
|
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
|
// Calculate the new Front vector
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
|
|
15
src/Camera.h
15
src/Camera.h
|
@ -18,6 +18,7 @@
|
||||||
#define __CAMERA_H__
|
#define __CAMERA_H__
|
||||||
|
|
||||||
#include "util/Common.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
|
// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
|
||||||
enum Camera_Movement {
|
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(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch, GLfloat roll);
|
||||||
~Camera();
|
~Camera();
|
||||||
|
|
||||||
glm::mat4 getViewMatrix();
|
glm::mat4 getViewMatrix(void);
|
||||||
glm::vec3 getPosition() const { return m_position; }
|
|
||||||
void processKeyboard(Camera_Movement direction, GLfloat deltaTime);
|
void processKeyboard(Camera_Movement direction, GLfloat deltaTime);
|
||||||
void processMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch);
|
void processMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch);
|
||||||
void processMouseScroll(GLfloat yoffset);
|
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:
|
private:
|
||||||
glm::vec3 m_position;
|
glm::vec3 m_position;
|
||||||
glm::vec3 m_front;
|
glm::vec3 m_front;
|
||||||
|
@ -66,6 +69,10 @@ private:
|
||||||
GLfloat m_mouseSensitivity;
|
GLfloat m_mouseSensitivity;
|
||||||
GLfloat m_zoom;
|
GLfloat m_zoom;
|
||||||
|
|
||||||
void updateCameraVectors();
|
// The Projection Matrix
|
||||||
|
glm::mat4 m_projection;
|
||||||
|
|
||||||
|
void updateCameraVectors(void);
|
||||||
|
void updateProjection(void);
|
||||||
};
|
};
|
||||||
#endif // __CAMERA_H__
|
#endif // __CAMERA_H__
|
||||||
|
|
Loading…
Reference in New Issue