TazGraph Project v0.1.0
Loading...
Searching...
No Matches
PerspectiveCamera.h
1#pragma once
2#include "ICamera.h"
3
4
5class PerspectiveCamera : public ICamera {
6public:
7 glm::vec3 panningAimPos{ 0,0,0 };
8
9 float fov = 45.0f;
10 float aspect = 0.0f;
11 float nearPlane = 0.1f;
12
14 {
15 _scale = 1.0f;
16 eyePos = glm::vec3(0.f, 0.f, -770.0f);
17 aimPos = glm::vec3(0.f, 0.f, 0.f);
18 }
19
21 {
22
23 }
24
25 void init() override {
26 aspect = (float)_screenWidth / (float)_screenHeight;
27 _projectionMatrix = glm::perspective(glm::radians(fov), aspect, nearPlane, zFar); //left, right, top, bottom
28 updateCameraOrientation();
29
30 _cameraMatrix = glm::mat4(1.0f);
31
32 _cameraMatrix = _projectionMatrix * _viewMatrix;
33
34 }
35
36 void update() override {
37 if (_cameraChange) {
38 updateCameraOrientation();
39
40 _cameraMatrix = glm::mat4(1.0f);
41
42 _cameraMatrix = _projectionMatrix * _viewMatrix;
43
44 }
45
46 }
47
48 glm::vec3 getEulerAnglesFromDirection(glm::vec3 direction) {
49 float yaw = glm::atan(direction.x, direction.z);
50 float pitch = glm::asin(-direction.y);
51 float roll = 0.0f;
52
53 return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), glm::degrees(roll));
54 }
55
56
57 float getZFar() {
58 return zFar;
59 }
60
61 void setPanningAimPos(const glm::vec3 newAimPos) {
62 panningAimPos = newAimPos;
63 }
64
65 glm::vec3 getPanningAimPos() {
66 return panningAimPos;
67 }
68
69 //getters
70
71 glm::ivec2 getCameraDimensions() const override {
72 glm::vec2 cameraDimensions = { _screenWidth, _screenHeight };
73 return cameraDimensions;
74 }
75
76 SDL_FRect getCameraRect() const override {
77 float cameraWidth = getCameraDimensions().x / getScale();
78 float cameraHeight = getCameraDimensions().y / getScale();
79
80 float cameraX = eyePos.x - cameraWidth / 2.0f;
81 float cameraY = eyePos.y - cameraHeight / 2.0f;
82
83 SDL_FRect cameraRect = { cameraX , cameraY , cameraWidth, cameraHeight };
84 return cameraRect;
85 }
86
87 void setCameraMatrix(glm::mat4 newMatrix) {
88 _cameraChange = true;
89 }
90 void resetCameraPosition() {
91
92 _scale = 1.0f;
93
94 eyePos = glm::vec3(0.f, 0.f, -770.0f);
95 aimPos = glm::vec3(0, 0, 0);
96
97 currentViewMode = ViewMode::Y_UP;
98 upDir = glm::vec3(0, -1, 0);
99
100 init();
101
102 _cameraChange = true;
103 }
104 float getMinScale() {
105 return _minScale;
106 }
107
108 float getMaxScale() {
109 return _maxScale;
110 }
111
112 void setAspect(float newAspectX, float newAspectY)
113 {
114 aspect = newAspectX/ newAspectY;
115 _projectionMatrix = glm::perspective(
116 glm::radians(fov),
117 aspect,
118 nearPlane,
119 zFar
120 );
121
122 updateCameraOrientation();
123
124 _cameraMatrix = glm::mat4(1.0f);
125
126 _cameraMatrix = _projectionMatrix * _viewMatrix;
127 }
128
129 float _minScale = 0.1f, _maxScale = 5.0f;
130
131};
Definition ICamera.h:10
Definition PerspectiveCamera.h:5