TDME2  1.9.200
Camera.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/engine/fwd-tdme.h>
8 #include <tdme/engine/Frustum.h>
9 #include <tdme/math/fwd-tdme.h>
10 #include <tdme/math/Matrix4x4.h>
11 #include <tdme/math/Vector3.h>
12 
13 using std::unique_ptr;
14 
19 
20 /**
21  * Camera
22  * @author Andreas Drewke
23  */
25 {
26 public:
29 
30 private:
32  Renderer* renderer { nullptr };
33  int32_t width;
34  int32_t height;
35  float fovX;
36  float zNear;
37  float zFar;
50  unique_ptr<Frustum> frustum;
51 
52 public:
53  // forbid class copy
55 
56  /**
57  * Public constructor
58  * @param renderer renderer
59  */
61 
62  /**
63  * Destructor
64  */
65  ~Camera();
66 
67  /**
68  * @return width
69  */
70  inline int getWidth() {
71  return width;
72  }
73 
74  /**
75  * @return height
76  */
77  inline int getHeight() {
78  return height;
79  }
80 
81  /**
82  * @return camera mode
83  */
84  inline CameraMode getCameraMode() const {
85  return cameraMode;
86  }
87 
88  /**
89  * Set camera mode
90  * @param camera mode
91  */
93  this->cameraMode = cameraMode;
94  }
95 
96  /**
97  * @return frustum mode
98  */
99  inline FrustumMode getFrustumMode() const {
100  return frustumMode;
101  }
102 
103  /**
104  * Set frustum mode
105  * @param frustum mode
106  */
108  this->frustumMode = frustumMode;
109  }
110 
111  /**
112  * @return orthographic frustum scale
113  */
114  inline float getOrthographicFrustumScale() const {
116  }
117 
118  /**
119  * Set orthographic frustum scale
120  * @param orthographicFrustumScale orthographic frustum scale
121  */
123  this->orthographicFrustumScale = orthographicFrustumScale;
124  }
125 
126  /**
127  * @return field of view X
128  */
129  inline float getFovX() const {
130  return fovX;
131  }
132 
133  /**
134  * Set field of view X
135  * @param fovX field of view X
136  */
137  inline void setFovX(float fovX) {
138  this->fovX = fovX;
139  }
140 
141  /**
142  * @return float
143  */
144  inline float getZNear() const {
145  return zNear;
146  }
147 
148  /**
149  * Set z near
150  * @param zNear zNear
151  */
152  inline void setZNear(float zNear) {
153  this->zNear = zNear;
154  }
155 
156  /**
157  * @return float
158  */
159  inline float getZFar() const {
160  return zFar;
161  }
162 
163  /**
164  * Set z far
165  * @param zFar zFar
166  */
167  inline void setZFar(float zFar) {
168  this->zFar = zFar;
169  }
170 
171  /**
172  * @return up vector
173  */
174  inline const Vector3& getUpVector() const {
175  return upVector;
176  }
177 
178  /**
179  * Set up vector
180  * @param upVector up vector
181  */
182  inline void setUpVector(const Vector3& upVector) {
183  this->upVector = upVector;
184  }
185 
186  /**
187  * @return forward vector
188  */
189  inline const Vector3& getForwardVector() const {
190  return forwardVector;
191  }
192 
193  /**
194  * Set forward vector
195  * @param forwardVector forward vector
196  */
197  inline void setForwardVector(const Vector3& forwardVector) {
198  this->forwardVector = forwardVector;
199  }
200 
201  /**
202  * @return side vector
203  */
204  inline const Vector3& getSideVector() const {
205  return sideVector;
206  }
207 
208  /**
209  * Set side vector
210  * @param sideVector side vector
211  */
212  inline void setSideVector(const Vector3& sideVector) {
213  this->sideVector = sideVector;
214  }
215 
216  /**
217  * @return look from vector
218  */
219  inline const Vector3& getLookFrom() const {
220  return lookFrom;
221  }
222 
223  /**
224  * Set look from
225  * @param lookFrom look from
226  */
227  inline void setLookFrom(const Vector3& lookFrom) {
228  this->lookFrom = lookFrom;
229  }
230 
231  /**
232  * @return look at vector
233  */
234  inline const Vector3& getLookAt() const {
235  return lookAt;
236  }
237 
238  /**
239  * Set look at
240  * @param lookAt look at
241  */
242  inline void setLookAt(const Vector3& lookAt) {
243  this->lookAt = lookAt;
244  }
245 
246  /**
247  * @return camera matrix
248  */
249  inline const Matrix4x4& getCameraMatrix() const {
250  return cameraMatrix;
251  }
252 
253  /**
254  * @return projection matrix
255  */
256  inline const Matrix4x4& getProjectionMatrix() const {
257  return projectionMatrix;
258  }
259 
260  /**
261  * @return model view projection matrix
262  */
263  inline const Matrix4x4& getModelViewProjectionMatrix() const {
264  return mvpMatrix;
265  }
266 
267  /**
268  * @return inverted model view porjection matrix
269  */
271  return mvpInvertedMatrix;
272  }
273 
274  /**
275  * @return frustum
276  */
277  inline Frustum* getFrustum() {
278  return frustum.get();
279  }
280 
281  /**
282  * Computes the up vector for given look from and look at vectors
283  * @param lookFrom look from
284  * @param lookAt look at
285  * @return up vector
286  */
287  static Vector3 computeUpVector(const Vector3& lookFrom, const Vector3& lookAt);
288 
289  /**
290  * Sets up camera while resizing the view port
291  * @param contextIdx context index
292  * @param width width
293  * @param height height
294  */
295  void update(int contextIdx, int32_t width, int32_t height);
296 
297 private:
298 
299  /**
300  * Computes the projection matrix
301  * @return projection matrix
302  */
304 
305  /**
306  * Computes projection matrix for given look from, look at and up vector
307  * @return model view matrix
308  */
310 
311 };
void setFrustumMode(FrustumMode frustumMode)
Set frustum mode.
Definition: Camera.h:107
Matrix4x4 projectionMatrix
Definition: Camera.h:46
Vector3 upVector
Definition: Camera.h:43
FrustumMode getFrustumMode() const
Definition: Camera.h:99
FrustumMode frustumMode
Definition: Camera.h:39
Vector3 lookFrom
Definition: Camera.h:41
const Matrix4x4 & getModelViewProjectionMatrix() const
Definition: Camera.h:263
static STATIC_DLL_IMPEXT Vector3 defaultUp
Definition: Camera.h:31
float getZFar() const
Definition: Camera.h:159
void setSideVector(const Vector3 &sideVector)
Set side vector.
Definition: Camera.h:212
Vector3 sideVector
Definition: Camera.h:45
Vector3 lookAt
Definition: Camera.h:42
int32_t height
Definition: Camera.h:34
const Matrix4x4 & getModelViewProjectionInvertedMatrix() const
Definition: Camera.h:270
float getOrthographicFrustumScale() const
Definition: Camera.h:114
const Vector3 & getLookAt() const
Definition: Camera.h:234
void update(int contextIdx, int32_t width, int32_t height)
Sets up camera while resizing the view port.
Definition: Camera.cpp:158
Camera(Renderer *renderer)
Public constructor.
Definition: Camera.cpp:24
Matrix4x4 mvpInvertedMatrix
Definition: Camera.h:49
void setFovX(float fovX)
Set field of view X.
Definition: Camera.h:137
const Matrix4x4 & getCameraMatrix() const
Definition: Camera.h:249
const Vector3 & getSideVector() const
Definition: Camera.h:204
const Matrix4x4 & getProjectionMatrix() const
Definition: Camera.h:256
void setUpVector(const Vector3 &upVector)
Set up vector.
Definition: Camera.h:182
Matrix4x4 & computeModelViewMatrix()
Computes projection matrix for given look from, look at and up vector.
Definition: Camera.cpp:122
unique_ptr< Frustum > frustum
Definition: Camera.h:50
void setCameraMode(CameraMode cameraMode)
Set camera mode.
Definition: Camera.h:92
void setLookFrom(const Vector3 &lookFrom)
Set look from.
Definition: Camera.h:227
void setOrthographicFrustumScale(float orthographicFrustumScale)
Set orthographic frustum scale.
Definition: Camera.h:122
CameraMode getCameraMode() const
Definition: Camera.h:84
Frustum * getFrustum()
Definition: Camera.h:277
Matrix4x4 mvpMatrix
Definition: Camera.h:48
float getFovX() const
Definition: Camera.h:129
void setForwardVector(const Vector3 &forwardVector)
Set forward vector.
Definition: Camera.h:197
~Camera()
Destructor.
Definition: Camera.cpp:43
static Vector3 computeUpVector(const Vector3 &lookFrom, const Vector3 &lookAt)
Computes the up vector for given look from and look at vectors.
Definition: Camera.cpp:48
const Vector3 & getLookFrom() const
Definition: Camera.h:219
void setZNear(float zNear)
Set z near.
Definition: Camera.h:152
float orthographicFrustumScale
Definition: Camera.h:40
Vector3 forwardVector
Definition: Camera.h:44
@ FRUSTUMMODE_ORTHOGRAPHIC
Definition: Camera.h:27
Matrix4x4 & computeProjectionMatrix()
Computes the projection matrix.
Definition: Camera.cpp:62
void setLookAt(const Vector3 &lookAt)
Set look at.
Definition: Camera.h:242
Renderer * renderer
Definition: Camera.h:32
void setZFar(float zFar)
Set z far.
Definition: Camera.h:167
const Vector3 & getForwardVector() const
Definition: Camera.h:189
const Vector3 & getUpVector() const
Definition: Camera.h:174
float getZNear() const
Definition: Camera.h:144
CameraMode cameraMode
Definition: Camera.h:38
Matrix4x4 cameraMatrix
Definition: Camera.h:47
Frustum class.
Definition: Frustum.h:29
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6