Developer Documentation
QGLViewerWidget.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 
45 #ifndef OPENMESHAPPS_QGLVIEWERWIDGET_HH
46 #define OPENMESHAPPS_QGLVIEWERWIDGET_HH
47 
48 
49 //== INCLUDES =================================================================
50 
51 
52 #include <OpenMesh/Core/Geometry/VectorT.hh>
53 #include <QGLWidget>
54 #include <string>
55 #include <vector>
56 #include <map>
57 
58 
59 //== FORWARD DECLARATIONS =====================================================
60 
61 class QMenu;
62 class QActionGroup;
63 class QAction;
64 
65 //== CLASS DEFINITION =========================================================
66 
67 
68 class QGLViewerWidget : public QGLWidget
69 {
70 
71  Q_OBJECT
72 
73 public:
74  typedef QGLWidget Super;
75 
76  // Default constructor.
77  explicit QGLViewerWidget( QWidget* _parent=0 );
78 
79  //
80  QGLViewerWidget( QGLFormat& _fmt, QWidget* _parent=0 );
81 
82  // Destructor.
83  virtual ~QGLViewerWidget();
84 
85 private:
86 
87  void init(void);
88 
89 public:
90 
91  /* Sets the center and size of the whole scene.
92  The _center is used as fixpoint for rotations and for adjusting
93  the camera/viewer (see view_all()). */
94  void set_scene_pos( const OpenMesh::Vec3f& _center, float _radius );
95 
96  /* view the whole scene: the eye point is moved far enough from the
97  center so that the whole scene is visible. */
98  void view_all();
99 
101  QAction *add_draw_mode(const std::string& _s);
102 
104  void del_draw_mode(const std::string& _s);
105 
106  const std::string& current_draw_mode() const
107  { return draw_mode_ ? draw_mode_names_[draw_mode_-1] : nomode_; }
108 
109  float radius() const { return radius_; }
110  const OpenMesh::Vec3f& center() const { return center_; }
111 
112  const GLdouble* modelview_matrix() const { return modelview_matrix_; }
113  const GLdouble* projection_matrix() const { return projection_matrix_; }
114 
115  float fovy() const { return 45.0f; }
116 
117  QAction* findAction(const char *name);
118  void addAction(QAction* action, const char* name);
119  void removeAction(const char* name);
120  void removeAction(QAction* action);
121 
122 protected:
123 
124  // draw the scene: will be called by the painGL() method.
125  virtual void draw_scene(const std::string& _draw_mode);
126 
127  double performance(void);
128 
129  void setDefaultMaterial(void);
130  void setDefaultLight(void);
131 
132 private slots:
133 
134  // popup menu clicked
135  void slotDrawMode(QAction *_mode);
136  void slotSnapshot( void );
137 
138 
139 private: // inherited
140 
141  // initialize OpenGL states (triggered by Qt)
142  void initializeGL();
143 
144  // draw the scene (triggered by Qt)
145  void paintGL();
146 
147  // handle resize events (triggered by Qt)
148  void resizeGL( int w, int h );
149 
150 protected:
151 
152  // Qt mouse events
153  virtual void mousePressEvent( QMouseEvent* );
154  virtual void mouseReleaseEvent( QMouseEvent* );
155  virtual void mouseMoveEvent( QMouseEvent* );
156  virtual void wheelEvent( QWheelEvent* );
157  virtual void keyPressEvent( QKeyEvent* );
158 
159 private:
160 
161  // updates projection matrix
162  void update_projection_matrix();
163 
164  // translate the scene and update modelview matrix
165  void translate(const OpenMesh::Vec3f& _trans);
166 
167  // rotate the scene (around its center) and update modelview matrix
168  void rotate(const OpenMesh::Vec3f& _axis, float _angle);
169 
170  OpenMesh::Vec3f center_;
171  float radius_;
172 
173  GLdouble projection_matrix_[16],
174  modelview_matrix_[16];
175 
176 
177  // popup menu for draw mode selection
178  QMenu* popup_menu_;
179  QActionGroup* draw_modes_group_;
180  typedef std::map<QString,QAction*> ActionMap;
181  ActionMap names_to_actions;
182  unsigned int draw_mode_;
183  unsigned int n_draw_modes_;
184  std::vector<std::string> draw_mode_names_;
185  static std::string nomode_;
186 
187 
188 
189  // virtual trackball: map 2D screen point to unit sphere
190  bool map_to_sphere(const QPoint& _point, OpenMesh::Vec3f& _result);
191 
192  QPoint last_point_2D_;
193  OpenMesh::Vec3f last_point_3D_;
194  bool last_point_ok_;
195 
196 };
197 
198 
199 //=============================================================================
200 #endif // OPENMESHAPPS_QGLVIEWERWIDGET_HH
201 //=============================================================================
202 
QAction * add_draw_mode(const std::string &_s)
add draw mode to popup menu, and return the QAction created
void del_draw_mode(const std::string &_s)
delete draw mode from popup menu