ManifoldMusic
music visualization
 All Classes Files Functions Variables Enumerations Enumerator
QGLViewerWidget.h
Go to the documentation of this file.
1 
39 /*===========================================================================*\
40  * *
41  * $Revision: 736 $ *
42  * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08 Okt 2012) $ *
43  * *
44 \*===========================================================================*/
45 
46 #ifndef OPENMESHAPPS_QGLVIEWERWIDGET_HH
47 #define OPENMESHAPPS_QGLVIEWERWIDGET_HH
48 
49 
50 //== INCLUDES =================================================================
51 
52 
53 #include <OpenMesh/Core/Geometry/VectorT.hh>
54 #include <QtOpenGL/qgl.h>
55 #include <QGLShaderProgram>
56 #include <string>
57 #include <vector>
58 #include <map>
59 
60 
61 //== FORWARD DECLARATIONS =====================================================
62 
63 class QMenu;
64 class QActionGroup;
65 class QAction;
66 
67 //== CLASS DEFINITION =========================================================
68 
69 
70 class QGLViewerWidget : public QGLWidget
71 {
72 
73  Q_OBJECT
74 
75 public:
76  typedef QGLWidget Super;
77 
78  // Default constructor.
79  QGLViewerWidget( QWidget* _parent=0 );
80 
81  //
82  QGLViewerWidget( QGLFormat& _fmt, QWidget* _parent=0 );
83 
84  // Destructor.
85  virtual ~QGLViewerWidget();
86 
87 private:
88 
89  void init(void);
90 
91 public:
92 
93  /* Sets the center and size of the whole scene.
94  The _center is used as fixpoint for rotations and for adjusting
95  the camera/viewer (see view_all()). */
96  void set_scene_pos( const OpenMesh::Vec3f& _center, float _radius );
97 
98  /* view the whole scene: the eye point is moved far enough from the
99  center so that the whole scene is visible. */
100  void view_all();
101 
103  QAction *add_draw_mode(const std::string& _s);
104 
106  void del_draw_mode(const std::string& _s);
107 
108  const std::string& current_draw_mode() const
109  { return draw_mode_ ? draw_mode_names_[draw_mode_-1] : nomode_; }
110 
111  float radius() const { return radius_; }
112  const OpenMesh::Vec3f& center() const { return center_; }
113 
114  const GLdouble* modelview_matrix() const { return modelview_matrix_; }
115  const GLdouble* projection_matrix() const { return projection_matrix_; }
116 
117  float fovy() const { return 45.0f; }
118 
119  QAction* findAction(const char *name);
120  void addAction(QAction* action, const char* name);
121  void removeAction(const char* name);
122  void removeAction(QAction* action);
123 
124 protected:
125 
126  // draw the scene: will be called by the painGL() method.
127  virtual void draw_scene(const std::string& _draw_mode);
128 
129  double performance(void);
130 
131  void setDefaultMaterial(void);
132  void setDefaultLight(void);
133 
134 private slots:
135 
136  // popup menu clicked
137  void slotDrawMode(QAction *_mode);
138  void slotSnapshot( void );
139 
140 
141 private: // inherited
142 
143  // initialize OpenGL states (triggered by Qt)
144  void initializeGL();
145 
146  bool QGLViewerWidget::prepareShaderProgram( const QString& vertexShaderPath,
147  const QString& fragmentShaderPath );
148 
149  // draw the scene (triggered by Qt)
150  void paintGL();
151 
152  // handle resize events (triggered by Qt)
153  void resizeGL( int w, int h );
154 
155 protected:
156 
157  // Qt mouse events
158  virtual void mousePressEvent( QMouseEvent* );
159  virtual void mouseReleaseEvent( QMouseEvent* );
160  virtual void mouseMoveEvent( QMouseEvent* );
161  virtual void wheelEvent( QWheelEvent* );
162  virtual void keyPressEvent( QKeyEvent* );
163 
164 private:
165 
166  // updates projection matrix
167  void update_projection_matrix();
168 
169  // translate the scene and update modelview matrix
170  void translate(const OpenMesh::Vec3f& _trans);
171 
172  // rotate the scene (around its center) and update modelview matrix
173  void rotate(const OpenMesh::Vec3f& _axis, float _angle);
174 
175  OpenMesh::Vec3f center_;
176  float radius_;
177 
178  GLdouble projection_matrix_[16],
179  modelview_matrix_[16];
180 
181  QGLShaderProgram m_shader;
182 
183 
184  // popup menu for draw mode selection
185  QMenu* popup_menu_;
186  QActionGroup* draw_modes_group_;
187  typedef std::map<QString,QAction*> ActionMap;
188  ActionMap names_to_actions;
189  unsigned int draw_mode_;
190  unsigned int n_draw_modes_;
191  std::vector<std::string> draw_mode_names_;
192  static std::string nomode_;
193 
194 
195 
196  // virtual trackball: map 2D screen point to unit sphere
197  bool map_to_sphere(const QPoint& _point, OpenMesh::Vec3f& _result);
198 
199  QPoint last_point_2D_;
200  OpenMesh::Vec3f last_point_3D_;
201  bool last_point_ok_;
202 
203  bool isInit;
204 
205 };
206 
207 
208 //=============================================================================
209 #endif // OPENMESHAPPS_QGLVIEWERWIDGET_HH
210 //=============================================================================
211