Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
qtflycamerawidget.hpp
Go to the documentation of this file.
1 
23 #ifndef __QTFLYCAMERAWIDGET__
24 #define __QTFLYCAMERAWIDGET__
25 
26 #include <GL/glew.h>
27 
28 #include "objimporter.hpp"
29 #include "plyimporter.hpp"
30 
31 #include <tucano.hpp>
32 #include <utils/flycamera.hpp>
33 #include <utils/trackball.hpp>
34 
35 #include <QGLWidget>
36 #include <QMouseEvent>
37 #include <QFileDialog>
38 
39 using namespace std;
40 
41 namespace Tucano
42 {
43 
50 class QtGlewFlycameraInitializer : public QGLWidget
51 {
52 public:
53  QtGlewFlycameraInitializer (QWidget* parent) : QGLWidget(parent)
54  {
55  makeCurrent();
57  }
58 
59 };
60 
67 {
68  Q_OBJECT
69 
70 protected:
71 
74 
77 
80 
81 public:
82 
87  explicit QtFlycameraWidget(QWidget *parent) : QtGlewFlycameraInitializer(parent){
88  camera = new Flycamera();
89  }
90 
95  delete camera;
96  }
97 
101  virtual void initializeGL (void)
102  {
103  makeCurrent();
104 
105  #ifdef TUCANODEBUG
106  QGLFormat glCurrentFormat = this->format();
107  cout << "QT GL version : " << glCurrentFormat.majorVersion() << " , " << glCurrentFormat.minorVersion() << endl;
108  #endif
109  }
110 
114  virtual void resizeGL (void)
115  {
116  camera->setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
117  camera->setPerspectiveMatrix(camera->getFovy(), (float)this->width()/(float)this->height(), 0.1f, 100.0f);
118  light_trackball.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
119  updateGL();
120  }
121 
125  void initialize (void)
126  {
127  Eigen::Vector2i size;
128  size << this->width(), this->height();
129 
130  camera->setPerspectiveMatrix(60.0, (float)this->width()/(float)this->height(), 0.1f, 100.0f);
131  camera->setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
132 
133  light_trackball.setRenderFlag(false);
134  light_trackball.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
135 
136  updateGL();
137  }
138 
144  virtual void openMesh (string filename)
145  {
146  QString str (filename.c_str());
147  QStringList strlist = str.split(".");
148  QString extension = strlist[strlist.size()-1];
149 
150  if (extension.compare("ply") != 0 && extension.compare("obj") != 0)
151  {
152  cerr << "file format [" << extension.toStdString() << "] not supported" << endl;
153  return;
154  }
155 
156  mesh = Mesh();
157 
158  if (extension.compare("ply") == 0)
159  {
160  MeshImporter::loadPlyFile(&mesh, filename);
161  }
162  if (extension.compare("obj") == 0)
163  {
164  MeshImporter::loadObjFile(&mesh, filename);
165  }
166 
167  mesh.normalizeModelMatrix();
168  }
169 
170 protected:
171 
176  virtual void keyPressEvent (QKeyEvent * event)
177  {
178  if (event->key() == Qt::Key_O)
179  {
180  QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Mesh Files (*.obj *.ply)"));
181  if (!filename.isEmpty())
182  {
183  openMesh (filename.toStdString());
184  }
185  }
186  if (event->key() == Qt::Key_R)
187  camera->reset();
188  if (event->key() == Qt::Key_A)
189  camera->strideLeft();
190  if (event->key() == Qt::Key_D)
191  camera->strideRight();
192  if (event->key() == Qt::Key_S)
193  camera->moveBack();
194  if (event->key() == Qt::Key_W)
195  camera->moveForward();
196  if (event->key() == Qt::Key_C)
197  camera->moveDown();
198  if (event->key() == Qt::Key_E)
199  camera->moveUp();
200 
201  camera->updateViewMatrix();
202 
203  event->ignore();
204  updateGL();
205  }
206 
213  void mousePressEvent (QMouseEvent * event)
214  {
215  setFocus ();
216  Eigen::Vector2f screen_pos (event->x(), event->y());
217  if (event->button() == Qt::LeftButton)
218  {
219  camera->startRotation(screen_pos);
220  camera->updateViewMatrix();
221  }
222  if (event->button() == Qt::RightButton)
223  {
224  light_trackball.rotateCamera(screen_pos);
225  }
226  updateGL ();
227  }
228 
235  void mouseMoveEvent (QMouseEvent * event)
236  {
237  Eigen::Vector2f screen_pos (event->x(), event->y());
238  if (event->buttons() & Qt::LeftButton)
239  {
240  camera->rotate(screen_pos);
241  camera->updateViewMatrix();
242  }
243  if (event->buttons() & Qt::RightButton)
244  {
245  light_trackball.rotateCamera(screen_pos);
246  }
247 
248  updateGL ();
249 
250  }
251 
258  virtual void mouseReleaseEvent (QMouseEvent * event)
259  {
260  if (event->button() == Qt::RightButton)
261  {
262  light_trackball.endRotation();
263  }
264 
265  updateGL ();
266  }
267 
268 signals:
269 
270 public slots:
271 
272 };
273 
274 }
275 #endif
void mouseMoveEvent(QMouseEvent *event)
Callback for mouse move event.
Definition: qtflycamerawidget.hpp:235
virtual Eigen::Quaternion< float > rotateCamera(const Eigen::Vector2f &pos)
Computes and applies the rotation transformations to the trackball given new position.
Definition: trackball.hpp:455
Definition: bufferobject.hpp:34
Trackball class for manipulating a camera.
Definition: trackball.hpp:79
void setViewport(const Eigen::Vector4f &vp)
Sets the viewport coordinates.
Definition: camera.hpp:274
This class is just to make sure that GLEW is initialized before anything else, so the constructor of ...
Definition: qtflycamerawidget.hpp:50
virtual void resizeGL(void)
Resize callback for the widget.
Definition: qtflycamerawidget.hpp:114
static void loadObjFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an OBJ file.
Definition: objimporter.hpp:50
virtual void openMesh(string filename)
Opens a mesh from file.
Definition: qtflycamerawidget.hpp:144
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
~QtFlycameraWidget()
Default destructor.
Definition: qtflycamerawidget.hpp:94
virtual void initializeGL(void)
Initializes openGL and GLEW.
Definition: qtflycamerawidget.hpp:101
A widget with a trackball iteration for a single mesh and a single light source. Extends QGLWidget cl...
Definition: qtflycamerawidget.hpp:66
void initialize(void)
Initializes the trackball and the mesh with a given filename.
Definition: qtflycamerawidget.hpp:125
virtual void keyPressEvent(QKeyEvent *event)
Callback for key press event.
Definition: qtflycamerawidget.hpp:176
virtual void moveBack(void)
Translates the view matrix back.
Definition: flycamera.hpp:214
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
virtual void moveDown(void)
Translates the view matrix down.
Definition: flycamera.hpp:232
void setRenderFlag(bool flag)
Returns wether the trackball representation should be drawn or not.
Definition: trackball.hpp:299
QtGlewFlycameraInitializer(QWidget *parent)
Definition: qtflycamerawidget.hpp:53
virtual void endRotation(void)
Indicates that a rotation has ended. Disables the rotating flag, indicating that the mouse callback f...
Definition: trackball.hpp:279
virtual void moveForward(void)
Translates the view matrix forward.
Definition: flycamera.hpp:223
static bool loadPlyFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an PLY file.
Definition: plyimporter.hpp:150
float getFovy(void) const
Returns current field of view angle in y axis.
Definition: camera.hpp:344
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
QtFlycameraWidget(QWidget *parent)
Default constructor.
Definition: qtflycamerawidget.hpp:87
void normalizeModelMatrix(void)
Normalize model matrix to center and scale model. The model matrix will include a translation to plac...
Definition: model.hpp:141
void startRotation(Eigen::Vector2f pos)
Begin view direction rotation.
Definition: flycamera.hpp:260
Mesh mesh
Triangle mesh.
Definition: qtflycamerawidget.hpp:73
Trackball light_trackball
Trackball for manipulating the light position.
Definition: qtflycamerawidget.hpp:79
virtual void strideRight(void)
Translates the view matrix to the right.
Definition: flycamera.hpp:205
void mousePressEvent(QMouseEvent *event)
Callback for mouse press event.
Definition: qtflycamerawidget.hpp:213
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
virtual void updateViewMatrix()
Compose rotation and translation.
Definition: flycamera.hpp:166
virtual void mouseReleaseEvent(QMouseEvent *event)
Callback for mouse release event.
Definition: qtflycamerawidget.hpp:258
virtual void reset(void)
Resets camera to initial position and orientation.
Definition: flycamera.hpp:76
Flycamera * camera
Flycamera.
Definition: qtflycamerawidget.hpp:76
void initGlew(void)
Initialize Glew.
Definition: misc.hpp:68