Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
flycamera.hpp
Go to the documentation of this file.
1 
23 #ifndef __FLYCAMERA__
24 #define __FLYCAMERA__
25 
26 
27 #include <tucano/camera.hpp>
29 #include <Eigen/Dense>
30 #include <cmath>
31 
32 namespace Tucano
33 {
34 
35 
41 class Flycamera : public Tucano::Camera {
42 
43 
44 protected:
45 
47  float speed;
48 
50  Eigen::Vector2f start_mouse_pos;
51 
53  Eigen::Matrix3f rotation_matrix;
54 
56  Eigen::Matrix3f default_rotation;
57 
59  Eigen::Vector3f translation_vector;
60 
61  // Default start translation vector
62  Eigen::Vector3f default_translation;
63 
67 
68  // Coordiante axes for rendering flycamera orientation
70 
71 public:
72 
76  virtual void reset (void)
77  {
78  start_mouse_pos = Eigen::Vector2f::Zero();
79  translation_vector = Eigen::Vector3f::Zero();
80  rotation_matrix = Eigen::Matrix3f::Identity();
81  default_translation = Eigen::Vector3f (0.0, 0.0, -2.0);
82  default_rotation = Eigen::Matrix3f::Identity();
83  rotation_X_axis = 0.0;
84  rotation_Y_axis = 0.0;
86  }
87 
90  {}
91 
96  void initOpenGLMatrices (void)
97  {
98  // reset all matrices
99  reset();
100  }
101 
106  {
107  speed = 0.05;
108  reset();
110 
111  }
112 
113 
118  void setDefaultView (Eigen::Affine3f view)
119  {
120  default_rotation = view.rotation();
121  default_translation = view.translation();
122 
123  cout << "R = " << endl << default_rotation << endl << endl;
124  cout << "T = " << default_translation.transpose() << endl << endl;
125  }
126 
131  Eigen::Vector3f getDefaultTranslation (void)
132  {
133  return default_translation;
134  }
135 
136 
140  void renderAtCorner (void)
141  {
142  float ratio = (viewport[2] - viewport[0]) / (viewport[3] - viewport[1]);
143 
144  Eigen::Matrix4f rep_projection_matrix = createOrthographicMatrix(-ratio, ratio, -1.0, 1.0, 0.1, 100.0);
145 
146  Eigen::Affine3f rep_view_matrix = Eigen::Affine3f::Identity();
147  rep_view_matrix.translate( Eigen::Vector3f(1.0, -0.75, -5.0));
148 
149  Camera lightcam;
150 
151  Camera cam;
152  cam.setViewMatrix(rep_view_matrix);
153  cam.setProjectionMatrix(rep_projection_matrix);
154  cam.setViewport(viewport);
155 
156  axes.resetModelMatrix();
157  axes.modelMatrix()->rotate(rotation_matrix.inverse());
158  axes.modelMatrix()->scale(0.3);
159  axes.render(cam, lightcam);
160  }
161 
162 
166  virtual void updateViewMatrix()
167  {
168  resetViewMatrix();
169 
170  // compute X axis restricted to a rotation around Y axis
171  Eigen::Vector3f rotX = Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY()) * Eigen::Vector3f::UnitX();
172  rotX.normalize();
173 
174  // rotate Z axis around Y axis, then rotate new Z axis around X new axis
175  Eigen::Vector3f rotZ = Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY()) * Eigen::Vector3f::UnitZ();
176  rotZ = Eigen::AngleAxisf(rotation_X_axis, rotX) * rotZ;
177  rotZ.normalize();
178 
179  // rotate Y axis around X new axis
180  Eigen::Vector3f rotY = Eigen::AngleAxisf(rotation_X_axis, rotX) * Eigen::Vector3f::UnitY();
181  rotY.normalize();
182 
183  rotation_matrix.row(0) = rotX;
184  rotation_matrix.row(1) = rotY;
185  rotation_matrix.row(2) = rotZ;
186 
187  view_matrix.rotate (default_rotation);
188  view_matrix.rotate (rotation_matrix);
189  view_matrix.translate (default_translation);
190  view_matrix.translate (translation_vector);
191  }
192 
196  virtual void strideLeft ( void )
197  {
198  Eigen::Vector3f dir = (Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY())) * Eigen::Vector3f(1.0, 0.0, 0.0);
199  translation_vector += dir * speed;
200  }
201 
205  virtual void strideRight ( void )
206  {
207  Eigen::Vector3f dir = (Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY())) * Eigen::Vector3f(-1.0, 0.0, 0.0);
208  translation_vector += dir * speed;
209  }
210 
214  virtual void moveBack ( void )
215  {
216  Eigen::Vector3f dir = (Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY())) * Eigen::Vector3f(0.0, 0.0, -1.0);
217  translation_vector += dir * speed;
218  }
219 
223  virtual void moveForward ( void )
224  {
225  Eigen::Vector3f dir = (Eigen::AngleAxisf(rotation_Y_axis, Eigen::Vector3f::UnitY())) * Eigen::Vector3f(0.0, 0.0, 1.0);
226  translation_vector += dir * speed;
227  }
228 
232  virtual void moveDown ( void )
233  {
234  translation_vector += Eigen::Vector3f::UnitY() * speed;
235  }
236 
240  virtual void moveUp ( void )
241  {
242  translation_vector -= Eigen::Vector3f::UnitY() * speed;
243  }
244 
250  Eigen::Vector2f normalizePosition (const Eigen::Vector2f& pos)
251  {
252  return Eigen::Vector2f ((pos[0]/((viewport[2]-viewport[0])/2.0)) - 1.0,
253  1.0 - (pos[1]/((viewport[3] - viewport[1])/2.0)));
254  }
255 
260  void startRotation ( Eigen::Vector2f pos )
261  {
262  start_mouse_pos = normalizePosition ( pos );
263  }
264 
269  virtual void rotate ( Eigen::Vector2f new_mouse_pos )
270  {
271  Eigen::Vector2f new_position = normalizePosition(new_mouse_pos);
272  Eigen::Vector2f dir2d = new_position - start_mouse_pos;
273 
274  start_mouse_pos = new_position;
275 
276  float anglex = -dir2d[1]*M_PI;
277  float angley = -dir2d[0]*M_PI;
278 
279  rotation_X_axis += anglex;
280  rotation_Y_axis += angley;
281 
282  if (rotation_X_axis > 2*M_PI)
283  rotation_X_axis -= 2*M_PI;
284  if (rotation_X_axis < 0)
285  rotation_X_axis += 2*M_PI;
286  if (rotation_Y_axis > 2*M_PI)
287  rotation_Y_axis -= 2*M_PI;
288  if (rotation_Y_axis < 0)
289  rotation_Y_axis += 2*M_PI;
290 
291  }
292 
297  virtual void rotateZ ( Eigen::Vector2f new_mouse_pos )
298  {}
299 
304  void setSpeed( const float& speed )
305  { this->speed = speed; }
306 };
307 
308 }
309 #endif
float rotation_Y_axis
Rotation angles.
Definition: flycamera.hpp:65
Eigen::Affine3f * modelMatrix(void)
Returns a pointer to the model matrix.
Definition: model.hpp:112
Eigen::Vector2f normalizePosition(const Eigen::Vector2f &pos)
Nomalizes a screen position to range [-1,1].
Definition: flycamera.hpp:250
Eigen::Matrix3f rotation_matrix
Camera rotation (view direction)
Definition: flycamera.hpp:53
Eigen::Vector3f translation_vector
Camera position.
Definition: flycamera.hpp:59
Definition: bufferobject.hpp:34
void setViewport(const Eigen::Vector4f &vp)
Sets the viewport coordinates.
Definition: camera.hpp:274
virtual void rotate(Eigen::Vector2f new_mouse_pos)
Rotates the camera view direction.
Definition: flycamera.hpp:269
Flythrough camera class for manipulating a camera.
Definition: flycamera.hpp:41
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render camera representation.
Definition: coordinateaxes.hpp:67
float speed
Global movement speed.
Definition: flycamera.hpp:47
Visual representation of a 3D coordinate axes.
Definition: coordinateaxes.hpp:44
void setViewMatrix(const Eigen::Affine3f &mat)
Sets the view matrix from a given an affine 3x3 matrix.
Definition: camera.hpp:309
void initOpenGLMatrices(void)
Initializes the view and projection matrices. They are all initialized as Identity matrices...
Definition: flycamera.hpp:96
float rotation_X_axis
Definition: flycamera.hpp:66
void setProjectionMatrix(const Eigen::Matrix4f &mat)
Sets the projection matrix from a given 4x4 matrix.
Definition: camera.hpp:300
virtual void moveBack(void)
Translates the view matrix back.
Definition: flycamera.hpp:214
virtual void moveDown(void)
Translates the view matrix down.
Definition: flycamera.hpp:232
Eigen::Vector3f default_translation
Definition: flycamera.hpp:62
Eigen::Vector4f viewport
Viewport dimensions [minX, minY, width, height].
Definition: camera.hpp:48
virtual void moveForward(void)
Translates the view matrix forward.
Definition: flycamera.hpp:223
void resetModelMatrix(void)
Resets the model matrix.
Definition: model.hpp:159
~Flycamera()
Default destructor.
Definition: flycamera.hpp:89
Tucano::Shapes::CoordinateAxes axes
Definition: flycamera.hpp:69
Flycamera()
Default constructor.
Definition: flycamera.hpp:105
void setDefaultView(Eigen::Affine3f view)
Set default rotation and translation from and Affine3f matrix.
Definition: flycamera.hpp:118
virtual void moveUp(void)
Translates the view matrix up.
Definition: flycamera.hpp:240
virtual void strideLeft(void)
Translates the view matrix to the left.
Definition: flycamera.hpp:196
void setSpeed(const float &speed)
Changes the camera speed.
Definition: flycamera.hpp:304
virtual void rotateZ(Eigen::Vector2f new_mouse_pos)
Rotates the camera view direction around Z axis. Default implementation does nothing.
Definition: flycamera.hpp:297
void startRotation(Eigen::Vector2f pos)
Begin view direction rotation.
Definition: flycamera.hpp:260
Eigen::Vector2f start_mouse_pos
Current mouse position.
Definition: flycamera.hpp:50
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
virtual void strideRight(void)
Translates the view matrix to the right.
Definition: flycamera.hpp:205
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void resetViewMatrix(void)
Reset view matrix.
Definition: camera.hpp:88
virtual void updateViewMatrix()
Compose rotation and translation.
Definition: flycamera.hpp:166
virtual void reset(void)
Resets camera to initial position and orientation.
Definition: flycamera.hpp:76
Eigen::Vector3f getDefaultTranslation(void)
Returns the default translation for placing the camera outside the trackball sphere.
Definition: flycamera.hpp:131
Eigen::Matrix3f default_rotation
Default start rotation matrix.
Definition: flycamera.hpp:56
Eigen::Affine3f view_matrix
View, or extrinsic, matrix.
Definition: camera.hpp:45
void renderAtCorner(void)
Renders the camera&#39;s coordinate axis at the lower right corner of the screen.
Definition: flycamera.hpp:140