CUGL 1.3
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 MIT 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() {
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) {
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__ */
cugl::Camera::lookAt
void lookAt(float x, float y, float z)
Definition: CUCamera.h:235
cugl::Camera::lookAt
void lookAt(const Vec3 &target)
cugl::Camera::normalizeUp
void normalizeUp()
Definition: CUCamera.h:246
cugl::Mat4::createRotationZ
static Mat4 createRotationZ(float angle)
Definition: CUMat4.h:628
cugl::Camera::transform
void transform(const Mat4 &transform)
Definition: CUCamera.h:432
cugl::Camera
Definition: CUCamera.h:56
cugl::Camera::_direction
Vec3 _direction
Definition: CUCamera.h:62
cugl::Vec3::x
float x
Definition: CUVec3.h:66
cugl::Mat4::createRotationY
static Mat4 createRotationY(float angle)
Definition: CUMat4.h:600
cugl::Vec3::z
float z
Definition: CUVec3.h:70
cugl::Camera::_projection
Mat4 _projection
Definition: CUCamera.h:67
cugl::Camera::setFar
void setFar(float value)
Definition: CUCamera.h:164
cugl::Camera::Camera
Camera()
cugl::Camera::screenToWorldCoords
Vec3 screenToWorldCoords(const Vec2 &screenCoords) const
Definition: CUCamera.h:497
cugl::Camera::rotateX
void rotateX(float angle)
Definition: CUCamera.h:292
cugl::Camera::_upwards
Vec3 _upwards
Definition: CUCamera.h:64
cugl::Camera::rotateAround
void rotateAround(const Vec3 &point, const Vec3 &axis, float angle)
cugl::Camera::translate
void translate(const Vec2 &vec)
Definition: CUCamera.h:390
cugl::Camera::update
virtual void update()=0
cugl::Camera::_far
float _far
Definition: CUCamera.h:78
cugl::Mat4::transformVector
static Vec2 * transformVector(const Mat4 &mat, const Vec2 &vec, Vec2 *dst)
cugl::Mat4::createRotationX
static Mat4 createRotationX(float angle)
Definition: CUMat4.h:572
cugl::Camera::rotateY
void rotateY(float angle)
Definition: CUCamera.h:307
cugl::Vec2::x
float x
Definition: CUVec2.h:66
cugl::Camera::rotate
void rotate(const Mat4 &transform)
Definition: CUCamera.h:351
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::Camera::setNear
void setNear(float value)
Definition: CUCamera.h:144
cugl::Camera::getPickRay
Ray getPickRay(const Vec3 &windowCoords) const
cugl::Camera::windowToScreenCoords
Vec2 windowToScreenCoords(const Vec3 &windowCoords) const
cugl::Camera::project
Vec3 project(const Vec3 &worldCoords) const
cugl::Camera::rotate
void rotate(float angle)
Definition: CUCamera.h:337
cugl::Camera::translate
void translate(float tx, float ty)
Definition: CUCamera.h:419
cugl::Camera::getDirection
const Vec3 & getDirection() const
Definition: CUCamera.h:120
cugl::Camera::worldToScreenCoords
Vec2 worldToScreenCoords(const Vec3 &worldCoords) const
Definition: CUCamera.h:553
cugl::Camera::getViewport
const Rect & getViewport() const
Definition: CUCamera.h:178
cugl::Vec2::y
float y
Definition: CUVec2.h:68
cugl::Camera::rotate
void rotate(const Vec3 &axis, float angle)
Definition: CUCamera.h:277
cugl::Camera::getProjection
const Mat4 & getProjection() const
Definition: CUCamera.h:185
cugl::Camera::getPickRayFromScreen
Ray getPickRayFromScreen(const Vec2 &screenCoords) const
Definition: CUCamera.h:617
cugl::Camera::rotateZ
void rotateZ(float angle)
Definition: CUCamera.h:322
cugl::Mat4::createRotation
static Mat4 createRotation(const Quaternion &quat)
Definition: CUMat4.h:517
cugl::Vec3::y
float y
Definition: CUVec3.h:68
cugl::Vec2
Definition: CUVec2.h:61
cugl::Camera::_combined
Mat4 _combined
Definition: CUCamera.h:71
cugl::Camera::rotate
void rotate(const Quaternion &quat)
Definition: CUCamera.h:261
cugl::Camera::getView
const Mat4 & getView() const
Definition: CUCamera.h:192
cugl::Camera::_position
Vec3 _position
Definition: CUCamera.h:60
cugl::Camera::_near
float _near
Definition: CUCamera.h:76
cugl::Camera::getCombined
const Mat4 & getCombined() const
Definition: CUCamera.h:199
cugl::Camera::dispose
virtual void dispose()
cugl::Camera::unproject
Vec3 unproject(const Vec3 &windowCoords) const
cugl::Quaternion
Definition: CUQuaternion.h:97
cugl::Camera::translate
void translate(float tx, float ty, float tz)
Definition: CUCamera.h:404
cugl::Ray
Definition: CURay.h:45
cugl::Camera::getInverseProjectView
const Mat4 & getInverseProjectView() const
Definition: CUCamera.h:206
cugl::Camera::~Camera
~Camera()
Definition: CUCamera.h:96
cugl::Vec3
Definition: CUVec3.h:61
cugl::Vec3::cross
Vec3 & cross(const Vec3 &v)
Definition: CUVec3.h:1002
cugl::Camera::_modelview
Mat4 _modelview
Definition: CUCamera.h:69
cugl::Camera::_viewport
Rect _viewport
Definition: CUCamera.h:81
cugl::Camera::getNear
float getNear() const
Definition: CUCamera.h:134
cugl::Camera::screenToWindowCoords
Vec3 screenToWindowCoords(const Vec2 &screenCoords) const
cugl::Vec3::normalize
Vec3 & normalize()
cugl::Camera::translate
void translate(const Vec3 &vec)
Definition: CUCamera.h:378
cugl::Camera::getPosition
const Vec3 & getPosition() const
Definition: CUCamera.h:113
cugl::Camera::_inverse
Mat4 _inverse
Definition: CUCamera.h:73
cugl::Camera::getUp
const Vec3 & getUp() const
Definition: CUCamera.h:127
cugl::Camera::getFar
float getFar() const
Definition: CUCamera.h:154