CoriEngine
Loading...
Searching...
No Matches
CameraController.cpp
Go to the documentation of this file.
2#include <glm/ext/matrix_clip_space.hpp>
3#include <glm/ext/matrix_transform.hpp>
4
5namespace Cori {
6 namespace Graphics {
7 // ReSharper disable once CppMemberFunctionMayBeConst
8 void CameraController::CreateOrthoCamera(const float left, float right, const float bottom, const float top, const uint8_t depth /*=50*/) {
9 m_CurrentCameraComponent->m_ProjectionMatrix = glm::ortho(left, right, bottom, top, static_cast<float>(-depth), 0.0f);
10 m_CurrentCameraComponent->m_ViewProjectionMatrix = m_CurrentCameraComponent->m_ProjectionMatrix;
11 m_CurrentCameraComponent->m_InitialCameraMinBound = { left, bottom };
12 m_CurrentCameraComponent->m_InitialCameraMaxBound = { right, top };
13 m_CurrentCameraComponent->m_CameraSize = { std::abs(right - left), std::abs(top - bottom) };
14
16
17 CORI_CORE_INFO_TAGGED({ Logger::Tags::Graphics::Self, Logger::Tags::Graphics::Camera }, "Created orthographic camera with properties - (left: {}, right: {}, bottom: {}, top: {}, depth: {})", left, right, bottom, top, depth);
18 }
19
20 // ReSharper disable once CppMemberFunctionMayBeConst
21 void CameraController::SetPosition(const glm::vec2 newPos) {
22 m_CurrentCameraComponent->m_CameraPosition = newPos;
23 }
24
26 return m_CurrentCameraComponent->m_CameraPosition;
27 }
28
30 return m_CurrentCameraComponent->m_CameraRotation;
31 }
32
33 glm::vec2 CameraController::GetScale() const {
34 return m_CurrentCameraComponent->m_CameraScale;
35 }
36
37 glm::vec2 CameraController::GetSize() const {
38 return m_CurrentCameraComponent->m_CameraSize;
39 }
40
41 // ReSharper disable once CppMemberFunctionMayBeConst
42 void CameraController::SetRotation(const float angle) {
43 m_CurrentCameraComponent->m_CameraRotation = angle;
44 }
45
46 // ReSharper disable once CppMemberFunctionMayBeConst
47 void CameraController::SetScale(const glm::vec2 factor) {
48 m_CurrentCameraComponent->m_CameraScale = factor;
49 }
50
51 // ReSharper disable once CppMemberFunctionMayBeConst
53 glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(m_CurrentCameraComponent->m_CameraSize / 2.0f, 0.0f)) *
54 glm::rotate(glm::mat4(1.0f), glm::radians(m_CurrentCameraComponent->m_CameraRotation), glm::vec3(0.0f, 0.0f, 1.0f)) *
55 glm::scale(glm::mat4(1.0f), glm::vec3(m_CurrentCameraComponent->m_CameraScale, 1.0f)) *
56 glm::translate(glm::mat4(1.0f), glm::vec3(-(m_CurrentCameraComponent->m_CameraSize / 2.0f + m_CurrentCameraComponent->m_CameraPosition), 0.0f));
57
58 m_CurrentCameraComponent->m_ViewProjectionMatrix = m_CurrentCameraComponent->m_ProjectionMatrix * view;
59
60 glm::mat3 translation;
61 translation[0] = glm::vec3(view[0].x, view[0].y, view[0].w);
62 translation[1] = glm::vec3(view[1].x, view[1].y, view[1].w);
63 translation[2] = glm::vec3(view[3].x, view[3].y, view[3].w);
64 translation = glm::inverse(translation) * glm::translate(glm::mat3(1.0f), glm::vec2(m_CurrentCameraComponent->m_CameraSize / 2.0f));
65
66 m_CurrentCameraComponent->m_CameraBounds = Utility::CalculateAABB(translation, m_CurrentCameraComponent->m_CameraSize / 2.0f);
67 }
68 }
69}
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
void SetPosition(const glm::vec2 newPos)
Sets the position of the camera.
void SetRotation(const float angle)
Sets the rotational angle of the camera.
void CreateOrthoCamera(const float left, float right, const float bottom, const float top, const uint8_t depth=50)
Creates or recreate an orthographic camera.
glm::vec2 GetPosition() const
Gets the current camera position.
float GetRotation() const
Gets the current camera rotation.
glm::vec2 GetSize() const
Gets the current camera size.
glm::vec2 GetScale() const
Gets the current camera scale.
void RecalculateVP()
Recalculates the view projection matrix.
void SetScale(const glm::vec2 scale)
Changes camera scale to the requested value.
Almost everything connected to graphics is in this namespace.
Definition Window.hpp:7
AABB CalculateAABB(const glm::mat3 &transform, const glm::vec2 halfSize)
Calculates the AABB for the quad taking into account rotation and scale.
Definition AABB.hpp:22
Global engine namespace.
static constexpr char Camera[]
Definition Logger.hpp:70
static constexpr char Self[]
Definition Logger.hpp:47