NGL  6.5
The NCCA Graphics Library
Camera.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef CAMERA_H_
18 #define CAMERA_H_
19 // must include types.h first for Real and GLEW if required
22 #include "Types.h"
23 #include "Vec4.h"
24 #include "Vec3.h"
25 #include "Mat4.h"
26 #include <cmath>
27 #include "RibExport.h"
28 #include "Plane.h"
29 #include "AABB.h"
30 
31 
32 namespace ngl
33 {
34 //----------------------------------------------------------------------------------------------------------------------
36 enum class CameraIntercept : char {OUTSIDE, INTERSECT, INSIDE};
37 
38 //----------------------------------------------------------------------------------------------------------------------
52 //----------------------------------------------------------------------------------------------------------------------
53 
55 {
56 
57 public :
58 
59  //----------------------------------------------------------------------------------------------------------------------
61  //----------------------------------------------------------------------------------------------------------------------
62  Camera() noexcept;
63 
64  //----------------------------------------------------------------------------------------------------------------------
69  //----------------------------------------------------------------------------------------------------------------------
70  Camera(const Vec3& _eye, const Vec3& _look,const Vec3& _up) noexcept;
71 
72  //----------------------------------------------------------------------------------------------------------------------
75  //----------------------------------------------------------------------------------------------------------------------
76  void roll(Real _angle) noexcept;
77  //----------------------------------------------------------------------------------------------------------------------
80  //----------------------------------------------------------------------------------------------------------------------
81  void pitch(Real _angle) noexcept;
82  //----------------------------------------------------------------------------------------------------------------------
85  //----------------------------------------------------------------------------------------------------------------------
86  void yaw(Real _angle) noexcept;
87 
88  //----------------------------------------------------------------------------------------------------------------------
93  //----------------------------------------------------------------------------------------------------------------------
94  void slide(Real _du,Real _dv,Real _dn) noexcept;
95 
96  //----------------------------------------------------------------------------------------------------------------------
101  //----------------------------------------------------------------------------------------------------------------------
102  void set(const Vec3 &_eye, const Vec3 &_look, const Vec3 &_up) noexcept;
103  //----------------------------------------------------------------------------------------------------------------------
109  //----------------------------------------------------------------------------------------------------------------------
110  void setShape(Real _viewAngle, Real _aspect, Real _near, Real _far) noexcept;
111  //----------------------------------------------------------------------------------------------------------------------
114  //----------------------------------------------------------------------------------------------------------------------
115  void setAspect(Real _asp) noexcept;
116 
117  //----------------------------------------------------------------------------------------------------------------------
119  //----------------------------------------------------------------------------------------------------------------------
120  void setDefaultCamera() noexcept;
121 
122  //----------------------------------------------------------------------------------------------------------------------
127  //----------------------------------------------------------------------------------------------------------------------
128  void move(Real _dx,Real _dy,Real _dz) noexcept;
129 
130  //----------------------------------------------------------------------------------------------------------------------
135  //----------------------------------------------------------------------------------------------------------------------
136  void moveBoth( Real _dx, Real _dy, Real _dz) noexcept;
137 
138  //----------------------------------------------------------------------------------------------------------------------
143  //----------------------------------------------------------------------------------------------------------------------
144  void moveEye(Real _dx, Real _dy, Real _dz) noexcept;
145  //----------------------------------------------------------------------------------------------------------------------
150  //----------------------------------------------------------------------------------------------------------------------
151  void moveLook(Real _dx, Real _dy, Real _dz) noexcept;
152  //----------------------------------------------------------------------------------------------------------------------
155  //----------------------------------------------------------------------------------------------------------------------
156  void setViewAngle(Real _angle) noexcept;
157  //----------------------------------------------------------------------------------------------------------------------
159  //----------------------------------------------------------------------------------------------------------------------
160  void update() noexcept;
161  //----------------------------------------------------------------------------------------------------------------------
164  //----------------------------------------------------------------------------------------------------------------------
165  void normalisedYaw(Real _angle) noexcept;
166 
167  //----------------------------------------------------------------------------------------------------------------------
170  //----------------------------------------------------------------------------------------------------------------------
171  void normalisedPitch(Real _angle) noexcept;
172 
173  //----------------------------------------------------------------------------------------------------------------------
176  //----------------------------------------------------------------------------------------------------------------------
177  void normalisedRoll(Real _angle) noexcept;
178 
179  //----------------------------------------------------------------------------------------------------------------------
184  //----------------------------------------------------------------------------------------------------------------------
185  void writeRib(RibExport &_rib )const noexcept;
186  //----------------------------------------------------------------------------------------------------------------------
189  //----------------------------------------------------------------------------------------------------------------------
190  const Mat4 & getViewMatrix() noexcept{return m_viewMatrix;}
191  //----------------------------------------------------------------------------------------------------------------------
194  //----------------------------------------------------------------------------------------------------------------------
195  const Mat4 & getProjectionMatrix() noexcept{return m_projectionMatrix;}
196  //----------------------------------------------------------------------------------------------------------------------
199  //----------------------------------------------------------------------------------------------------------------------
200  Mat4 getVPMatrix()const noexcept{return m_viewMatrix*m_projectionMatrix;}
201  //----------------------------------------------------------------------------------------------------------------------
204  //----------------------------------------------------------------------------------------------------------------------
205  Vec4 getEye() const noexcept{return m_eye;}
206  //----------------------------------------------------------------------------------------------------------------------
208  //----------------------------------------------------------------------------------------------------------------------
209  void setEye(Vec4 _e) noexcept{m_eye=_e; update();}
210  //----------------------------------------------------------------------------------------------------------------------
212  //----------------------------------------------------------------------------------------------------------------------
213  void setLook(Vec4 _e) noexcept{m_look=_e; update();}
214  //----------------------------------------------------------------------------------------------------------------------
215  //----------------------------------------------------------------------------------------------------------------------
218  //----------------------------------------------------------------------------------------------------------------------
219  Vec4 getLook() const noexcept{return m_look;}
220  //----------------------------------------------------------------------------------------------------------------------
223  //----------------------------------------------------------------------------------------------------------------------
224  Vec4 getUp() const noexcept{return m_up;}
225  //----------------------------------------------------------------------------------------------------------------------
228  //----------------------------------------------------------------------------------------------------------------------
229  Vec4 getU() const noexcept{return m_u;}
230  //----------------------------------------------------------------------------------------------------------------------
233  //----------------------------------------------------------------------------------------------------------------------
234  Vec4 getV()const noexcept{return m_v;}
235  //----------------------------------------------------------------------------------------------------------------------
238  //----------------------------------------------------------------------------------------------------------------------
239  Vec4 getN() const noexcept{return m_n;}
240  //----------------------------------------------------------------------------------------------------------------------
243  //----------------------------------------------------------------------------------------------------------------------
244  Real getFOV() const noexcept{return m_fov;}
245  //----------------------------------------------------------------------------------------------------------------------
248  //----------------------------------------------------------------------------------------------------------------------
249  Real getAspect() const noexcept{return m_aspect;}
250  //----------------------------------------------------------------------------------------------------------------------
253  //----------------------------------------------------------------------------------------------------------------------
254  Real getNear() const noexcept{return m_zNear;}
255  //----------------------------------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------------------------------
259  Real getFar() const noexcept{return m_zFar;}
260  //----------------------------------------------------------------------------------------------------------------------
262  //----------------------------------------------------------------------------------------------------------------------
263  void calculateFrustum() noexcept;
264  //----------------------------------------------------------------------------------------------------------------------
266  //----------------------------------------------------------------------------------------------------------------------
267  void drawFrustum() noexcept;
268  //----------------------------------------------------------------------------------------------------------------------
272  //----------------------------------------------------------------------------------------------------------------------
273  CameraIntercept isPointInFrustum(const Vec3 &_p) const noexcept;
274  //----------------------------------------------------------------------------------------------------------------------
279  //----------------------------------------------------------------------------------------------------------------------
280  CameraIntercept isSphereInFrustum(const Vec3 &_p, Real _radius) const noexcept;
281  //----------------------------------------------------------------------------------------------------------------------
285  //----------------------------------------------------------------------------------------------------------------------
286  CameraIntercept boxInFrustum(const AABB &b) const noexcept;
287 
288 protected :
289 
290  //----------------------------------------------------------------------------------------------------------------------
292  //----------------------------------------------------------------------------------------------------------------------
293  void setPerspProjection() noexcept;
294  //----------------------------------------------------------------------------------------------------------------------
299  //----------------------------------------------------------------------------------------------------------------------
300  void rotAxes(Vec4& io_a, Vec4& io_b, const Real _angle) noexcept;
301 
302 protected :
303 
304  //----------------------------------------------------------------------------------------------------------------------
306  //----------------------------------------------------------------------------------------------------------------------
308  //----------------------------------------------------------------------------------------------------------------------
310  //----------------------------------------------------------------------------------------------------------------------
312  //----------------------------------------------------------------------------------------------------------------------
314  //----------------------------------------------------------------------------------------------------------------------
316  //----------------------------------------------------------------------------------------------------------------------
318  //----------------------------------------------------------------------------------------------------------------------
320  //----------------------------------------------------------------------------------------------------------------------
322  //----------------------------------------------------------------------------------------------------------------------
324  //----------------------------------------------------------------------------------------------------------------------
326  //----------------------------------------------------------------------------------------------------------------------
328  //----------------------------------------------------------------------------------------------------------------------
330  //----------------------------------------------------------------------------------------------------------------------
332  //----------------------------------------------------------------------------------------------------------------------
334  //----------------------------------------------------------------------------------------------------------------------
336  //----------------------------------------------------------------------------------------------------------------------
338  //----------------------------------------------------------------------------------------------------------------------
340  //----------------------------------------------------------------------------------------------------------------------
342  //----------------------------------------------------------------------------------------------------------------------
344  //----------------------------------------------------------------------------------------------------------------------
346  //----------------------------------------------------------------------------------------------------------------------
348  //----------------------------------------------------------------------------------------------------------------------
350  //----------------------------------------------------------------------------------------------------------------------
352  //----------------------------------------------------------------------------------------------------------------------
354  //----------------------------------------------------------------------------------------------------------------------
356  //----------------------------------------------------------------------------------------------------------------------
358  //----------------------------------------------------------------------------------------------------------------------
359  Plane m_planes[6];
360  //----------------------------------------------------------------------------------------------------------------------
362  //----------------------------------------------------------------------------------------------------------------------
363  enum class ProjPlane : char { TOP = 0,BOTTOM,LEFT,RIGHT,NEARP,FARP};
364  //----------------------------------------------------------------------------------------------------------------------
366  //----------------------------------------------------------------------------------------------------------------------
367  Vec3 m_ntl,m_ntr,m_nbl,m_nbr,m_ftl,m_ftr,m_fbl,m_fbr;
368  //----------------------------------------------------------------------------------------------------------------------
370  //----------------------------------------------------------------------------------------------------------------------
372  //----------------------------------------------------------------------------------------------------------------------
374  //----------------------------------------------------------------------------------------------------------------------
375  void setViewMatrix() noexcept;
376  //----------------------------------------------------------------------------------------------------------------------
378  //----------------------------------------------------------------------------------------------------------------------
379  void setProjectionMatrix() noexcept;
380 
381 };
382 }
383 #endif // end of class
384 
385 //----------------------------------------------------------------------------------------------------------------------
386 
const Mat4 & getProjectionMatrix() noexcept
accesor to get the projection matrix
Definition: Camera.h:195
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
Vec4 getN() const noexcept
get the n vector
Definition: Camera.h:239
Vec4 m_v
vector for the Camera local cord frame
Definition: Camera.h:311
CameraIntercept
Definition: Camera.h:36
Mat4 m_projectionMatrix
Projection a Matrix to hold the perspective transfomatio matrix for the camera.
Definition: Camera.h:371
main definition of types and namespace
#define NGL_DLLEXPORT
Definition: Types.h:65
Real m_aspect
used to store the current camera aspect ratio (can be derived from Width / Height ...
Definition: Camera.h:339
Vec4 m_look
where the camera is looking to, used with Eye to calculate the Vector m_n
Definition: Camera.h:323
Real getNear() const noexcept
get the near clip value
Definition: Camera.h:254
Definition: AABB.h:42
Real getFar() const noexcept
get the far clip value
Definition: Camera.h:259
void setEye(Vec4 _e) noexcept
set the eye position and re-calculate the matrices
Definition: Camera.h:209
an Axis Aligned Bounding Box
Vec4 getV() const noexcept
get the v vector
Definition: Camera.h:234
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
const Mat4 & getViewMatrix() noexcept
accesor to get the view matrix
Definition: Camera.h:190
a simple mathmatical representation of a plane
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
GLdouble GLdouble GLdouble b
Definition: glew.h:9162
Real m_fov
the feild of view of the camera view volume, measured from the eye
Definition: Camera.h:351
encapsulates a 4d Homogenous Point / Vector object
simple rib export class, attempts to auto tab the rib file etc. needs lots of work to make it complet...
Definition: RibExport.h:41
Vec4 getLook() const noexcept
get the look position
Definition: Camera.h:219
fmt::BufferedFile & move(fmt::BufferedFile &f)
Definition: posix.h:361
a simple 3 tuple container for compatibility with glsl
Real m_zNear
the near clipping plane of the camera view volume
Definition: Camera.h:343
Vec4 m_eye
the position of the Camera used to calculate the local cord frame
Definition: Camera.h:319
Vec4 m_up
gives a general indication of which way up the camera is
Definition: Camera.h:327
Vec4 m_n
vector for the Camera local cord frame
Definition: Camera.h:315
simple camera class to allow movement in an opengl scene. a lot of this stuff is from the HILL book C...
Definition: Camera.h:54
Mat4 getVPMatrix() const noexcept
get the View * Projection matrix used as part of the MVP calculations
Definition: Camera.h:200
void setLook(Vec4 _e) noexcept
set the look position and re-calculate the matrices
Definition: Camera.h:213
ProjPlane
index values for the planes array
Definition: Camera.h:363
Real m_height
the height of the display image used for some perspective projection calculations ...
Definition: Camera.h:335
Vec4 getU() const noexcept
get the u vector
Definition: Camera.h:229
Vec4 getUp() const noexcept
get the up vector
Definition: Camera.h:224
Real m_zFar
the far clipping plane of the camera view volume
Definition: Camera.h:347
Vec4 m_u
vector for the Camera local cord frame
Definition: Camera.h:307
a simple rib exporter function
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
Vec4 getEye() const noexcept
get the eye position
Definition: Camera.h:205
Vec3 m_ntr
Definition: Camera.h:367
Real getFOV() const noexcept
get the fov value
Definition: Camera.h:244
Real getAspect() const noexcept
get the aspect value
Definition: Camera.h:249
Mat4 m_viewMatrix
a Matrix to hold the combined modelling and viewing matrix to load into OpenGL
Definition: Camera.h:355
Real m_width
the width of the display image used for some perspective projection calculations
Definition: Camera.h:331