Skip to content

Commit

Permalink
Added SceneCamera + camera resizing support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed Aug 5, 2020
1 parent 7ee0fde commit 8a81a7e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 17 deletions.
7 changes: 5 additions & 2 deletions Hazel/src/Hazel/Renderer/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ namespace Hazel {
class Camera
{
public:
Camera() = default;
Camera(const glm::mat4& projection)
: m_Projection(projection) {}

virtual ~Camera() = default;

const glm::mat4& GetProjection() const { return m_Projection; }
private:
glm::mat4 m_Projection;
protected:
glm::mat4 m_Projection = glm::mat4(1.0f);
};

}
7 changes: 3 additions & 4 deletions Hazel/src/Hazel/Scene/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <glm/glm.hpp>

#include "Hazel/Renderer/Camera.h"
#include "SceneCamera.h"

namespace Hazel {

Expand Down Expand Up @@ -41,13 +41,12 @@ namespace Hazel {

struct CameraComponent
{
Hazel::Camera Camera;
SceneCamera Camera;
bool Primary = true; // TODO: think about moving to Scene
bool FixedAspectRatio = false;

CameraComponent() = default;
CameraComponent(const CameraComponent&) = default;
CameraComponent(const glm::mat4& projection)
: Camera(projection) {}
};

}
22 changes: 19 additions & 3 deletions Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ namespace Hazel {
Camera* mainCamera = nullptr;
glm::mat4* cameraTransform = nullptr;
{
auto group = m_Registry.view<TransformComponent, CameraComponent>();
for (auto entity : group)
auto view = m_Registry.view<TransformComponent, CameraComponent>();
for (auto entity : view)
{
auto& [transform, camera] = group.get<TransformComponent, CameraComponent>(entity);
auto& [transform, camera] = view.get<TransformComponent, CameraComponent>(entity);

if (camera.Primary)
{
Expand Down Expand Up @@ -97,4 +97,20 @@ namespace Hazel {

}

void Scene::OnViewportResize(uint32_t width, uint32_t height)
{
m_ViewportWidth = width;
m_ViewportHeight = height;

// Resize our non-FixedAspectRatio cameras
auto view = m_Registry.view<CameraComponent>();
for (auto entity : view)
{
auto& cameraComponent = view.get<CameraComponent>(entity);
if (!cameraComponent.FixedAspectRatio)
cameraComponent.Camera.SetViewportSize(width, height);
}

}

}
2 changes: 2 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace Hazel {
Entity CreateEntity(const std::string& name = std::string());

void OnUpdate(Timestep ts);
void OnViewportResize(uint32_t width, uint32_t height);
private:
entt::registry m_Registry;
uint32_t m_ViewportWidth = 0, m_ViewportHeight = 0;

friend class Entity;
};
Expand Down
38 changes: 38 additions & 0 deletions Hazel/src/Hazel/Scene/SceneCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "hzpch.h"
#include "SceneCamera.h"

#include <glm/gtc/matrix_transform.hpp>

namespace Hazel {

SceneCamera::SceneCamera()
{
RecalculateProjection();
}

void SceneCamera::SetOrthographic(float size, float nearClip, float farClip)
{
m_OrthographicSize = size;
m_OrthographicNear = nearClip;
m_OrthographicFar = farClip;
RecalculateProjection();
}

void SceneCamera::SetViewportSize(uint32_t width, uint32_t height)
{
m_AspectRatio = (float)width / (float)height;
RecalculateProjection();
}

void SceneCamera::RecalculateProjection()
{
float orthoLeft = -m_OrthographicSize * m_AspectRatio * 0.5f;
float orthoRight = m_OrthographicSize * m_AspectRatio * 0.5f;
float orthoBottom = -m_OrthographicSize * 0.5f;
float orthoTop = m_OrthographicSize * 0.5f;

m_Projection = glm::ortho(orthoLeft, orthoRight,
orthoBottom, orthoTop, m_OrthographicNear, m_OrthographicFar);
}

}
28 changes: 28 additions & 0 deletions Hazel/src/Hazel/Scene/SceneCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "Hazel/Renderer/Camera.h"

namespace Hazel {

class SceneCamera : public Camera
{
public:
SceneCamera();
virtual ~SceneCamera() = default;

void SetOrthographic(float size, float nearClip, float farClip);

void SetViewportSize(uint32_t width, uint32_t height);

float GetOrthographicSize() const { return m_OrthographicSize; }
void SetOrthographicSize(float size) { m_OrthographicSize = size; RecalculateProjection(); }
private:
void RecalculateProjection();
private:
float m_OrthographicSize = 10.0f;
float m_OrthographicNear = -1.0f, m_OrthographicFar = 1.0f;

float m_AspectRatio = 0.0f;
};

}
12 changes: 6 additions & 6 deletions Hazelnut/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ Collapsed=0

[Window][Settings]
Pos=0,19
Size=426,701
Size=899,701
Collapsed=0
DockId=0x00000001,0

[Window][Viewport]
Pos=428,19
Size=852,701
Pos=901,19
Size=379,701
Collapsed=0
DockId=0x00000002,0

[Docking][Data]
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=320,351 Size=1280,701 Split=X Selected=0x995B0CF8
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=426,701 Selected=0x1C33C293
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=852,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=437,391 Size=1280,701 Split=X Selected=0x995B0CF8
DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=899,701 Selected=0x1C33C293
DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1659,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8

13 changes: 11 additions & 2 deletions Hazelnut/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace Hazel {
m_SquareEntity = square;

m_CameraEntity = m_ActiveScene->CreateEntity("Camera Entity");
m_CameraEntity.AddComponent<CameraComponent>(glm::ortho(-16.0f, 16.0f, -9.0f, 9.0f, -1.0f, 1.0f));
m_CameraEntity.AddComponent<CameraComponent>();

m_SecondCamera = m_ActiveScene->CreateEntity("Clip-Space Entity");
auto& cc = m_SecondCamera.AddComponent<CameraComponent>(glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f));
auto& cc = m_SecondCamera.AddComponent<CameraComponent>();
cc.Primary = false;
}

Expand All @@ -55,6 +55,8 @@ namespace Hazel {
{
m_Framebuffer->Resize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_CameraController.OnResize(m_ViewportSize.x, m_ViewportSize.y);

m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
}

// Update
Expand Down Expand Up @@ -166,6 +168,13 @@ namespace Hazel {
m_SecondCamera.GetComponent<CameraComponent>().Primary = !m_PrimaryCamera;
}

{
auto& camera = m_SecondCamera.GetComponent<CameraComponent>().Camera;
float orthoSize = camera.GetOrthographicSize();
if (ImGui::DragFloat("Second Camera Ortho Size", &orthoSize))
camera.SetOrthographicSize(orthoSize);
}


ImGui::End();

Expand Down

0 comments on commit 8a81a7e

Please sign in to comment.