Vivid
Loading...
Searching...
No Matches
OrthoCamera.cpp
1#include "OrthoCamera.h"
2#include "common/maths/Vec.h"
3#include <glm/gtc/matrix_transform.hpp>
4#include <iostream>
5
6OrthoCamera::OrthoCamera(Vivid::Maths::Vec3 position, float rotation, float zoomLevel, float near, float far)
7 : m_Rotation(rotation)
8 , m_ZoomLevel(zoomLevel)
9 , m_Near(near)
10 , m_Far(far)
11{
12 m_Position = position;
13 m_Left = 0;
14 m_Right = m_ViewportWidth / 2;
15 m_Bottom = 0;
16 m_Top = m_ViewportHeight / 2;
17
18 updateProjectionMatrix();
19 updateViewMatrix();
20}
21
22void OrthoCamera::updateProjectionMatrix()
23{
24 // m_ProjectionMatrix = glm::ortho(m_Left * m_ZoomLevel, m_Right * m_ZoomLevel, m_Bottom * m_ZoomLevel, m_Top * m_ZoomLevel, m_Near, m_Far);
25 // For now ignore zoom level
26 m_ProjectionMatrix = glm::ortho(m_Left, m_Right, m_Bottom, m_Top, m_Near, m_Far);
27}
28
29void OrthoCamera::updateViewMatrix()
30{
31 // TODO: Add rotations and movement
32 // m_ViewMatrix = glm::translate(glm::mat4(1.0f), m_Position.ToGLM());
33 glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position.ToGLM())
34 * glm::rotate(glm::mat4(1.0f), m_Rotation, glm::vec3(0, 0, 1));
35 m_ViewMatrix = glm::inverse(transform);
36}
37
38void OrthoCamera::SetRotation(float rotation)
39{
40 m_Rotation = rotation;
41}
42
43void OrthoCamera::SetPerspective(float left, float right, float bottom, float top)
44{
45 m_ProjectionMatrix = glm::ortho(left, right, bottom, top, -1.0f, 1.0f);
46}
47
48void OrthoCamera::SetViewportSize(int width, int height)
49{
50 m_Left = 0;
51 m_Right = width;
52 m_Bottom = 0;
53 m_Top = height;
54 m_ViewportWidth = width;
55 m_ViewportHeight = height;
56 updateProjectionMatrix();
57 updateViewMatrix();
58}
59
60void OrthoCamera::MoveForward()
61{
62 // m_ZoomLevel -= m_Speed;
63 m_ZoomLevel = std::fmax(m_ZoomLevel, 0.01f);
64 updateViewMatrix();
65}
66
67void OrthoCamera::MoveBackward()
68{
69 // m_ZoomLevel += m_Speed;
70 m_ZoomLevel = std::fmax(m_ZoomLevel, 0.01f);
71 updateViewMatrix();
72}
73
74void OrthoCamera::MoveLeft()
75{
76 m_Position.x += m_Speed;
77 updateViewMatrix();
78}
79
80void OrthoCamera::MoveRight()
81{
82 m_Position.x -= m_Speed;
83 updateViewMatrix();
84}
85
86void OrthoCamera::ProcessMouseScroll(float scrollOffset)
87{
88 m_ZoomLevel -= scrollOffset;
89 m_ZoomLevel = std::fmax(m_ZoomLevel, 0.25f);
90 updateProjectionMatrix();
91}
92
93void OrthoCamera::ProcessMouseMovement(float xOffset, float yOffset, bool constrainPitch)
94{
95 m_Position.x -= xOffset * m_Speed * m_ScrollSpeed;
96 m_Position.y += yOffset * m_Speed * m_ScrollSpeed;
97 updateViewMatrix();
98}
99
100Vivid::Maths::Vec2 OrthoCamera::ScreenToWorldCoords(float x, float y)
101{
102 glm::vec4 screenPos = glm::vec4(x, y, 0.0f, 1.0f);
103 glm::vec4 worldPos = glm::inverse(m_ViewMatrix) * screenPos;
104 // glm::vec4 worldPos = screenPos;
105 // glm::vec4 worldPos = screenPos + glm::vec4((m_Position * 2).ToGLM(),0.0f);
106
107 return Vivid::Maths::Vec2(worldPos.x / worldPos.w, worldPos.y / worldPos.w);
108}
Contains a 2D vector.
Definition: Vec.h:108
Contains a 3D vector.
Definition: Vec.h:51