TazGraph Project v0.1.0
Loading...
Searching...
No Matches
PerspectiveCamera.h
1#pragma once
2#include <SDL2/SDL.h>
3#include "ICamera.h"
4
5
7public:
8 glm::vec3 panningAimPos{ 0,0,0 };
9
10 float fov = 45.0f;
11 float aspect = 0.0f;
12 float nearPlane = 0.1f;
13
15 _scale(1.0f)
16 {
17 eyePos = glm::vec3(0.f, 0.f, -770.0f);
18 aimPos = glm::vec3(0.f, 0.f, 0.f);
19 }
20
22 {
23
24 }
25
26 void init() override {
27 aspect = (float)_screenWidth / (float)_screenHeight;
28 _projectionMatrix = glm::perspective(glm::radians(fov), aspect, nearPlane, zFar); //left, right, top, bottom
29 updateCameraOrientation();
30
31 _cameraMatrix = glm::mat4(1.0f);
32
33 _cameraMatrix = _projectionMatrix * _viewMatrix;
34
35 }
36
37 void update() override {
38 if (_cameraChange) {
39 updateCameraOrientation();
40
41 _cameraMatrix = glm::mat4(1.0f);
42
43 _cameraMatrix = _projectionMatrix * _viewMatrix;
44
45 }
46
47 }
48
49 glm::vec2 convertScreenToWorld(glm::vec2 screenCoords) const override {
50 SDL_FRect cameraRect = getCameraRect();
51
52 glm::vec2 worldCoords;
53
54 worldCoords = screenCoords;
55 worldCoords /= _scale;
56
57 worldCoords.x = worldCoords.x + cameraRect.x;
58 worldCoords.y = worldCoords.y + cameraRect.y;
59
60
61 return worldCoords;
62 }
63
64 //setters
65 void setPosition(const glm::vec3 newPosition) override {
66 eyePos = newPosition;
67 _cameraChange = true;
68 }
69
70 void setPosition_X(const float newPosition) override {
71 eyePos.x = newPosition;
72 _cameraChange = true;
73 }
74
75 void setPosition_Y(const float newPosition) override {
76 eyePos.y = newPosition;
77 _cameraChange = true;
78 }
79
80 void setPosition_Z(const float newPosition) override {
81 eyePos.z = newPosition;
82 _cameraChange = true;
83 }
84
85 void movePosition_Hor(const float step) {
86 glm::vec3 direction = glm::normalize(aimPos - eyePos); // Get movement direction
87
88 // Calculate the right vector (perpendicular to direction and up)
89 glm::vec3 right = glm::normalize(glm::cross(direction, upDir));
90
91 // Move the camera horizontally along the right vector
92 eyePos += right * step;
93 aimPos += right * step;
94 _cameraChange = true;
95 }
96 void movePosition_Vert(const float step) {
97 glm::vec3 direction = glm::normalize(aimPos - eyePos); // Get movement direction
98
99 // Move the camera horizontally along the right vector
100 eyePos += upDir * step;
101 aimPos += upDir * step;
102 _cameraChange = true;
103 }
104
105 void movePosition_Forward(const float step) {
106 glm::vec3 direction = glm::normalize(aimPos - eyePos);
107 eyePos += direction * step;
108 aimPos += direction * step;
109 _cameraChange = true;
110 }
111
112 void setAimPos(const glm::vec3 newAimPos) {
113 aimPos = newAimPos;
114 _cameraChange = true;
115 }
116
117 void moveAimPos(glm::vec3 startingAimPos, const glm::vec2 distance) {
118 aimPos = startingAimPos;
119 const float sensitivity = 0.005f;
120
121 float yaw = distance.x * sensitivity;
122 float pitch = distance.y * sensitivity;
123
124 glm::vec3 direction = glm::normalize(aimPos - eyePos);
125
126 direction = glm::rotate(direction, yaw, upDir);
127
128 glm::vec3 right = glm::normalize(glm::cross(direction, upDir));
129
130 direction = glm::rotate(direction, pitch, right);
131
132 // Update the aimPos based on the new direction
133 aimPos = eyePos + direction;
134 _cameraChange = true;
135 }
136
137 glm::vec3 getEulerAnglesFromDirection(glm::vec3 direction) {
138 float yaw = glm::atan(direction.x, direction.z);
139 float pitch = glm::asin(-direction.y);
140 float roll = 0.0f;
141
142 return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), glm::degrees(roll));
143 }
144
145
146 float getZFar() {
147 return zFar;
148 }
149
150 glm::vec3 getAimPos() override {
151 return aimPos;
152 }
153
154 void setPanningAimPos(const glm::vec3 newAimPos) {
155 panningAimPos = newAimPos;
156 }
157
158 glm::vec3 getPanningAimPos() {
159 return panningAimPos;
160 }
161
162 void setScale(float newScale) override {
163 _scale = newScale;
164 _cameraChange = true;
165 }
166
167 //getters
168 glm::vec3 getPosition() const override {
169 return eyePos;
170 }
171
172 float getScale() const override {
173 return _scale;
174 }
175
176 glm::mat4 getCameraMatrix() const override {
177 return _cameraMatrix;
178 }
179
180 glm::ivec2 getCameraDimensions() const override {
181 glm::vec2 cameraDimensions = { _screenWidth, _screenHeight };
182 return cameraDimensions;
183 }
184
185 SDL_FRect getCameraRect() const override {
186 float cameraWidth = getCameraDimensions().x / getScale();
187 float cameraHeight = getCameraDimensions().y / getScale();
188
189 float cameraX = eyePos.x - cameraWidth / 2.0f ;
190 float cameraY = eyePos.y - cameraHeight / 2.0f ;
191
192 SDL_FRect cameraRect = { cameraX , cameraY , cameraWidth, cameraHeight };
193 return cameraRect;
194 }
195
196 void setCameraMatrix(glm::mat4 newMatrix) {
197 _cameraChange = true;
198 }
199 void resetCameraPosition() {
200
201 _scale = 1.0f;
202
203 eyePos = glm::vec3(0.f, 0.f, -770.0f);
204 aimPos = glm::vec3( 0,0,0 );
205
206 currentViewMode = ViewMode::Y_UP;
207 upDir = glm::vec3( 0,-1,0 );
208
209 init();
210
211 _cameraChange = true;
212 }
213 float getMinScale() {
214 return _minScale;
215 }
216
217 float getMaxScale() {
218 return _maxScale;
219 }
220
221
222 bool hasChanged() override {
223 return _cameraChange;
224 }
225
226 void makeCameraDirty() override {
227 _cameraChange = true;
228 }
229
230 void refreshCamera() override {
231 _cameraChange = false;
232 }
233
234
235
236 float _minScale = 0.1f, _maxScale = 5.0f;
237 float _scale; // decreases when zoom-out
238
239};
Definition ICamera.h:14
Definition PerspectiveCamera.h:6