TazGraph Project v0.1.0
Loading...
Searching...
No Matches
OrthoCamera.h
1#pragma once
2#include <SDL2/SDL.h>
3#include "ICamera.h"
4
5
6class OrthoCamera : public ICamera {
7public:
8
9
10 OrthoCamera() :
11 _scale(1.0f)
12 {
13 eyePos = glm::vec3(0.f, 0.f, -770.0f);
14 aimPos = glm::vec3(0.f, 0.f, 0.f);
15 }
17 {
18
19 }
20
21 void init() override {
22
23 _projectionMatrix = glm::ortho(0.0f, (float)_screenWidth, 0.0f, (float)_screenHeight );
24
25 updateCameraOrientation();
26
27 _cameraMatrix = glm::mat4(1.0f);
28
29 _cameraMatrix = _projectionMatrix * _viewMatrix;
30 }
31
32 void update() override {
33
34 if (_cameraChange) {
35 updateCameraOrientation();
36
37 _cameraMatrix = glm::mat4(1.0f);
38
39 _cameraMatrix = _projectionMatrix * _viewMatrix;
40 }
41 }
42
43
44 glm::vec2 convertScreenToWorld(glm::vec2 screenCoords) const override {
45 //Make 0 the center
46 screenCoords -= glm::vec2(_screenWidth / 2, _screenHeight / 2);
47 //Scale coordinates
48 screenCoords /= _scale;
49 screenCoords += glm::vec2(_screenWidth / 2, _screenHeight / 2);
50 //Translate with the camera2D.worldLocation position
51 screenCoords.x += eyePos.x;
52 screenCoords.y += eyePos.y;
53
54
55 return screenCoords;
56 }
57
58 //setters
59 void setPosition(const glm::vec3 newPosition) override {
60 eyePos = newPosition;
61 _cameraChange = true;
62 }
63
64 void setPosition_X(const float newPosition) override {
65 eyePos.x = newPosition;
66 _cameraChange = true;
67 }
68
69 void setPosition_Y(const float newPosition) override {
70 eyePos.y = newPosition;
71 _cameraChange = true;
72 }
73
74 void setPosition_Z(const float newPosition) override {
75 eyePos.z = newPosition;
76 _cameraChange = true;
77 }
78
79 void movePosition_Hor(const float step) {
80 glm::vec3 direction = glm::normalize(aimPos - eyePos); // Get movement direction
81
82 // Calculate the right vector (perpendicular to direction and up)
83 glm::vec3 right = glm::normalize(glm::cross(direction, upDir));
84
85 // Move the camera horizontally along the right vector
86 eyePos += right * step;
87 aimPos += right * step;
88 _cameraChange = true;
89 }
90 void movePosition_Vert(const float step) {
91 glm::vec3 direction = glm::normalize(aimPos - eyePos); // Get movement direction
92
93 // Move the camera horizontally along the right vector
94 eyePos += upDir * step;
95 aimPos += upDir * step;
96 _cameraChange = true;
97 }
98
99 void movePosition_Forward(const float step) {
100 glm::vec3 direction = glm::normalize(aimPos - eyePos);
101 eyePos += direction * step;
102 aimPos += direction * step;
103 _cameraChange = true;
104 }
105
106 void setAimPos(const glm::vec3 newAimPos) {
107 aimPos = newAimPos;
108 _cameraChange = true;
109 }
110
111 void moveAimPos(glm::vec3 startingAimPos, const glm::vec2 distance) {
112 aimPos = startingAimPos;
113 const float sensitivity = 0.005f;
114
115 float yaw = distance.x * sensitivity;
116 float pitch = distance.y * sensitivity;
117
118 glm::vec3 direction = glm::normalize(aimPos - eyePos);
119
120 direction = glm::rotate(direction, yaw, upDir);
121
122 glm::vec3 right = glm::normalize(glm::cross(direction, upDir));
123
124 direction = glm::rotate(direction, pitch, right);
125
126 // Update the aimPos based on the new direction
127 aimPos = eyePos + direction;
128 _cameraChange = true;
129 }
130
131 glm::vec3 getEulerAnglesFromDirection(glm::vec3 direction) {
132 float yaw = glm::atan(direction.x, direction.z);
133 float pitch = glm::asin(-direction.y);
134 float roll = 0.0f;
135
136 return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), glm::degrees(roll));
137 }
138
139 glm::vec3 getAimPos() override {
140 return aimPos;
141 }
142
143 void setScale(float newScale) override {
144 _scale = newScale;
145 _cameraChange = true;
146 }
147
148 //getters
149 glm::vec3 getPosition() const override {
150 return eyePos;
151 }
152
153 float getScale() const override {
154 return _scale;
155 }
156
157 glm::mat4 getCameraMatrix() const override {
158 return _cameraMatrix;
159 }
160
161 glm::ivec2 getCameraDimensions() const override {
162 glm::vec2 cameraDimensions = { _screenWidth, _screenHeight };
163 return cameraDimensions;
164 }
165
166 SDL_FRect getCameraRect() const override {
167 float cameraWidth = getCameraDimensions().x / getScale();
168 float cameraHeight = getCameraDimensions().y / getScale();
169
170 float cameraX = eyePos.x - cameraWidth / 2.0f + getCameraDimensions().x / 2;
171 float cameraY = eyePos.y - cameraHeight / 2.0f + getCameraDimensions().y / 2;
172
173 SDL_FRect cameraRect = { cameraX , cameraY , cameraWidth, cameraHeight };
174 return cameraRect;
175 }
176
177 void setCameraMatrix(glm::mat4 newMatrix) {
178 _cameraChange = true;
179 }
180
181 bool hasChanged() override {
182 return _cameraChange;
183 }
184
185 void makeCameraDirty() override {
186 _cameraChange = true;
187 }
188
189 void refreshCamera() override {
190 _cameraChange = false;
191 }
192
193private:
194 float _scale;
195};
Definition ICamera.h:14
Definition OrthoCamera.h:6