Visualization Library 2.1.0

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-2020, 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>
38 #include <vlGraphics/Viewport.hpp>
39 #include <vlGraphics/Frustum.hpp>
40 #include <vlCore/Ray.hpp>
41 
42 namespace vl
43 {
44  class AABB;
45  //-----------------------------------------------------------------------------
46  // Camera
47  //-----------------------------------------------------------------------------
50  {
52 
53  public:
55  Camera();
56 
64  void computeNearFarOptimizedProjMatrix(const Sphere& scene_bounding_sphere);
65 
69  void computeFrustumPlanes();
70 
72  void applyModelViewMatrix(const mat4& model_matrix) const;
73 
75  void applyViewMatrix() const;
76 
78  void applyProjMatrix() const;
79 
82  real aspectRatio() const
83  {
84  if (viewport())
85  return (real)viewport()->width()/viewport()->height();
86  else
87  return 0;
88  }
89 
92  void setFOV(real fov) { mFOV = fov; }
93 
95  real fov() const { return mFOV; }
96 
99  void setNearPlane(real nearplane) { mNearPlane = nearplane; }
100 
102  real nearPlane() const { return mNearPlane; }
103 
106  void setFarPlane(real farplane) { mFarPlane = farplane; }
107 
109  real farPlane() const { return mFarPlane; }
110 
112  real left() const { return mLeft; }
113  void setLeft(real v) { mLeft = v; }
114 
116  real right() const { return mRight; }
117  void setRight(real v) { mRight = v; }
118 
120  real bottom() const { return mBottom; }
121  void setBottom(real v) { mBottom = v; }
122 
124  real top() const { return mTop; }
125  void setTop(real v) { mTop = v; }
126 
128  void setFrustum(const Frustum& frustum) { mFrustum = frustum; }
129 
131  const Frustum& frustum() const { return mFrustum; }
132 
134  Frustum& frustum() { return mFrustum; }
135 
137  void setViewport(Viewport* viewport) { mViewport = viewport; }
138 
140  Viewport* viewport() { return mViewport.get(); }
141 
143  const Viewport* viewport() const { return mViewport.get(); }
144 
146  void bindTransform(Transform* transform) { mBoundTransform = transform; }
147 
149  const Transform* boundTransform() const { return mBoundTransform.get(); }
150 
152  Transform* boundTransform() { return mBoundTransform.get(); }
153 
156  void setViewMatrix(const mat4& mat) { mViewMatrix = mat; mViewMatrix.getInverse(mModelingMatrix); }
157 
160  const mat4& viewMatrix() const { return mViewMatrix; }
161 
164  void setModelingMatrix(const mat4& mat) { mModelingMatrix = mat; mModelingMatrix.getInverse(mViewMatrix); }
165 
168  const mat4& modelingMatrix() const { return mModelingMatrix; }
169 
171  void setProjectionMatrix(const mat4& mat, EProjectionMatrixType proj_type) { mProjectionMatrix = mat; mProjectionType = proj_type; }
172 
174  const mat4& projectionMatrix() const { return mProjectionMatrix; }
175 
177  EProjectionMatrixType projectionMatrixType() const { return mProjectionType; }
178 
181  void setProjectionPerspective();
182 
185  void setProjectionPerspective(real fov, real near, real far);
186 
197  void setProjectionFrustum(real left, real right, real bottom, real top, real znear, real zfar);
198 
205  void setProjectionOrtho(real left, real right, real bottom, real top, real znear, real zfar);
206 
213  void setProjectionOrtho();
214 
223  void setProjectionOrtho(real offset);
224 
229  void setViewMatrixLookAt(const vec3& eye, const vec3& at, const vec3& up);
230 
236  void getViewMatrixAsLookAt(vec3& eye, vec3& at, vec3& up, vec3& right) const;
237 
239  bool project(const vec4& in_world, vec4& out_viewp) const;
240 
247  bool unproject(const vec3& in_viewp, vec4& out_world) const;
248 
250  bool unproject(std::vector<vec3>& points) const;
251 
259  Ray computeRay(int viewp_x, int viewp_y);
260 
262  Frustum computeRayFrustum(int viewp_x, int viewp_y);
263 
269  void adjustView(const AABB& aabb, const vec3& dir, const vec3& up, real bias=1.0f);
270 
271  protected:
278  real mFOV;
279  real mLeft, mRight, mBottom, mTop;
281  real mFarPlane;
283  };
284 }
285 
286 #endif
EProjectionMatrixType projectionMatrixType() const
The Camera&#39;s projection matrix type.
Definition: Camera.hpp:177
void setNearPlane(real nearplane)
The near clipping plane.
Definition: Camera.hpp:99
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:160
void setViewport(Viewport *viewport)
The viewport bound to a camera.
Definition: Camera.hpp:137
const Transform * boundTransform() const
Returns the Transform bound to a camera.
Definition: Camera.hpp:149
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
mat4 mViewMatrix
Definition: Camera.hpp:272
real mTop
Definition: Camera.hpp:279
The Ray class defines a ray as an origin and direction using real precision.
Definition: Ray.hpp:41
ref< Viewport > mViewport
Definition: Camera.hpp:275
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:143
real mFarPlane
Definition: Camera.hpp:281
mat4 mProjectionMatrix
Definition: Camera.hpp:274
void setFarPlane(real farplane)
The far clipping plane.
Definition: Camera.hpp:106
ref< Transform > mBoundTransform
Definition: Camera.hpp:277
void setLeft(real v)
Definition: Camera.hpp:113
real left() const
&#39;left&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:112
real nearPlane() const
The near clipping plane.
Definition: Camera.hpp:102
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
EProjectionMatrixType mProjectionType
Definition: Camera.hpp:282
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:140
Visualization Library main namespace.
Frustum mFrustum
Definition: Camera.hpp:276
const Frustum & frustum() const
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:131
T_Scalar getInverse(Matrix4 &dest) const
Definition: Matrix4.hpp:1014
real mNearPlane
Definition: Camera.hpp:280
EProjectionMatrixType
real fov() const
The field of view of the camera.
Definition: Camera.hpp:95
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:124
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:168
Frustum & frustum()
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:134
Implements the viewport and clearing settings associated to a Camera.
Definition: Viewport.hpp:51
real farPlane() const
The far clipping plane.
Definition: Camera.hpp:109
real bottom() const
&#39;bottom&#39; parameter as passed to the last setProjectionFrustum() or setProjectionOrtho*() ...
Definition: Camera.hpp:120
void setRight(real v)
Definition: Camera.hpp:117
void setProjectionMatrix(const mat4 &mat, EProjectionMatrixType proj_type)
The Camera&#39;s projection matrix.
Definition: Camera.hpp:171
void setModelingMatrix(const mat4 &mat)
Sets the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:164
void setViewMatrix(const mat4 &mat)
Sets the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:156
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:273
void setFrustum(const Frustum &frustum)
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:128
void bindTransform(Transform *transform)
Bind the camera to a Transform.
Definition: Camera.hpp:146
const mat4 & projectionMatrix() const
The Camera&#39;s projection matrix.
Definition: Camera.hpp:174
Transform * boundTransform()
Returns the Transform bound to a camera.
Definition: Camera.hpp:152
void setBottom(real v)
Definition: Camera.hpp:121
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:116
void setFOV(real fov)
The field of view of the camera.
Definition: Camera.hpp:92
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
real aspectRatio() const
Returns the aspect ratio computed as viewport()->width()/viewport()->height().
Definition: Camera.hpp:82
void setTop(real v)
Definition: Camera.hpp:125
real mFOV
Definition: Camera.hpp:278