Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Camera.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Camera_INCLUDE_ONCE
33 #define Camera_INCLUDE_ONCE
34 
35 #include <vlCore/checks.hpp>
36 #include <vlCore/math_utils.hpp>
37 #include <vlCore/Vector4.hpp>
39 #include <vlGraphics/Viewport.hpp>
40 #include <vlGraphics/Frustum.hpp>
41 #include <vlCore/Ray.hpp>
42 
43 namespace vl
44 {
45  class AABB;
46  //-----------------------------------------------------------------------------
47  // Camera
48  //-----------------------------------------------------------------------------
51  {
53 
54  public:
56  Camera();
57 
65  void computeNearFarOptimizedProjMatrix(const Sphere& scene_bounding_sphere);
66 
70  void computeFrustumPlanes();
71 
73  void applyModelViewMatrix(const mat4& model_matrix) const;
74 
76  void applyViewMatrix() const;
77 
79  void applyProjMatrix() const;
80 
83  real aspectRatio() const
84  {
85  if (viewport())
86  return (real)viewport()->width()/viewport()->height();
87  else
88  return 0;
89  }
90 
93  void setFOV(real fov) { mFOV = fov; }
94 
96  real fov() const { return mFOV; }
97 
100  void setNearPlane(real nearplane) { mNearPlane = nearplane; }
101 
103  real nearPlane() const { return mNearPlane; }
104 
107  void setFarPlane(real farplane) { mFarPlane = farplane; }
108 
110  real farPlane() const { return mFarPlane; }
111 
113  real left() const { return mLeft; }
114  void setLeft(real v) { mLeft = v; }
115 
117  real right() const { return mRight; }
118  void setRight(real v) { mRight = v; }
119 
121  real bottom() const { return mBottom; }
122  void setBottom(real v) { mBottom = v; }
123 
125  real top() const { return mTop; }
126  void setTop(real v) { mTop = v; }
127 
129  void setFrustum(const Frustum& frustum) { mFrustum = frustum; }
130 
132  const Frustum& frustum() const { return mFrustum; }
133 
135  Frustum& frustum() { return mFrustum; }
136 
138  void setViewport(Viewport* viewport) { mViewport = viewport; }
139 
141  Viewport* viewport() { return mViewport.get(); }
142 
144  const Viewport* viewport() const { return mViewport.get(); }
145 
147  void bindTransform(Transform* transform) { mBoundTransform = transform; }
148 
150  const Transform* boundTransform() const { return mBoundTransform.get(); }
151 
153  Transform* boundTransform() { return mBoundTransform.get(); }
154 
157  void setViewMatrix(const mat4& mat) { mViewMatrix = mat; mViewMatrix.getInverse(mModelingMatrix); }
158 
161  const mat4& viewMatrix() const { return mViewMatrix; }
162 
165  void setModelingMatrix(const mat4& mat) { mModelingMatrix = mat; mModelingMatrix.getInverse(mViewMatrix); }
166 
169  const mat4& modelingMatrix() const { return mModelingMatrix; }
170 
172  void setProjectionMatrix(const mat4& mat, EProjectionMatrixType proj_type) { mProjectionMatrix = mat; mProjectionType = proj_type; }
173 
175  const mat4& projectionMatrix() const { return mProjectionMatrix; }
176 
178  EProjectionMatrixType projectionMatrixType() const { return mProjectionType; }
179 
182  void setProjectionPerspective();
183 
186  void setProjectionPerspective(real fov, real near, real far);
187 
198  void setProjectionFrustum(real left, real right, real bottom, real top, real znear, real zfar);
199 
206  void setProjectionOrtho(real left, real right, real bottom, real top, real znear, real zfar);
207 
214  void setProjectionOrtho();
215 
224  void setProjectionOrtho(real offset);
225 
230  void setViewMatrixLookAt(const vec3& eye, const vec3& at, const vec3& up);
231 
237  void getViewMatrixAsLookAt(vec3& eye, vec3& at, vec3& up, vec3& right) const;
238 
240  bool project(const vec4& in_world, vec4& out_viewp) const;
241 
248  bool unproject(const vec3& in_viewp, vec4& out_world) const;
249 
251  bool unproject(std::vector<vec3>& points) const;
252 
260  Ray computeRay(int viewp_x, int viewp_y);
261 
263  Frustum computeRayFrustum(int viewp_x, int viewp_y);
264 
270  void adjustView(const AABB& aabb, const vec3& dir, const vec3& up, real bias=1.0f);
271 
272  protected:
279  real mFOV;
280  real mLeft, mRight, mBottom, mTop;
282  real mFarPlane;
284  };
285 }
286 
287 #endif
EProjectionMatrixType projectionMatrixType() const
The Camera&#39;s projection matrix type.
Definition: Camera.hpp:178
void setNearPlane(real nearplane)
The near clipping plane.
Definition: Camera.hpp:100
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
void setViewport(Viewport *viewport)
The viewport bound to a camera.
Definition: Camera.hpp:138
const Transform * boundTransform() const
Returns the Transform bound to a camera.
Definition: Camera.hpp:150
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
mat4 mViewMatrix
Definition: Camera.hpp:273
real mTop
Definition: Camera.hpp:280
The Ray class defines a ray as an origin and direction using real precision.
Definition: Ray.hpp:41
ref< Viewport > mViewport
Definition: Camera.hpp:276
A set of planes defining a frustum used for culling purposes (frustum culling).
Definition: Frustum.hpp:49
const Viewport * viewport() const
The viewport bound to a camera.
Definition: Camera.hpp:144
real mFarPlane
Definition: Camera.hpp:282
mat4 mProjectionMatrix
Definition: Camera.hpp:275
void setFarPlane(real farplane)
The far clipping plane.
Definition: Camera.hpp:107
ref< Transform > mBoundTransform
Definition: Camera.hpp:278
void setLeft(real v)
Definition: Camera.hpp:114
real left() const
&#39;left&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:113
real nearPlane() const
The near clipping plane.
Definition: Camera.hpp:103
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
EProjectionMatrixType mProjectionType
Definition: Camera.hpp:283
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:141
Visualization Library main namespace.
Frustum mFrustum
Definition: Camera.hpp:277
const Frustum & frustum() const
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:132
T_Scalar getInverse(Matrix4 &dest) const
Definition: Matrix4.hpp:1009
real mNearPlane
Definition: Camera.hpp:281
EProjectionMatrixType
real fov() const
The field of view of the camera.
Definition: Camera.hpp:96
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
real top() const
&#39;top&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:125
The base class for all the reference counted objects.
Definition: Object.hpp:158
const mat4 & modelingMatrix() const
Returns the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:169
Frustum & frustum()
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:135
Implements the viewport and clearing settings associated to a Camera.
Definition: Viewport.hpp:51
real farPlane() const
The far clipping plane.
Definition: Camera.hpp:110
real bottom() const
&#39;bottom&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:121
void setRight(real v)
Definition: Camera.hpp:118
void setProjectionMatrix(const mat4 &mat, EProjectionMatrixType proj_type)
The Camera&#39;s projection matrix.
Definition: Camera.hpp:172
void setModelingMatrix(const mat4 &mat)
Sets the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:165
void setViewMatrix(const mat4 &mat)
Sets the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:157
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
mat4 mModelingMatrix
Definition: Camera.hpp:274
void setFrustum(const Frustum &frustum)
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:129
void bindTransform(Transform *transform)
Bind the camera to a Transform.
Definition: Camera.hpp:147
const mat4 & projectionMatrix() const
The Camera&#39;s projection matrix.
Definition: Camera.hpp:175
Transform * boundTransform()
Returns the Transform bound to a camera.
Definition: Camera.hpp:153
void setBottom(real v)
Definition: Camera.hpp:122
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
real right() const
&#39;right&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:117
void setFOV(real fov)
The field of view of the camera.
Definition: Camera.hpp:93
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
real aspectRatio() const
Returns the aspect ratio computed as viewport()->width()/viewport()->height().
Definition: Camera.hpp:83
void setTop(real v)
Definition: Camera.hpp:126
real mFOV
Definition: Camera.hpp:279