Skip to content
Permalink
Browse files

Added orthographic camera controller

  • Loading branch information...
TheCherno committed Sep 20, 2019
1 parent f50af96 commit 0e034759705bb3b8ec5fa942e664798df9487469
@@ -11,6 +11,7 @@
#include "Hazel/Input.h"
#include "Hazel/KeyCodes.h"
#include "Hazel/MouseButtonCodes.h"
#include "Hazel/OrthographicCameraController.h"

#include "Hazel/ImGui/ImGuiLayer.h"

@@ -0,0 +1,63 @@
#include "hzpch.h"
#include "OrthographicCameraController.h"

#include "Hazel/Input.h"
#include "Hazel/KeyCodes.h"

namespace Hazel {

OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation)
: m_AspectRatio(aspectRatio), m_Camera(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_Rotation(rotation)
{
}

void OrthographicCameraController::OnUpdate(Timestep ts)
{
if (Input::IsKeyPressed(HZ_KEY_A))
m_CameraPosition.x -= m_CameraTranslationSpeed * ts;
else if (Input::IsKeyPressed(HZ_KEY_D))
m_CameraPosition.x += m_CameraTranslationSpeed * ts;

if (Input::IsKeyPressed(HZ_KEY_W))
m_CameraPosition.y += m_CameraTranslationSpeed * ts;
else if (Input::IsKeyPressed(HZ_KEY_S))
m_CameraPosition.y -= m_CameraTranslationSpeed * ts;

if (m_Rotation)
{
if (Input::IsKeyPressed(HZ_KEY_Q))
m_CameraRotation += m_CameraRotationSpeed * ts;
if (Input::IsKeyPressed(HZ_KEY_E))
m_CameraRotation -= m_CameraRotationSpeed * ts;

m_Camera.SetRotation(m_CameraRotation);
}

m_Camera.SetPosition(m_CameraPosition);

m_CameraTranslationSpeed = m_ZoomLevel;
}

void OrthographicCameraController::OnEvent(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.Dispatch<MouseScrolledEvent>(HZ_BIND_EVENT_FN(OrthographicCameraController::OnMouseScrolled));
dispatcher.Dispatch<WindowResizeEvent>(HZ_BIND_EVENT_FN(OrthographicCameraController::OnWindowResized));
}

bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e)
{
m_ZoomLevel -= e.GetYOffset() * 0.25f;
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
return false;
}

bool OrthographicCameraController::OnWindowResized(WindowResizeEvent& e)
{
m_AspectRatio = (float)e.GetWidth() / (float)e.GetHeight();
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
return false;
}

}
@@ -0,0 +1,36 @@
#pragma once

#include "Hazel/Renderer/OrthographicCamera.h"
#include "Hazel/Core/Timestep.h"

#include "Hazel/Events/ApplicationEvent.h"
#include "Hazel/Events/MouseEvent.h"

namespace Hazel {

class OrthographicCameraController
{
public:
OrthographicCameraController(float aspectRatio, bool rotation = false);

void OnUpdate(Timestep ts);
void OnEvent(Event& e);

OrthographicCamera& GetCamera() { return m_Camera; }
const OrthographicCamera& GetCamera() const { return m_Camera; }
private:
bool OnMouseScrolled(MouseScrolledEvent& e);
bool OnWindowResized(WindowResizeEvent& e);
private:
float m_AspectRatio;
float m_ZoomLevel = 1.0f;
OrthographicCamera m_Camera;

bool m_Rotation;

glm::vec3 m_CameraPosition = { 0.0f, 0.0f, 0.0f };
float m_CameraRotation = 0.0f;
float m_CameraTranslationSpeed = 5.0f, m_CameraRotationSpeed = 180.0f;
};

}
@@ -11,6 +11,12 @@ namespace Hazel {
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}

void OrthographicCamera::SetProjection(float left, float right, float bottom, float top)
{
m_ProjectionMatrix = glm::ortho(left, right, bottom, top, -1.0f, 1.0f);
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}

void OrthographicCamera::RecalculateViewMatrix()
{
glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position) *
@@ -9,6 +9,8 @@ namespace Hazel {
public:
OrthographicCamera(float left, float right, float bottom, float top);

void SetProjection(float left, float right, float bottom, float top);

const glm::vec3& GetPosition() const { return m_Position; }
void SetPosition(const glm::vec3& position) { m_Position = position; RecalculateViewMatrix(); }

@@ -14,5 +14,10 @@ ViewportId=0x080FC883
Size=550,680
Collapsed=0

[Window][Settings]
Pos=974,649
Size=297,57
Collapsed=0

[Docking][Data]

@@ -11,7 +11,7 @@ class ExampleLayer : public Hazel::Layer
{
public:
ExampleLayer()
: Layer("Example"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f)
: Layer("Example"), m_CameraController(1280.0f / 720.0f)
{
m_VertexArray.reset(Hazel::VertexArray::Create());

@@ -139,28 +139,14 @@ class ExampleLayer : public Hazel::Layer

void OnUpdate(Hazel::Timestep ts) override
{
if (Hazel::Input::IsKeyPressed(HZ_KEY_LEFT))
m_CameraPosition.x -= m_CameraMoveSpeed * ts;
else if (Hazel::Input::IsKeyPressed(HZ_KEY_RIGHT))
m_CameraPosition.x += m_CameraMoveSpeed * ts;

if (Hazel::Input::IsKeyPressed(HZ_KEY_UP))
m_CameraPosition.y += m_CameraMoveSpeed * ts;
else if (Hazel::Input::IsKeyPressed(HZ_KEY_DOWN))
m_CameraPosition.y -= m_CameraMoveSpeed * ts;

if (Hazel::Input::IsKeyPressed(HZ_KEY_A))
m_CameraRotation += m_CameraRotationSpeed * ts;
if (Hazel::Input::IsKeyPressed(HZ_KEY_D))
m_CameraRotation -= m_CameraRotationSpeed * ts;
// Update
m_CameraController.OnUpdate(ts);

// Render
Hazel::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 });
Hazel::RenderCommand::Clear();

m_Camera.SetPosition(m_CameraPosition);
m_Camera.SetRotation(m_CameraRotation);

Hazel::Renderer::BeginScene(m_Camera);
Hazel::Renderer::BeginScene(m_CameraController.GetCamera());

glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));

@@ -197,8 +183,9 @@ class ExampleLayer : public Hazel::Layer
ImGui::End();
}

void OnEvent(Hazel::Event& event) override
void OnEvent(Hazel::Event& e) override
{
m_CameraController.OnEvent(e);
}
private:
Hazel::ShaderLibrary m_ShaderLibrary;
@@ -210,13 +197,7 @@ class ExampleLayer : public Hazel::Layer

Hazel::Ref<Hazel::Texture2D> m_Texture, m_ChernoLogoTexture;

Hazel::OrthographicCamera m_Camera;
glm::vec3 m_CameraPosition;
float m_CameraMoveSpeed = 5.0f;

float m_CameraRotation = 0.0f;
float m_CameraRotationSpeed = 180.0f;

Hazel::OrthographicCameraController m_CameraController;
glm::vec3 m_SquareColor = { 0.2f, 0.3f, 0.8f };
};

0 comments on commit 0e03475

Please sign in to comment.
You can’t perform that action at this time.