Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
camera.hpp
Go to the documentation of this file.
1 
22 #ifndef __CAMERA__
23 #define __CAMERA__
24 
25 #include <tucano/shader.hpp>
26 #include <Eigen/Dense>
27 #include <cmath>
28 
29 namespace Tucano
30 {
31 
37 class Camera {
38 
39 protected:
40 
42  Eigen::Matrix4f projection_matrix;
43 
45  Eigen::Affine3f view_matrix;
46 
48  Eigen::Vector4f viewport;
49 
50  // A default view matrix for reseting view
51  Eigen::Affine3f default_view;
52 
54  float near_plane;
55 
57  float far_plane;
58 
60  float frustum_left;
61 
64 
66  float frustum_top;
67 
70 
72  float aspect_ratio;
73 
75  float focal_length;
76 
78  float fovy;
79 
82 
83 public:
84 
88  void resetViewMatrix (void)
89  {
90  view_matrix = default_view;
91  }
92 
97  {
98  projection_matrix = Eigen::Matrix4f::Identity();
99  }
100 
104  void reset (void)
105  {
106  resetViewMatrix();
108  }
109 
110 
115  Eigen::Vector3f getCenter (void) const
116  {
117  return view_matrix.linear().inverse() * (-view_matrix.translation());
118  }
119 
126  void getViewMatrix (GLdouble *matrix)
127  {
128  Eigen::Matrix4f mv = view_matrix.matrix();
129  for (int i = 0; i < 16; ++i)
130  {
131  matrix[i] = mv(i);
132  }
133  }
134 
135 
142  void getProjectionMatrix (GLdouble *matrix)
143  {
144  for (int i = 0; i < 16; ++i)
145  {
146  matrix[i] = projection_matrix(i);
147  }
148  }
149 
157  inline Eigen::Vector3f projectPoint (const Eigen::Vector4f& pt, Eigen::Vector4f& viewport)
158  {
159  Eigen::Vector3f screen_pt = Eigen::Vector3f::Zero();
160  Eigen::Vector4f proj = projection_matrix * view_matrix * pt;
161  if (proj[3] == 0.0)
162  return screen_pt;
163 
164  proj /= proj[3];
165 
166  screen_pt[0] = viewport[0] + viewport[2] * (proj[0] * 0.5 + 0.5);
167  screen_pt[1] = viewport[1] + viewport[3] * (proj[1] * 0.5 + 0.5);
168  screen_pt[2] = proj[2]*0.5 + 0.5;
169  return screen_pt;
170  }
171 
172 
177  Eigen::Affine3f getViewMatrix (void) const
178  {
179  return view_matrix;
180  }
181 
186  Eigen::Affine3f* viewMatrix (void)
187  {
188  return &view_matrix;
189  }
190 
195  Eigen::Matrix4f getProjectionMatrix (void) const
196  {
197  return projection_matrix;
198  }
199 
204  Eigen::Matrix4f* projectionMatrix (void)
205  {
206  return &projection_matrix;
207  }
208 
209 
214  Eigen::Matrix3f getRotationMatrix (void) const
215  {
216  return view_matrix.rotation();
217  }
218 
219 
224  Eigen::Vector3f getTranslationMatrix (void) const
225  {
226  return view_matrix.translation();
227  }
228 
235  float getPerspectiveScale (void) const
236  {
237  return (float)1.0f/tan((fovy/2.0f)*(M_PI/180.0f));
238  }
239 
246  Eigen::Vector4f getViewport (void) const
247  {
248  return viewport;
249  }
250 
256  Eigen::Vector2i getViewportSize (void) const
257  {
258  return Eigen::Vector2i(viewport[2], viewport[3]);
259  }
260 
265  float getViewportAspectRatio (void) const
266  {
267  return (viewport[2] / viewport[3]);
268  }
269 
274  void setViewport (const Eigen::Vector4f &vp)
275  {
276  viewport = vp;
277  }
278 
279  void setViewport (const Eigen::Vector2i &vp)
280  {
281  viewport = Eigen::Vector4f(0.0, 0.0, (float)vp[0], (float)vp[1]);
282  }
283 
284 
291  void setViewport (const Eigen::Vector2f &vp)
292  {
293  viewport = Eigen::Vector4f(0.0, 0.0, vp[0], vp[1]);
294  }
295 
300  void setProjectionMatrix(const Eigen::Matrix4f& mat)
301  {
302  projection_matrix = mat;
303  }
304 
309  void setViewMatrix(const Eigen::Affine3f& mat)
310  {
311  view_matrix = mat;
312  }
313 
314 
319  float getNearPlane (void) const
320  {
321  return near_plane;
322  }
323 
328  float getFarPlane (void) const
329  {
330  return far_plane;
331  }
332 
337  {
338  }
339 
344  float getFovy (void) const
345  {
346  return fovy;
347  }
348 
349 public:
350 
354  Camera (void)
355  {
356  frustum_left = -1.0;
357  frustum_right = 1.0;
358  frustum_bottom = -1.0;
359  frustum_top = -1.0;
360  near_plane = 0.1;
361  far_plane = 100.0;
362  fovy = 60.0;
363 
364  default_view = Eigen::Affine3f::Identity();
365  reset();
366  }
367 
368 
377  static Eigen::Matrix4f createPerspectiveMatrix (float fy, float in_aspect_ratio, float in_near_plane,float in_far_plane)
378  {
379  Eigen::Matrix4f out = Eigen::Matrix4f::Zero();
380 
381  const float
382  y_scale = (float)1.0/tan((fy/2.0)*(M_PI/180.0)),
383  x_scale = y_scale / in_aspect_ratio,
384  frustum_length = in_far_plane - in_near_plane;
385 
386  out(0,0) = x_scale;
387  out(1,1) = y_scale;
388  out(2,2) = -((in_far_plane + in_near_plane) / frustum_length);
389  out(3,2) = -1.0;
390  out(2,3) = -((2 * in_near_plane * in_far_plane) / frustum_length);
391 
392  return out;
393  }
394 
405  Eigen::Matrix4f setPerspectiveMatrix (float fy, float in_aspect_ratio, float in_near_plane,float in_far_plane)
406  {
407  fovy = fy;
408  aspect_ratio = in_aspect_ratio;
409  near_plane = in_near_plane;
410  far_plane = in_far_plane;
411 
412  Eigen::Matrix4f proj = createPerspectiveMatrix(fovy, aspect_ratio, near_plane, far_plane);
413  setProjectionMatrix(proj);
414  use_perspective = true;
415  return proj;
416  }
417 
422  void changeFovy (float new_fovy)
423  {
424  fovy = new_fovy;
425  setPerspectiveMatrix(fovy, aspect_ratio, near_plane, far_plane);
426  }
427 
438  static Eigen::Matrix4f createOrthographicMatrix (float left, float right, float bottom, float top, float near_plane, float far_plane)
439  {
440  Eigen::Matrix4f out = Eigen::Matrix4f::Zero();
441 
442  out(0,0) = 2.0/(right-left);
443  out(1,1) = 2.0/(top-bottom);
444  out(2,2) = -2.0/(far_plane-near_plane);
445  out(3,3) = 1.0;
446  out(0,3) = -(right+left)/(right-left);
447  out(1,3) = -(top+bottom)/(top-bottom);
448  out(2,3) = -(far_plane+near_plane)/(far_plane-near_plane);
449 
450  return out;
451  }
452 
465  Eigen::Matrix4f setOrthographicMatrix (float left, float right, float bottom, float top, float near_plane, float far_plane)
466  {
467  Eigen::Matrix4f proj = createOrthographicMatrix(left, right, bottom, top, near_plane, far_plane);
468  setProjectionMatrix(proj);
469  use_perspective = false;
470  return proj;
471  }
472 
477  void incrementFov (float inc)
478  {
479  if (use_perspective)
480  {
481  changeFovy(fovy + inc);
482  }
483  }
484 
489  void translate (const Eigen::Vector3f& translation)
490  {
491  view_matrix.translate(translation);
492  }
493 
498  void rotate (const Eigen::Quaternion<float>& rotation)
499  {
500  view_matrix.rotate(rotation);
501  }
502 
507  void scale (const Eigen::Vector3f& scale_factors)
508  {
509  view_matrix.scale(scale_factors);
510  }
511 
516  void scale (float scale_factor)
517  {
518  view_matrix.scale(scale_factor);
519  }
520 };
521 
522 }
523 #endif
Eigen::Affine3f getViewMatrix(void) const
Returns the view matrix as an Affine 3x3 matrix.
Definition: camera.hpp:177
float getNearPlane(void) const
Returns near plane value.
Definition: camera.hpp:319
float frustum_right
Frustum right dimension.
Definition: camera.hpp:63
bool use_perspective
Flag to indicate if using a perspective or othograpic projection.
Definition: camera.hpp:81
void setViewport(const Eigen::Vector2i &vp)
Definition: camera.hpp:279
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
float aspect_ratio
Aspect ratio for projection matrix.
Definition: camera.hpp:72
static Eigen::Matrix4f createPerspectiveMatrix(float fy, float in_aspect_ratio, float in_near_plane, float in_far_plane)
Returns a perspective projection matrix with the given parameters.
Definition: camera.hpp:377
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
float getFarPlane(void) const
Returns far plane value.
Definition: camera.hpp:328
void setViewport(const Eigen::Vector4f &vp)
Sets the viewport coordinates.
Definition: camera.hpp:274
void scale(float scale_factor)
Scales the view matrix by given factor in all axis.
Definition: camera.hpp:516
float far_plane
Far plane used for projection matrix.
Definition: camera.hpp:57
Eigen::Matrix4f getProjectionMatrix(void) const
Returns the view matrix as an 4x4 matrix.
Definition: camera.hpp:195
void incrementFov(float inc)
Increases the fov of the perspective matrix by a given increment.
Definition: camera.hpp:477
float fovy
Field of view angle in y axis.
Definition: camera.hpp:78
float frustum_bottom
Frustum bottom dimension.
Definition: camera.hpp:69
float near_plane
Near plane used for projection matrix.
Definition: camera.hpp:54
~Camera()
Default destructor.
Definition: camera.hpp:336
Eigen::Matrix3f getRotationMatrix(void) const
Returns a 3x3 matrix containing only the rotation of the view matrix.
Definition: camera.hpp:214
void setViewMatrix(const Eigen::Affine3f &mat)
Sets the view matrix from a given an affine 3x3 matrix.
Definition: camera.hpp:309
void translate(const Eigen::Vector3f &translation)
Translates the view matrix by a given vector.
Definition: camera.hpp:489
void setProjectionMatrix(const Eigen::Matrix4f &mat)
Sets the projection matrix from a given 4x4 matrix.
Definition: camera.hpp:300
void resetProjectionMatrix(void)
Reset projection matrix.
Definition: camera.hpp:96
void rotate(const Eigen::Quaternion< float > &rotation)
Rotate the view matrix by a given quaternion.
Definition: camera.hpp:498
void changeFovy(float new_fovy)
Changes the fovy and computes new perspective projection matrix.
Definition: camera.hpp:422
void scale(const Eigen::Vector3f &scale_factors)
Scales the view matrix by given factors.
Definition: camera.hpp:507
Eigen::Matrix4f setPerspectiveMatrix(float fy, float in_aspect_ratio, float in_near_plane, float in_far_plane)
Sets the projection matrix as a perspective matrix.
Definition: camera.hpp:405
Eigen::Vector3f projectPoint(const Eigen::Vector4f &pt, Eigen::Vector4f &viewport)
Returns screen space coordinates of a 3D point.
Definition: camera.hpp:157
Eigen::Vector4f viewport
Viewport dimensions [minX, minY, width, height].
Definition: camera.hpp:48
Eigen::Vector3f getCenter(void) const
Returns the center of the camera in world space.
Definition: camera.hpp:115
float focal_length
Camera&#39;s Focal Length.
Definition: camera.hpp:75
Eigen::Affine3f default_view
Definition: camera.hpp:51
float getFovy(void) const
Returns current field of view angle in y axis.
Definition: camera.hpp:344
Eigen::Vector3f getTranslationMatrix(void) const
Returns the translation part of the view matrix as a vector.
Definition: camera.hpp:224
void setViewport(const Eigen::Vector2f &vp)
Sets the viewport coordinates considering that the minimum coordinates are zero.
Definition: camera.hpp:291
Camera(void)
Default constructor.
Definition: camera.hpp:354
Eigen::Affine3f * viewMatrix(void)
Returns a pointer to the view matrix as an Affine 3x3 matrix.
Definition: camera.hpp:186
float frustum_top
Frustum top dimension.
Definition: camera.hpp:66
float getPerspectiveScale(void) const
Returns the perspective scale.
Definition: camera.hpp:235
float frustum_left
Frustum left dimension.
Definition: camera.hpp:60
void reset(void)
Resets trackball to initial position and orientation.
Definition: camera.hpp:104
static Eigen::Matrix4f createOrthographicMatrix(float left, float right, float bottom, float top, float near_plane, float far_plane)
Returns an orthographic projection matrix with the given parameters.
Definition: camera.hpp:438
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void resetViewMatrix(void)
Reset view matrix.
Definition: camera.hpp:88
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
Eigen::Vector2i getViewportSize(void) const
Returns the dimensions of the viewport.
Definition: camera.hpp:256
Eigen::Matrix4f projection_matrix
Projection, or intrinsic, matrix.
Definition: camera.hpp:42
Eigen::Matrix4f setOrthographicMatrix(float left, float right, float bottom, float top, float near_plane, float far_plane)
Sets the projection matrix as a orthographic matrix.
Definition: camera.hpp:465
Eigen::Affine3f view_matrix
View, or extrinsic, matrix.
Definition: camera.hpp:45
Eigen::Matrix4f * projectionMatrix(void)
Returns a pointer to the projection matrix as an 4x4 matrix.
Definition: camera.hpp:204
float getViewportAspectRatio(void) const
Returns the viewport aspect ratio.
Definition: camera.hpp:265