BARE2D
Camera2D.cpp
Go to the documentation of this file.
1 #include "Camera2D.hpp"
2 
3 #include <glm/gtc/matrix_transform.hpp>
4 
5 #include "Logger.hpp"
6 
7 namespace BARE2D {
8 
10  m_resolution(1.0f, 1.0f), m_cameraMatrix(glm::mat4(0.0f)), m_orthographicMatrix(glm::mat4(0.0f)),
11  m_matrixNeedsUpdate(true) {
12  }
13 
15  }
16 
17  void Camera2D::init(float screenWidth, float screenHeight) {
18  // Set the necessary bits
19  m_resolution = glm::vec2(screenWidth, screenHeight);
20 
21  // Reset the necessary bits
22  m_state.scale = glm::vec2(1.0f);
23  m_state.focus = m_resolution / 2.0f;
25 
26  // Specifies a 2D coordinate system from 0.0-screenWidth in the X dimension and 0.0-screenHeight in the y direction.
27  // Allows for transformation from an unbounded/3D system to a 2D projection.
28  m_orthographicMatrix = glm::ortho(0.0f, m_resolution.x, 0.0f, m_resolution.y);
29  m_cameraMatrix = glm::mat4(0.0f);
30 
31  // Make sure we update possible old stuff and initialize new stuff otherwise.
32  m_matrixNeedsUpdate = true;
33  }
34 
36  // Check if we need to do expensive operations
38  // Shit. We do need to.
39  // First, translate the focus of the camera to the origin, so that we scale from the center.
40  glm::vec2 statePosition = m_state.focus;
41  glm::vec3 translation = -glm::vec3(statePosition.x, statePosition.y, 0.0f);
42 
43  // Multiply by our transforming vector to actually move it.
44  m_cameraMatrix = glm::translate(m_orthographicMatrix, translation);
45 
46  // Find the scale of the camera
47  glm::vec3 scale = glm::vec3(m_state.scale.x, m_state.scale.y, 0.0f);
48 
49  // Multiply by our scale matrix.
50  m_cameraMatrix = glm::scale(glm::mat4(1.0f), scale) * m_cameraMatrix;
51 
52  // Now that we're scaled, translate the bottom left to the origin, so that we have 0-resolution in both axes.
53  glm::vec3 translation1 = glm::vec3(m_resolution.x / 2.0f, m_resolution.y / 2.0f, 0.0f);
54  // Multiply
55  m_cameraMatrix = glm::translate(m_cameraMatrix, translation1);
56 
57  // Make sure we don't have to do this again every goddamn frame
58  m_matrixNeedsUpdate = false;
59  }
60  }
61 
62  glm::vec2 Camera2D::getWorldspaceCoord(glm::vec2 pos) const {
63  // Inverse of viewspacecoord
64  /*glm::vec2 result = pos * m_state.scale;
65  result -= m_state.focus * m_state.scale;
66  result += m_state.focus;
67  result -= getStatePosition();*/
68 
69  glm::vec2 result = getStatePosition() + pos / m_state.scale;
70 
71  return result;
72  }
73 
74  glm::vec2 Camera2D::getViewspaceCoord(glm::vec2 pos) const {
75  /*glm::vec2 diffUnscaled = (pos - m_state.focus);
76  glm::vec2 result = m_state.focus;
77 
78  result += getStatePosition() / m_state.scale;
79  result += diffUnscaled / m_state.scale;*/
80 
81  glm::vec2 result = pos - getStatePosition();
82  result *= m_state.scale;
83 
84  return result;
85  }
86 
87  glm::vec2 Camera2D::getWorldspaceSize(glm::vec2 size) const {
88  glm::vec2 result = size;
89 
90  result /= m_state.scale;
91 
92  return result;
93  }
94 
95  glm::vec2 Camera2D::getViewspaceSize(glm::vec2 size) const {
96  glm::vec2 result = size;
97 
98  result *= m_state.scale;
99 
100  return result;
101  }
102 
103  glm::vec4 Camera2D::getWorldspaceRect(glm::vec4 destRect) const {
104  glm::vec2 pos = glm::vec2(destRect.x, destRect.y);
105  glm::vec2 size = glm::vec2(destRect.z, destRect.w);
106 
107  pos = getWorldspaceCoord(pos);
108  size = getWorldspaceSize(size);
109 
110  return glm::vec4(pos.x, pos.y, size.x, size.y);
111  }
112 
113  glm::vec4 Camera2D::getViewspaceRect(glm::vec4 destRect) const {
114  glm::vec2 pos = glm::vec2(destRect.x, destRect.y);
115  glm::vec2 size = glm::vec2(destRect.z, destRect.w);
116 
117  pos = getViewspaceCoord(pos);
118  size = getViewspaceSize(size);
119 
120  return glm::vec4(pos.x, pos.y, size.x, size.y);
121  }
122 
123  void Camera2D::setFocus(glm::vec2 newFocus) {
125 
126  m_state.focus = newFocus;
127 
128  m_matrixNeedsUpdate = true;
129  }
130 
131  void Camera2D::offsetFocus(glm::vec2 deltaFocus) {
133 
134  m_state.focus += deltaFocus;
135 
136  m_matrixNeedsUpdate = true;
137  }
138 
139  void Camera2D::setScale(float newScaleX, float newScaleY) {
141 
142  m_state.scale.x = newScaleX;
143  m_state.scale.y = newScaleY;
144 
145  m_matrixNeedsUpdate = true;
146  }
147 
148  void Camera2D::offsetScale(float deltaScaleX, float deltaScaleY) {
150 
151  m_state.scale.x += deltaScaleX;
152  m_state.scale.y += deltaScaleY;
153 
154  m_matrixNeedsUpdate = true;
155  }
156 
158  return m_resolution;
159  }
160 
161  glm::vec2 Camera2D::getFocus() const {
162  return m_state.focus;
163  }
164 
165  glm::vec2 Camera2D::getScale() const {
166  return m_state.scale;
167  }
168 
169  const glm::mat4& Camera2D::getCameraMatrix() const {
170  return m_cameraMatrix;
171  }
172 
173  bool Camera2D::isRectInViewspace(glm::vec4& destRect) const {
174  return true;
175  }
176 
178  return m_state;
179  }
180 
182  return m_lastState;
183  }
184 
185  glm::vec2 Camera2D::getStatePosition() const {
186  return m_state.focus - (m_resolution / 2.0f) / m_state.scale;
187  }
188 
189 } // namespace BARE2D
BARE2D::Camera2D::init
void init(float screenWidth, float screenHeight)
Initializes the camera.
Definition: Camera2D.cpp:17
BARE2D
Definition: App.cpp:13
BARE2D::Camera2D::offsetScale
void offsetScale(float deltaScaleX, float deltaScaleY)
Adds to the zoom factor of the camera.
Definition: Camera2D.cpp:148
BARE2D::Camera2D::isRectInViewspace
bool isRectInViewspace(glm::vec4 &destRect) const
Definition: Camera2D.cpp:173
BARE2D::Camera2D::m_orthographicMatrix
glm::mat4 m_orthographicMatrix
Definition: Camera2D.hpp:145
BARE2D::Camera2D::getScale
glm::vec2 getScale() const
Definition: Camera2D.cpp:165
BARE2D::Camera2D::m_matrixNeedsUpdate
bool m_matrixNeedsUpdate
Definition: Camera2D.hpp:147
BARE2D::Camera2D::m_cameraMatrix
glm::mat4 m_cameraMatrix
Definition: Camera2D.hpp:145
Camera2D.hpp
BARE2D::Camera2D::getCameraMatrix
const glm::mat4 & getCameraMatrix() const
Definition: Camera2D.cpp:169
BARE2D::Camera2D::getViewspaceCoord
glm::vec2 getViewspaceCoord(glm::vec2 pos) const
Converts a coordinate from Worldspace to Viewspace.
Definition: Camera2D.cpp:74
BARE2D::Camera2D::~Camera2D
~Camera2D()
Definition: Camera2D.cpp:14
BARE2D::Camera2D::setFocus
void setFocus(glm::vec2 newFocus)
Sets the center of the camera to newFocus.
Definition: Camera2D.cpp:123
BARE2D::Camera2D::m_lastState
CameraState m_lastState
Definition: Camera2D.hpp:142
BARE2D::Camera2D::getWorldspaceCoord
glm::vec2 getWorldspaceCoord(glm::vec2 pos) const
Converts a coordinate from Worldspace to Viewspace.
Definition: Camera2D.cpp:62
BARE2D::Camera2D::Camera2D
Camera2D()
Definition: Camera2D.cpp:9
BARE2D::Camera2D::getStatePosition
glm::vec2 getStatePosition() const
Definition: Camera2D.cpp:185
BARE2D::Camera2D::getViewspaceSize
glm::vec2 getViewspaceSize(glm::vec2 size) const
Definition: Camera2D.cpp:95
BARE2D::CameraState
Holds some basic information that the camera holds in a state.
Definition: Camera2D.hpp:11
BARE2D::Camera2D::getViewspaceRect
glm::vec4 getViewspaceRect(glm::vec4 destRect) const
Definition: Camera2D.cpp:113
BARE2D::Camera2D::offsetFocus
void offsetFocus(glm::vec2 deltaPos)
Moves the center of the camera to focus + deltaPos.
Definition: Camera2D.cpp:131
BARE2D::Camera2D::update
void update()
Updates the matrix if necessary.
Definition: Camera2D.cpp:35
BARE2D::Camera2D::m_state
CameraState m_state
Definition: Camera2D.hpp:140
BARE2D::Camera2D::getWorldspaceRect
glm::vec4 getWorldspaceRect(glm::vec4 destRect) const
Definition: Camera2D.cpp:103
BARE2D::Camera2D::getViewspaceResolution
glm::vec2 getViewspaceResolution() const
Definition: Camera2D.cpp:157
BARE2D::Camera2D::getLastState
CameraState getLastState()
Definition: Camera2D.cpp:181
BARE2D::Camera2D::setScale
void setScale(float newScaleX, float newScaleY)
Sets the scale of the camera.
Definition: Camera2D.cpp:139
BARE2D::CameraState::focus
glm::vec2 focus
Definition: Camera2D.hpp:12
BARE2D::Camera2D::getState
CameraState getState()
Definition: Camera2D.cpp:177
BARE2D::CameraState::scale
glm::vec2 scale
Definition: Camera2D.hpp:13
BARE2D::Camera2D::m_resolution
glm::vec2 m_resolution
Definition: Camera2D.hpp:138
BARE2D::Camera2D::getFocus
glm::vec2 getFocus() const
Definition: Camera2D.cpp:161
Logger.hpp
BARE2D::Camera2D::getWorldspaceSize
glm::vec2 getWorldspaceSize(glm::vec2 size) const
Definition: Camera2D.cpp:87