Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
freecamera.hpp
Go to the documentation of this file.
1 
23 #ifndef __FREECAMERA__
24 #define __FREECAMERA__
25 
27 #include <Eigen/Dense>
28 #include <cmath>
29 
30 namespace Tucano
31 {
32 
38 class Freecamera : public Tucano::Flycamera {
39 protected:
40 
42 
43 public:
44 
48  void reset (void) override
49  {
50  start_mouse_pos = Eigen::Vector2f::Zero();
51  rotation_matrix = Eigen::Matrix3f::Identity();
52  default_translation = Eigen::Vector3f (0.0, 0.0, -2.0);
54  rotation_X_axis = 0.0;
55  rotation_Y_axis = 0.0;
56  rotation_Z_axis = 0.0;
58  }
59 
63  void updateViewMatrix() override
64  {
65  Eigen::Vector3f xAxis = rotation_matrix.row(0);
66  Eigen::Vector3f yAxis = rotation_matrix.row(1);
67  Eigen::Vector3f zAxis = rotation_matrix.row(2);
68 
69  if( rotation_Z_axis )
70  {
71  Eigen::AngleAxisf transfRotZ = Eigen::AngleAxisf(rotation_Z_axis, zAxis);
72 
73  // compute X axis restricted to a rotation around Z axis
74  xAxis = transfRotZ * xAxis;
75  xAxis.normalize();
76 
77  // compute Y axis restricted to a rotation around Z axis
78  yAxis = transfRotZ * yAxis;
79  yAxis.normalize();
80 
81  rotation_Z_axis = 0.0;
82  }
83 
84  Eigen::AngleAxisf transfRotY = Eigen::AngleAxisf(rotation_Y_axis, yAxis);
85 
86  // compute X axis restricted to a rotation around Y axis
87  Eigen::Vector3f rotX = transfRotY * xAxis;
88  rotX.normalize();
89 
90  Eigen::AngleAxisf transfRotX = Eigen::AngleAxisf(rotation_X_axis, rotX);
91 
92  // rotate Z axis around Y axis, then rotate new Z axis around X new axis
93  Eigen::Vector3f rotZ = transfRotY * zAxis;
94  rotZ = transfRotX * rotZ;
95  rotZ.normalize();
96 
97  // rotate Y axis around X new axis
98  Eigen::Vector3f rotY = transfRotX * yAxis;
99  rotY.normalize();
100 
101  rotation_matrix.row(0) = rotX;
102  rotation_matrix.row(1) = rotY;
103  rotation_matrix.row(2) = rotZ;
104 
105  resetViewMatrix();
106  view_matrix.rotate (rotation_matrix);
107  view_matrix.translate (translation_vector);
108 
109  rotation_X_axis = 0.0;
110  rotation_Y_axis = 0.0;
111  }
112 
116  void strideLeft ( void ) override
117  {
119  }
120 
124  void strideRight ( void ) override
125  {
127  }
128 
132  void moveBack ( void ) override
133  {
135  }
136 
140  void moveForward ( void ) override
141  {
143  }
144 
148  void moveDown ( void ) override
149  {
151  }
152 
156  void moveUp ( void ) override
157  {
159  }
160 
165  void rotate ( Eigen::Vector2f new_mouse_pos ) override
166  {
167  Eigen::Vector2f new_position = normalizePosition(new_mouse_pos);
168  Eigen::Vector2f dir2d = new_position - start_mouse_pos;
169 
170  start_mouse_pos = new_position;
171 
172  rotation_X_axis = dir2d[1]*M_PI;
173  rotation_Y_axis = -dir2d[0]*M_PI;
174 
175  if (rotation_X_axis > 2*M_PI)
176  rotation_X_axis -= 2*M_PI;
177  if (rotation_X_axis < 0)
178  rotation_X_axis += 2*M_PI;
179  if (rotation_Y_axis > 2*M_PI)
180  rotation_Y_axis -= 2*M_PI;
181  if (rotation_Y_axis < 0)
182  rotation_Y_axis += 2*M_PI;
183  }
184 
189  void rotateZ ( Eigen::Vector2f new_mouse_pos ) override
190  {
191  Eigen::Vector2f new_position = normalizePosition(new_mouse_pos);
192  Eigen::Vector2f dir2d = new_position - start_mouse_pos;
193 
194  start_mouse_pos = new_position;
195 
196  rotation_Z_axis = dir2d[1]*M_PI;
197 
198  if (rotation_Z_axis > 2*M_PI)
199  rotation_Z_axis -= 2*M_PI;
200  if (rotation_Z_axis < 0)
201  rotation_X_axis += 2*M_PI;
202  }
203 
204 };
205 
206 }
207 
208 #endif
float rotation_Y_axis
Rotation angles.
Definition: flycamera.hpp:65
void moveDown(void) override
Translates the view matrix down.
Definition: freecamera.hpp:148
float rotation_Z_axis
Definition: freecamera.hpp:41
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 strideLeft(void) override
Translates the view matrix to the left.
Definition: freecamera.hpp:116
void updateViewMatrix() override
Compose rotation and translation.
Definition: freecamera.hpp:63
void rotate(Eigen::Vector2f new_mouse_pos) override
Rotates the camera view direction around X and Y axes.
Definition: freecamera.hpp:165
Flythrough camera class for manipulating a camera.
Definition: flycamera.hpp:41
void moveForward(void) override
Translates the view matrix forward.
Definition: freecamera.hpp:140
float speed
Global movement speed.
Definition: flycamera.hpp:47
void reset(void) override
Resets camera to initial position and orientation.
Definition: freecamera.hpp:48
float rotation_X_axis
Definition: flycamera.hpp:66
void moveUp(void) override
Translates the view matrix up.
Definition: freecamera.hpp:156
Eigen::Vector3f default_translation
Definition: flycamera.hpp:62
Free camera class for manipulating a camera.
Definition: freecamera.hpp:38
void strideRight(void) override
Translates the view matrix to the right.
Definition: freecamera.hpp:124
void moveBack(void) override
Translates the view matrix back.
Definition: freecamera.hpp:132
Eigen::Vector2f start_mouse_pos
Current mouse position.
Definition: flycamera.hpp:50
void rotateZ(Eigen::Vector2f new_mouse_pos) override
Rotates the camera view direction around Z axis.
Definition: freecamera.hpp:189
void resetViewMatrix(void)
Reset view matrix.
Definition: camera.hpp:88
Eigen::Affine3f view_matrix
View, or extrinsic, matrix.
Definition: camera.hpp:45