Add zoom level to orthographic camera
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include <memory> // std::make_shared
|
#include <algorithm> // std::max, std::min
|
||||||
|
#include <memory> // std::make_shared
|
||||||
|
|
||||||
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective, glm::ortho
|
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective, glm::ortho
|
||||||
#include <glm/ext/matrix_transform.hpp> // glm::radians, glm::lookAt
|
#include <glm/ext/matrix_transform.hpp> // glm::radians, glm::lookAt
|
||||||
@@ -39,8 +40,10 @@ namespace Inferno {
|
|||||||
|
|
||||||
void OrthographicCamera::initialize()
|
void OrthographicCamera::initialize()
|
||||||
{
|
{
|
||||||
|
m_zoomLevel = 1.0f;
|
||||||
|
|
||||||
// Set the roatation axis
|
// Set the roatation axis
|
||||||
m_rotate = glm::vec3(0.0f, 0.0f, 1.0f);
|
m_rotateAxis = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
dbg(Log::Info) << "OrthographicCamera initialized";
|
dbg(Log::Info) << "OrthographicCamera initialized";
|
||||||
}
|
}
|
||||||
@@ -91,18 +94,31 @@ namespace Inferno {
|
|||||||
|
|
||||||
transform()->setTranslate(translate);
|
transform()->setTranslate(translate);
|
||||||
|
|
||||||
|
// Update camera zoom
|
||||||
|
|
||||||
|
float zoomSpeed = ZOOM_SENSITIVITY * deltaTime;
|
||||||
|
|
||||||
|
if (Input::isKeyPressed(KeyCode("GLFW_KEY_EQUAL"))) {
|
||||||
|
m_zoomLevel -= zoomSpeed;
|
||||||
|
}
|
||||||
|
if (Input::isKeyPressed(KeyCode("GLFW_KEY_MINUS"))) {
|
||||||
|
m_zoomLevel += zoomSpeed;
|
||||||
|
}
|
||||||
|
m_zoomLevel = std::max(m_zoomLevel, 0.25f);
|
||||||
|
m_zoomLevel = std::min(m_zoomLevel, 10.0f);
|
||||||
|
|
||||||
// Update camera matrix
|
// Update camera matrix
|
||||||
|
|
||||||
// Local space -> World space: model matrix
|
// Local space -> World space: model matrix
|
||||||
// Is done in Object::update()
|
// Is done in Object::update()
|
||||||
|
|
||||||
// World space -> View space: view matrix
|
// World space -> View space: view matrix
|
||||||
transform()->setTransform(glm::translate(glm::mat4(1.0f), translate) * glm::rotate(glm::mat4(1.0f), glm::radians(rotate.z), m_rotate));
|
transform()->setTransform(glm::translate(glm::mat4(1.0f), translate) * glm::rotate(glm::mat4(1.0f), glm::radians(rotate.z), m_rotateAxis));
|
||||||
transform()->setTransform(glm::inverse(transform()->transform()));
|
transform()->setTransform(glm::inverse(transform()->transform()));
|
||||||
|
|
||||||
// View space -> Clip space: projection matrix
|
// View space -> Clip space: projection matrix
|
||||||
float aspectRatio = Application::get().getWindow().getAspect();
|
float aspectRatio = Application::get().getWindow().getAspect();
|
||||||
setProjection(glm::ortho(-aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f));
|
setProjection(glm::ortho(-aspectRatio * m_zoomLevel, aspectRatio * m_zoomLevel, -m_zoomLevel, m_zoomLevel, -1.0f, 1.0f));
|
||||||
|
|
||||||
// Clip space -> Screen space: viewport transform
|
// Clip space -> Screen space: viewport transform
|
||||||
// Is done in the fragment shader using the settings of glViewport
|
// Is done in the fragment shader using the settings of glViewport
|
||||||
@@ -137,8 +153,8 @@ namespace Inferno {
|
|||||||
void PerspectiveCamera::update(float deltaTime)
|
void PerspectiveCamera::update(float deltaTime)
|
||||||
{
|
{
|
||||||
// Get mouse movement offset compared to last frame
|
// Get mouse movement offset compared to last frame
|
||||||
float xOffset = Input::getXOffset() * SENSITIVITY;
|
float xOffset = Input::getXOffset() * MOUSE_SENSITIVITY;
|
||||||
float yOffset = Input::getYOffset() * SENSITIVITY;
|
float yOffset = Input::getYOffset() * MOUSE_SENSITIVITY;
|
||||||
m_yaw += xOffset;
|
m_yaw += xOffset;
|
||||||
m_pitch += yOffset;
|
m_pitch += yOffset;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#ifndef CAMERA_H
|
#ifndef CAMERA_H
|
||||||
#define CAMERA_H
|
#define CAMERA_H
|
||||||
|
|
||||||
#include "inferno/log.h" // @@@@@@@@@@
|
|
||||||
#define TRANSLATE_SPEED 2.5f
|
#define TRANSLATE_SPEED 2.5f
|
||||||
#define ROTATE_SPEED 90.0f
|
#define ROTATE_SPEED 90.0f
|
||||||
#define SENSITIVITY 0.25f
|
#define ZOOM_SENSITIVITY 2.5f
|
||||||
|
#define MOUSE_SENSITIVITY 0.25f
|
||||||
#define NEAR_PLANE 0.1f
|
#define NEAR_PLANE 0.1f
|
||||||
#define FAR_PLANE 100.0f
|
#define FAR_PLANE 100.0f
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ namespace Inferno {
|
|||||||
public:
|
public:
|
||||||
Camera();
|
Camera();
|
||||||
Camera(glm::vec3 translate, glm::vec3 rotate);
|
Camera(glm::vec3 translate, glm::vec3 rotate);
|
||||||
virtual ~Camera() { dbg(Log::Danger) << "KILLED CAMERA"; }
|
virtual ~Camera() {}
|
||||||
|
|
||||||
virtual void initialize() = 0;
|
virtual void initialize() = 0;
|
||||||
virtual void update(float deltaTime) = 0;
|
virtual void update(float deltaTime) = 0;
|
||||||
@@ -44,7 +44,7 @@ namespace Inferno {
|
|||||||
public:
|
public:
|
||||||
OrthographicCamera();
|
OrthographicCamera();
|
||||||
OrthographicCamera(glm::vec3 translate, glm::vec3 rotate);
|
OrthographicCamera(glm::vec3 translate, glm::vec3 rotate);
|
||||||
virtual ~OrthographicCamera() { dbg(Log::Danger) << "KILLED ORTHOGRAPHICCAMERA"; }
|
virtual ~OrthographicCamera() {}
|
||||||
|
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
virtual void update(float deltaTime) override;
|
virtual void update(float deltaTime) override;
|
||||||
@@ -52,7 +52,8 @@ namespace Inferno {
|
|||||||
// virtual void destroy() override;
|
// virtual void destroy() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
glm::vec3 m_rotate;
|
float m_zoomLevel;
|
||||||
|
glm::vec3 m_rotateAxis;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
@@ -61,7 +62,7 @@ namespace Inferno {
|
|||||||
public:
|
public:
|
||||||
PerspectiveCamera();
|
PerspectiveCamera();
|
||||||
PerspectiveCamera(glm::vec3 translate, glm::vec3 rotate);
|
PerspectiveCamera(glm::vec3 translate, glm::vec3 rotate);
|
||||||
virtual ~PerspectiveCamera() { dbg(Log::Danger) << "KILLED PERSPECTIVECAMERA"; }
|
virtual ~PerspectiveCamera() {}
|
||||||
|
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
virtual void update(float deltaTime) override;
|
virtual void update(float deltaTime) override;
|
||||||
@@ -83,5 +84,4 @@ namespace Inferno {
|
|||||||
|
|
||||||
// @Todo:
|
// @Todo:
|
||||||
// - Add sensitivity, fov to settings:camera
|
// - Add sensitivity, fov to settings:camera
|
||||||
// - Add zoom to ortho camera
|
|
||||||
// - Change ortho view matrix to use glm::lookAt()
|
// - Change ortho view matrix to use glm::lookAt()
|
||||||
|
|||||||
Reference in New Issue
Block a user