CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUCamera.h
1 //
2 // CUCamera.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the abstract base class for our camera classes.
6 // Because it is abstract, it has only a basic constructor. It has no
7 // initializers or allocator.
8 //
9 // This module is based on the original file Camera.java by Mario Zechner,
10 // written for LibGDX ( https://libgdx.badlogicgames.com ). It has been
11 // modified to support the CUGL framework.
12 //
13 // CUGL zlib License:
14 // This software is provided 'as-is', without any express or implied
15 // warranty. In no event will the authors be held liable for any damages
16 // arising from the use of this software.
17 //
18 // Permission is granted to anyone to use this software for any purpose,
19 // including commercial applications, and to alter it and redistribute it
20 // freely, subject to the following restrictions:
21 //
22 // 1. The origin of this software must not be misrepresented; you must not
23 // claim that you wrote the original software. If you use this software
24 // in a product, an acknowledgment in the product documentation would be
25 // appreciated but is not required.
26 //
27 // 2. Altered source versions must be plainly marked as such, and must not
28 // be misrepresented as being the original software.
29 //
30 // 3. This notice may not be removed or altered from any source distribution.
31 //
32 // Author: Walker White
33 // Version: 6/24/16
34 
35 #ifndef __CU_CAMERA_H__
36 #define __CU_CAMERA_H__
37 
38 #include <cugl/math/CURect.h>
39 #include <cugl/math/CUVec2.h>
40 #include <cugl/math/CUVec3.h>
41 #include <cugl/math/CUMat4.h>
42 #include <cugl/math/CUQuaternion.h>
43 #include <cugl/math/CURay.h>
44 #include <cugl/util/CUDebug.h>
45 
46 namespace cugl {
47 
56 class Camera {
57 #pragma mark Values
58 protected:
65 
74 
76  float _near;
78  float _far;
79 
82 
83 #pragma mark -
84 #pragma mark Constructor
85 public:
91  Camera();
92 
96  ~Camera() { dispose(); }
97 
103  virtual void dispose();
104 
105 
106 #pragma mark -
107 #pragma mark Attributes
108 
113  const Vec3& getPosition() const { return _position; }
114 
120  const Vec3& getDirection() const { return _direction; }
121 
127  const Vec3& getUp() const { return _upwards; }
128 
134  float getNear() const { return _near; }
135 
144  void setNear(float value) {
145  CUAssertLog(value >= 0, "Value is not positive");
146  _near = value;
147  }
148 
154  float getFar() const { return _far; }
155 
164  void setFar(float value) {
165  CUAssertLog(value >= 0, "Value is not positive");
166  _far = value;
167  }
168 
178  const Rect& getViewport() const { return _viewport; }
179 
185  const Mat4& getProjection() const { return _projection; }
186 
192  const Mat4& getView() const { return _modelview; }
193 
199  const Mat4& getCombined() const { return _combined; }
200 
206  const Mat4& getInverseProjectView() const { return _inverse; }
207 
208 #pragma mark -
209 #pragma mark Updates
210 
215  virtual void update() = 0;
216 
224  void lookAt(const Vec3& target);
225 
235  void lookAt (float x, float y, float z) {
236  lookAt(Vec3(x,y,z));
237  }
238 
246  void normalizeUp() {
247  _upwards = _direction.cross(_upwards).normalize();
248  _upwards.cross(_direction).normalize();
249  }
250 
251 #pragma mark -
252 #pragma mark View Transforms
253 
261  void rotate(const Quaternion& quat) {
262  Mat4 temp;
263  Mat4::createRotation(quat,&temp);
264  _direction *= temp;
265  _upwards *= temp;
266  }
267 
277  void rotate(const Vec3& axis, float angle) {
278  Mat4 temp;
279  Mat4::createRotation(axis,angle,&temp);
280  _direction *= temp;
281  _upwards *= temp;
282  }
283 
292  void rotateX(float angle) {
293  Mat4 temp;
294  Mat4::createRotationX(angle,&temp);
295  _direction *= temp;
296  _upwards *= temp;
297  }
298 
307  void rotateY(float angle) {
308  Mat4 temp;
309  Mat4::createRotationY(angle,&temp);
310  _direction *= temp;
311  _upwards *= temp;
312  }
313 
322  void rotateZ(float angle) {
323  Mat4 temp;
324  Mat4::createRotationZ(angle,&temp);
325  _direction *= temp;
326  _upwards *= temp;
327  }
328 
337  void rotate(float angle) {
338  rotate(_direction, angle);
339  }
340 
351  void rotate(const Mat4& transform) {
352  Mat4::transformVector(transform, _direction, &_direction);
353  _direction.normalize();
354  Mat4::transformVector(transform, _upwards, &_upwards);
355  _upwards.normalize();
356  }
357 
371  void rotateAround(const Vec3& point, const Vec3& axis, float angle);
372 
378  void translate(const Vec3& vec) {
379  _position += vec;
380  }
381 
390  void translate (const Vec2& vec) {
391  translate(vec.x,vec.y);
392  }
393 
404  void translate(float tx, float ty, float tz) {
405  _position.x += tx;
406  _position.y += ty;
407  _position.z += tz;
408  }
409 
419  void translate (float tx, float ty) {
420  _position.x += tx;
421  _position.y += ty;
422  }
423 
432  void transform(const Mat4& transform) {
433  _position *= transform;
434  rotate(transform);
435  }
436 
437 #pragma mark -
438 #pragma mark Coordinate Transforms
439 
456  Vec3 unproject(const Vec3& windowCoords) const;
457 
476  Vec3 unproject(const Vec3& windowCoords, const Rect& viewport) const;
477 
497  Vec3 screenToWorldCoords(const Vec2& screenCoords) const {
498  return unproject(screenToWindowCoords(screenCoords));
499  }
500 
517  Vec3 project(const Vec3& worldCoords) const;
518 
536  Vec3 project(const Vec3& worldCoords, const Rect& viewport) const;
537 
553  Vec2 worldToScreenCoords(const Vec3& worldCoords) const {
554  return windowToScreenCoords(project(worldCoords));
555  }
556 
577  Ray getPickRay(const Vec3& windowCoords) const;
578 
600  Ray getPickRay(const Vec3& windowCoords, const Rect& viewport) const;
601 
617  Ray getPickRayFromScreen(const Vec2& screenCoords) const {
618  return getPickRay(screenToWindowCoords(screenCoords));
619  }
620 
634  Vec2 windowToScreenCoords(const Vec3& windowCoords) const;
635 
650  Vec2 windowToScreenCoords(const Vec3& windowCoords, const Rect& viewport) const;
651 
669  Vec3 screenToWindowCoords(const Vec2& screenCoords) const;
670 
689  Vec3 screenToWindowCoords(const Vec2& screenCoords, const Rect& viewport) const;
690 };
691 
692 }
693 
694 #endif /* __CU_CAMERA_H__ */
static Mat4 createRotationX(float angle)
Definition: CUMat4.h:573
void translate(float tx, float ty)
Definition: CUCamera.h:419
void translate(const Vec3 &vec)
Definition: CUCamera.h:378
float x
Definition: CUVec2.h:66
float y
Definition: CUVec2.h:68
const Mat4 & getProjection() const
Definition: CUCamera.h:185
Vec3 & cross(const Vec3 &v)
Definition: CUVec3.h:1025
float x
Definition: CUVec3.h:66
void rotateZ(float angle)
Definition: CUCamera.h:322
~Camera()
Definition: CUCamera.h:96
float _near
Definition: CUCamera.h:76
Definition: CUVec2.h:61
Vec3 & normalize()
Mat4 _modelview
Definition: CUCamera.h:69
float z
Definition: CUVec3.h:70
static Vec2 * transformVector(const Mat4 &mat, const Vec2 &vec, Vec2 *dst)
void translate(float tx, float ty, float tz)
Definition: CUCamera.h:404
static Mat4 createRotationY(float angle)
Definition: CUMat4.h:601
const Vec3 & getDirection() const
Definition: CUCamera.h:120
Mat4 _projection
Definition: CUCamera.h:67
Ray getPickRay(const Vec3 &windowCoords) const
void translate(const Vec2 &vec)
Definition: CUCamera.h:390
Vec2 worldToScreenCoords(const Vec3 &worldCoords) const
Definition: CUCamera.h:553
Vec3 _upwards
Definition: CUCamera.h:64
void transform(const Mat4 &transform)
Definition: CUCamera.h:432
float _far
Definition: CUCamera.h:78
Ray getPickRayFromScreen(const Vec2 &screenCoords) const
Definition: CUCamera.h:617
Vec3 screenToWorldCoords(const Vec2 &screenCoords) const
Definition: CUCamera.h:497
void rotate(const Mat4 &transform)
Definition: CUCamera.h:351
const Mat4 & getView() const
Definition: CUCamera.h:192
Vec3 _direction
Definition: CUCamera.h:62
void rotateAround(const Vec3 &point, const Vec3 &axis, float angle)
virtual void update()=0
const Vec3 & getUp() const
Definition: CUCamera.h:127
float getFar() const
Definition: CUCamera.h:154
void rotateX(float angle)
Definition: CUCamera.h:292
const Vec3 & getPosition() const
Definition: CUCamera.h:113
Vec3 _position
Definition: CUCamera.h:60
void lookAt(const Vec3 &target)
Vec2 windowToScreenCoords(const Vec3 &windowCoords) const
void rotate(const Quaternion &quat)
Definition: CUCamera.h:261
const Rect & getViewport() const
Definition: CUCamera.h:178
Vec3 screenToWindowCoords(const Vec2 &screenCoords) const
Definition: CURect.h:45
Vec3 unproject(const Vec3 &windowCoords) const
void rotate(float angle)
Definition: CUCamera.h:337
void lookAt(float x, float y, float z)
Definition: CUCamera.h:235
Vec3 project(const Vec3 &worldCoords) const
void rotate(const Vec3 &axis, float angle)
Definition: CUCamera.h:277
const Mat4 & getCombined() const
Definition: CUCamera.h:199
void setNear(float value)
Definition: CUCamera.h:144
void setFar(float value)
Definition: CUCamera.h:164
Definition: CURay.h:45
Definition: CUVec3.h:61
Mat4 _inverse
Definition: CUCamera.h:73
Definition: CUCamera.h:56
static Mat4 createRotation(const Quaternion &quat)
Definition: CUMat4.h:518
void normalizeUp()
Definition: CUCamera.h:246
Rect _viewport
Definition: CUCamera.h:81
float y
Definition: CUVec3.h:68
void rotateY(float angle)
Definition: CUCamera.h:307
virtual void dispose()
const Mat4 & getInverseProjectView() const
Definition: CUCamera.h:206
float getNear() const
Definition: CUCamera.h:134
Definition: CUAnimationNode.h:52
Definition: CUQuaternion.h:97
Definition: CUMat4.h:92
static Mat4 createRotationZ(float angle)
Definition: CUMat4.h:629
Mat4 _combined
Definition: CUCamera.h:71