OpenMesh
QGLViewerWidget.hh
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2023, 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#include <OpenMesh/Core/Geometry/VectorT.hh>
51#include <string>
52#include <vector>
53#include <map>
54#if QT_VERSION_MAJOR < 6
55 #include <QGLWidget>
56#else
57 #include <QtOpenGLWidgets/QOpenGLWidget>
58#endif
59
60//== FORWARD DECLARATIONS =====================================================
61
62class QMenu;
63class QActionGroup;
64class QAction;
65
66//== CLASS DEFINITION =========================================================
67
68#if QT_VERSION_MAJOR < 6
69class QGLViewerWidget : public QGLWidget
70#else
71class QGLViewerWidget : public QOpenGLWidget
72#endif
73{
74 Q_OBJECT
75
76
77public:
78 #if QT_VERSION_MAJOR < 6
79 typedef QGLWidget Super;
80 #else
81 typedef QOpenGLWidget Super;
82 #endif
83
84 // Default constructor.
85 explicit QGLViewerWidget( QWidget* _parent=0 );
86
87 // Destructor.
88 virtual ~QGLViewerWidget();
89
90private:
91
92 void init(void);
93
94public:
95
96#if QT_VERSION_MAJOR > 5
97 /* Updates the gui - used to provide backwards compability */
98 void updateGL();
99#endif
100
101 /* Sets the center and size of the whole scene.
102 The _center is used as fixpoint for rotations and for adjusting
103 the camera/viewer (see view_all()). */
104 void set_scene_pos( const OpenMesh::Vec3f& _center, float _radius );
105
106 /* view the whole scene: the eye point is moved far enough from the
107 center so that the whole scene is visible. */
108 void view_all();
109
111 QAction *add_draw_mode(const std::string& _s);
112
114 void del_draw_mode(const std::string& _s);
115
116 const std::string& current_draw_mode() const
117 { return draw_mode_ ? draw_mode_names_[draw_mode_-1] : nomode_; }
118
119 float radius() const { return radius_; }
120 const OpenMesh::Vec3f& center() const { return center_; }
121
122 const GLdouble* modelview_matrix() const { return modelview_matrix_; }
123 const GLdouble* projection_matrix() const { return projection_matrix_; }
124
125 float fovy() const { return 45.0f; }
126
127 QAction* findAction(const char *name);
128 void addAction(QAction* action, const char* name);
129 void removeAction(const char* name);
130 void removeAction(QAction* action);
131
132protected:
133
134 // draw the scene: will be called by the painGL() method.
135 virtual void draw_scene(const std::string& _draw_mode);
136
137 double performance(void);
138
139 void setDefaultMaterial(void);
140 void setDefaultLight(void);
141
142private slots:
143
144 // popup menu clicked
145 void slotDrawMode(QAction *_mode);
146 void slotSnapshot( void );
147
148
149private: // inherited
150
151 // initialize OpenGL states (triggered by Qt)
152 void initializeGL();
153
154 // draw the scene (triggered by Qt)
155 void paintGL();
156
157 // handle resize events (triggered by Qt)
158 void resizeGL( int w, int h );
159
160protected:
161
162 // Qt mouse events
163 virtual void mousePressEvent( QMouseEvent* );
164 virtual void mouseReleaseEvent( QMouseEvent* );
165 virtual void mouseMoveEvent( QMouseEvent* );
166 virtual void wheelEvent( QWheelEvent* );
167 virtual void keyPressEvent( QKeyEvent* );
168
169private:
170
171 // updates projection matrix
172 void update_projection_matrix();
173
174 // translate the scene and update modelview matrix
175 void translate(const OpenMesh::Vec3f& _trans);
176
177 // rotate the scene (around its center) and update modelview matrix
178 void rotate(const OpenMesh::Vec3f& _axis, float _angle);
179
180 OpenMesh::Vec3f center_;
181 float radius_;
182
183 GLdouble projection_matrix_[16],
184 modelview_matrix_[16];
185
186
187 // popup menu for draw mode selection
188 QMenu* popup_menu_;
189 QActionGroup* draw_modes_group_;
190 typedef std::map<QString,QAction*> ActionMap;
191 ActionMap names_to_actions;
192 unsigned int draw_mode_;
193 unsigned int n_draw_modes_;
194 std::vector<std::string> draw_mode_names_;
195 static std::string nomode_;
196
197
198
199 // virtual trackball: map 2D screen point to unit sphere
200 bool map_to_sphere(const QPoint& _point, OpenMesh::Vec3f& _result);
201
202 QPoint last_point_2D_;
203 OpenMesh::Vec3f last_point_3D_;
204 bool last_point_ok_;
205
206};
207
208
209//=============================================================================
210#endif // OPENMESHAPPS_QGLVIEWERWIDGET_HH
211//=============================================================================
212
Definition: QGLViewerWidget.hh:73
QAction * add_draw_mode(const std::string &_s)
add draw mode to popup menu, and return the QAction created
Definition: QGLViewerWidget.cc:650
void del_draw_mode(const std::string &_s)
delete draw mode from popup menu
Definition: QGLViewerWidget.cc:717

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .