TazGraph Project v0.1.0
Loading...
Searching...
No Matches
OrthoCamera.h
1#pragma once
2#include <SDL2/SDL.h>
3#include "ICamera.h"
4
5class OrthoCamera : public ICamera {
6public:
7 OrthoCamera() : _position(0.0f),
8 _cameraMatrix(1.0f), //I
9 _projectionMatrix(1.0f), //I
10 _viewMatrix(1.0f),
11 _scale(1.0f),
12 _cameraChange(true),
13 _screenWidth(800),
14 _screenHeight(640)
15 {
16
17 }
19 {
20
21 }
22
23 void init() override {
24
25 _projectionMatrix = glm::ortho(0.0f, (float)_screenWidth, (float)_screenHeight, 0.0f);
26 }
27
28 void update() override {
29
30 if (_cameraChange) {
31
32 _cameraMatrix = glm::mat4(1.0f);
33
34 glm::vec3 scale(_scale, _scale, 1.0f);
35 _cameraMatrix = glm::scale(_cameraMatrix, scale);
36
37
38 glm::vec3 translate(-_position.x, -_position.y, 0.0f);
39 _cameraMatrix = glm::translate(_cameraMatrix, translate); //if glm ortho = -1,1,-1,1 then 1 horizontal with -400,-320 to bottom-left
40
41 _cameraMatrix = _projectionMatrix * _viewMatrix * _cameraMatrix;
42
43 //_cameraMatrix = glm::scale(_cameraMatrix, scale);
44 _cameraChange = false;
45 }
46 }
47
48 glm::vec2 convertScreenToWorld(glm::vec2 screenCoords) const override {
49 //Make 0 the center
50 screenCoords -= glm::vec2(_screenWidth / 2, _screenHeight / 2);
51 //Scale coordinates
52 screenCoords /= _scale;
53 screenCoords += glm::vec2(_screenWidth / 2, _screenHeight / 2);
54 //Translate with the camera2D.worldLocation position
55 screenCoords.x += _position.x;
56 screenCoords.y += _position.y;
57
58
59 return screenCoords;
60 }
61
62 //setters
63 void setPosition(const glm::vec3 newPosition) override {
64 _position = newPosition;
65 _cameraChange = true;
66 }
67
68 void setPosition_X(const float newPosition) override {
69 _position.x = newPosition;
70 _cameraChange = true;
71 }
72
73 void setPosition_Y(const float newPosition) override {
74 _position.y = newPosition;
75 _cameraChange = true;
76 }
77
78 void setPosition_Z(const float newPosition) override {
79 _position.z = newPosition;
80 _cameraChange = true;
81 }
82
83 void setScale(float newScale) override {
84 _scale = newScale;
85 _cameraChange = true;
86 }
87
88 //getters
89 glm::vec3 getPosition() const override {
90 return _position;
91 }
92
93 float getScale() const override {
94 return _scale;
95 }
96
97 glm::mat4 getCameraMatrix() const override {
98 return _cameraMatrix;
99 }
100
101 glm::ivec2 getCameraDimensions() const override {
102 glm::vec2 cameraDimensions = { _screenWidth, _screenHeight };
103 return cameraDimensions;
104 }
105
106 SDL_FRect getCameraRect() const override {
107 float cameraWidth = getCameraDimensions().x / getScale();
108 float cameraHeight = getCameraDimensions().y / getScale();
109
110 float cameraX = _position.x - cameraWidth / 2.0f + getCameraDimensions().x / 2;
111 float cameraY = _position.y - cameraHeight / 2.0f + getCameraDimensions().y / 2;
112
113 SDL_FRect cameraRect = { cameraX , cameraY , cameraWidth, cameraHeight };
114 return cameraRect;
115 }
116
117 void setCameraMatrix(glm::mat4 newMatrix) {
118 _cameraChange = true;
119 }
120
121 bool isPointInCameraView(const glm::vec4 point, float margin)
122 {
123 glm::mat4 vpMatrix = _cameraMatrix;
124
125 glm::vec4 clipSpacePos = vpMatrix * point;
126
127 if (clipSpacePos.w != 0.0f) {
128 clipSpacePos.x /= clipSpacePos.w;
129 clipSpacePos.y /= clipSpacePos.w;
130 clipSpacePos.z /= clipSpacePos.w;
131 }
132
133 // 0.2f is the margin
134 if (clipSpacePos.x < -1.0f - margin || clipSpacePos.x > 1.0f + margin) return false;
135 if (clipSpacePos.y < -1.0f - margin || clipSpacePos.y > 1.0f + margin) return false;
136 if (clipSpacePos.z < -margin || clipSpacePos.z > 1.0f + margin) return false;
137
138 return true;
139 }
140
141 bool hasChanged() override {
142 return _cameraChange;
143 }
144
145 void makeCameraDirty() override {
146 _cameraChange = true;
147 }
148
149 void refreshCamera() override {
150 _cameraChange = false;
151 }
152
153private:
154 int _screenWidth, _screenHeight;
155 float _scale;
156 bool _cameraChange;
157
158 glm::vec3 _position;
159 glm::mat4 _projectionMatrix; // changed once in init
160 glm::mat4 _viewMatrix;
161 glm::mat4 _cameraMatrix;
162};
Definition ICamera.h:9
Definition OrthoCamera.h:5