Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
qttrackballwidget.hpp
Go to the documentation of this file.
1 
23 #ifndef __QTTRACKBALLWIDGET__
24 #define __QTTRACKBALLWIDGET__
25 
26 #include <GL/glew.h>
27 
28 #include "objimporter.hpp"
29 #include "plyimporter.hpp"
30 
31 #include <tucano.hpp>
32 #include <utils/trackball.hpp>
33 
34 #include <QGLWidget>
35 #include <QMouseEvent>
36 #include <QFileDialog>
37 
38 using namespace std;
39 
40 namespace Tucano
41 {
42 
49 class QtGlewInitializer : public QGLWidget
50 {
51 public:
52  QtGlewInitializer (QWidget* parent) : QGLWidget(parent)
53  {
54  makeCurrent();
56  }
57 
58 };
59 
66 {
67  Q_OBJECT
68 
69 protected:
70 
73 
76 
79 
80 public:
81 
86  explicit QtTrackballWidget(QWidget *parent) : QtGlewInitializer(parent) {}
87 
92 
96  virtual void initializeGL (void)
97  {
98  makeCurrent();
99 
100  #ifdef TUCANODEBUG
101  QGLFormat glCurrentFormat = this->format();
102  cout << "QT GL version : " << glCurrentFormat.majorVersion() << " , " << glCurrentFormat.minorVersion() << endl;
103  #endif
104  }
105 
109  virtual void resizeGL (void)
110  {
111  camera.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
112  camera.setPerspectiveMatrix(camera.getFovy(), (float)this->width()/(float)this->height(), 0.1f, 100.0f);
113  light_trackball.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
114  updateGL();
115  }
116 
120  void initialize (void)
121  {
122  camera.setPerspectiveMatrix(60.0, (float)this->width()/(float)this->height(), 0.1f, 100.0f);
123  camera.setRenderFlag(true);
124  camera.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
125 
126  light_trackball.setRenderFlag(false);
127  light_trackball.setViewport(Eigen::Vector2f ((float)this->width(), (float)this->height()));
128 
129  updateGL();
130  }
131 
137  virtual void openMesh (string filename)
138  {
139  QString str (filename.c_str());
140  QStringList strlist = str.split(".");
141  QString extension = strlist[strlist.size()-1];
142 
143  if (extension.compare("ply") != 0 && extension.compare("obj") != 0)
144  {
145  cerr << "file format [" << extension.toStdString() << "] not supported" << endl;
146  return;
147  }
148 
149  mesh = Mesh();
150 
151  if (extension.compare("ply") == 0)
152  {
153  MeshImporter::loadPlyFile(&mesh, filename);
154  }
155  if (extension.compare("obj") == 0)
156  {
157  MeshImporter::loadObjFile(&mesh, filename);
158  }
159 
160  mesh.normalizeModelMatrix();
161  }
162 
163 protected:
164 
169  void keyPressEvent (QKeyEvent * event)
170  {
171  if (event->key() == Qt::Key_O)
172  {
173  QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Mesh Files (*.obj *.ply)"));
174  if (!filename.isEmpty())
175  {
176  openMesh (filename.toStdString());
177  }
178  }
179  event->ignore();
180  updateGL();
181  }
182 
189  void mousePressEvent (QMouseEvent * event)
190  {
191  setFocus ();
192  Eigen::Vector2f screen_pos (event->x(), event->y());
193  if (event->modifiers() & Qt::ShiftModifier)
194  {
195  if (event->button() == Qt::LeftButton)
196  {
197  camera.translateCamera(screen_pos);
198  }
199  }
200  else
201  {
202  if (event->button() == Qt::LeftButton)
203  {
204  camera.rotateCamera(screen_pos);
205  }
206  if (event->button() == Qt::RightButton)
207  {
208  light_trackball.rotateCamera(screen_pos);
209  }
210  }
211  updateGL ();
212  }
213 
220  void mouseMoveEvent (QMouseEvent * event)
221  {
222  Eigen::Vector2f screen_pos (event->x(), event->y());
223  if (event->modifiers() & Qt::ShiftModifier && event->buttons() & Qt::LeftButton)
224  {
225  camera.translateCamera(screen_pos);
226  }
227  else
228  {
229  if (event->buttons() & Qt::LeftButton)
230  {
231  camera.rotateCamera(screen_pos);
232  }
233  if (event->buttons() & Qt::RightButton)
234  {
235  light_trackball.rotateCamera(screen_pos);
236  }
237  }
238 
239  updateGL ();
240 
241  }
242 
249  void mouseReleaseEvent (QMouseEvent * event)
250  {
251  if (event->button() == Qt::LeftButton)
252  {
253  camera.endTranslation();
254  camera.endRotation();
255  }
256  if (event->button() == Qt::RightButton)
257  {
258  light_trackball.endRotation();
259  }
260 
261  updateGL ();
262  }
263 
270  void wheelEvent (QWheelEvent * event)
271  {
272  const int WHEEL_STEP = 120;
273 
274  float pos = event->delta () / float (WHEEL_STEP);
275 
276  if (event->modifiers() & Qt::ShiftModifier) // change FOV
277  {
278  camera.incrementFov(pos);
279  }
280  else // change ZOOM
281  {
282  if( (pos > 0) )
283  {
284  camera.increaseZoom(1.05);
285  }
286 
287  else if(pos < 0)
288  {
289  camera.increaseZoom(1.0/1.05);
290  }
291  }
292  updateGL ();
293  }
294 
295 signals:
296 
297 public slots:
298 
299 };
300 
301 }
302 #endif
void endTranslation(void)
Indicates that a translation has ended. Disable the translating flag, indicating that the mouse callb...
Definition: trackball.hpp:288
void initialize(void)
Initializes the trackball and the mesh with a given filename.
Definition: qttrackballwidget.hpp:120
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
virtual void openMesh(string filename)
Opens a mesh from file.
Definition: qttrackballwidget.hpp:137
virtual Eigen::Vector3f translateCamera(const Eigen::Vector2f &pos)
Computes and applies the translation transformations to the trackball given new position.
Definition: trackball.hpp:481
void wheelEvent(QWheelEvent *event)
Callback for mouse wheel event.
Definition: qttrackballwidget.hpp:270
void setViewport(const Eigen::Vector4f &vp)
Sets the viewport coordinates.
Definition: camera.hpp:274
QtTrackballWidget(QWidget *parent)
Default constructor.
Definition: qttrackballwidget.hpp:86
void keyPressEvent(QKeyEvent *event)
Callback for key press event.
Definition: qttrackballwidget.hpp:169
static void loadObjFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an OBJ file.
Definition: objimporter.hpp:50
void incrementFov(float inc)
Increases the fov of the perspective matrix by a given increment.
Definition: camera.hpp:477
This class is just to make sure that GLEW is initialized before anything else, so the constructor of ...
Definition: qttrackballwidget.hpp:49
QtGlewInitializer(QWidget *parent)
Definition: qttrackballwidget.hpp:52
virtual void initializeGL(void)
Initializes openGL and GLEW.
Definition: qttrackballwidget.hpp:96
void mouseReleaseEvent(QMouseEvent *event)
Callback for mouse release event.
Definition: qttrackballwidget.hpp:249
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
void setRenderFlag(bool flag)
Returns wether the trackball representation should be drawn or not.
Definition: trackball.hpp:299
virtual void endRotation(void)
Indicates that a rotation has ended. Disables the rotating flag, indicating that the mouse callback f...
Definition: trackball.hpp:279
Trackball light_trackball
Trackball for manipulating the light position.
Definition: qttrackballwidget.hpp:78
static bool loadPlyFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an PLY file.
Definition: plyimporter.hpp:150
Mesh mesh
Triangle mesh.
Definition: qttrackballwidget.hpp:72
virtual void resizeGL(void)
Resize callback for the widget.
Definition: qttrackballwidget.hpp:109
float getFovy(void) const
Returns current field of view angle in y axis.
Definition: camera.hpp:344
void mouseMoveEvent(QMouseEvent *event)
Callback for mouse move event.
Definition: qttrackballwidget.hpp:220
void mousePressEvent(QMouseEvent *event)
Callback for mouse press event.
Definition: qttrackballwidget.hpp:189
A widget with a trackball iteration for a single mesh and a single light source. Extends QGLWidget cl...
Definition: qttrackballwidget.hpp:65
void normalizeModelMatrix(void)
Normalize model matrix to center and scale model. The model matrix will include a translation to plac...
Definition: model.hpp:141
Trackball camera
Trackball for manipulating the camera.
Definition: qttrackballwidget.hpp:75
void increaseZoom(float scale)
Increases the zoom on the scene by appling a scale to the View Matrix. The current scale used in View...
Definition: trackball.hpp:506
~QtTrackballWidget()
Default destructor.
Definition: qttrackballwidget.hpp:91
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
void initGlew(void)
Initialize Glew.
Definition: misc.hpp:68