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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 #ifndef OPENMESHAPPS_QGLVIEWERWIDGET_HH
51 #define OPENMESHAPPS_QGLVIEWERWIDGET_HH
52 
53 
54 //== INCLUDES =================================================================
55 
56 
57 #include <OpenMesh/Core/Geometry/VectorT.hh>
58 #include <QGLWidget>
59 #include <string>
60 #include <vector>
61 #include <map>
62 
63 
64 //== FORWARD DECLARATIONS =====================================================
65 
66 class QMenu;
67 class QActionGroup;
68 class QAction;
69 
70 //== CLASS DEFINITION =========================================================
71 
72 
73 class QGLViewerWidget : public QGLWidget
74 {
75 
76  Q_OBJECT
77 
78 public:
79  typedef QGLWidget Super;
80 
81  // Default constructor.
82  QGLViewerWidget( QWidget* _parent=0 );
83 
84  //
85  QGLViewerWidget( QGLFormat& _fmt, QWidget* _parent=0 );
86 
87  // Destructor.
88  virtual ~QGLViewerWidget();
89 
90 private:
91 
92  void init(void);
93 
94 public:
95 
96  /* Sets the center and size of the whole scene.
97  The _center is used as fixpoint for rotations and for adjusting
98  the camera/viewer (see view_all()). */
99  void set_scene_pos( const OpenMesh::Vec3f& _center, float _radius );
100 
101  /* view the whole scene: the eye point is moved far enough from the
102  center so that the whole scene is visible. */
103  void view_all();
104 
106  QAction *add_draw_mode(const std::string& _s);
107 
109  void del_draw_mode(const std::string& _s);
110 
111  const std::string& current_draw_mode() const
112  { return draw_mode_ ? draw_mode_names_[draw_mode_-1] : nomode_; }
113 
114  float radius() const { return radius_; }
115  const OpenMesh::Vec3f& center() const { return center_; }
116 
117  const GLdouble* modelview_matrix() const { return modelview_matrix_; }
118  const GLdouble* projection_matrix() const { return projection_matrix_; }
119 
120  float fovy() const { return 45.0f; }
121 
122  QAction* findAction(const char *name);
123  void addAction(QAction* action, const char* name);
124  void removeAction(const char* name);
125  void removeAction(QAction* action);
126 
127 protected:
128 
129  // draw the scene: will be called by the painGL() method.
130  virtual void draw_scene(const std::string& _draw_mode);
131 
132  double performance(void);
133 
134  void setDefaultMaterial(void);
135  void setDefaultLight(void);
136 
137 private slots:
138 
139  // popup menu clicked
140  void slotDrawMode(QAction *_mode);
141  void slotSnapshot( void );
142 
143 
144 private: // inherited
145 
146  // initialize OpenGL states (triggered by Qt)
147  void initializeGL();
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 
182  // popup menu for draw mode selection
183  QMenu* popup_menu_;
184  QActionGroup* draw_modes_group_;
185  typedef std::map<QString,QAction*> ActionMap;
186  ActionMap names_to_actions;
187  unsigned int draw_mode_;
188  unsigned int n_draw_modes_;
189  std::vector<std::string> draw_mode_names_;
190  static std::string nomode_;
191 
192 
193 
194  // virtual trackball: map 2D screen point to unit sphere
195  bool map_to_sphere(const QPoint& _point, OpenMesh::Vec3f& _result);
196 
197  QPoint last_point_2D_;
198  OpenMesh::Vec3f last_point_3D_;
199  bool last_point_ok_;
200 
201 };
202 
203 
204 //=============================================================================
205 #endif // OPENMESHAPPS_QGLVIEWERWIDGET_HH
206 //=============================================================================
207 
void del_draw_mode(const std::string &_s)
delete draw mode from popup menu
QAction * add_draw_mode(const std::string &_s)
add draw mode to popup menu, and return the QAction created