OpenMesh
OpenMesh/Apps/Unsupported/Streaming-qt4/Client/QGLViewerWidget.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 
00043 #ifndef OPENMESHAPPS_QGLVIEWERWIDGET_HH
00044 #define OPENMESHAPPS_QGLVIEWERWIDGET_HH
00045 
00046 
00047 //== INCLUDES =================================================================
00048 
00049 
00050 #include <OpenMesh/Core/Geometry/VectorT.hh>
00051 #include <QGLWidget>
00052 #include <QMenu>
00053 #include <string>
00054 #include <vector>
00055 
00056 
00057 //== FORWARD DECLARATIONS =====================================================
00058 
00059 
00060 class QPopupMenu;
00061 
00062 
00063 //== CLASS DEFINITION =========================================================
00064 
00065   
00066 class QGLViewerWidget : public QGLWidget
00067 {
00068 
00069   Q_OBJECT
00070   
00071 public:
00072    
00073   // Default constructor.
00074   QGLViewerWidget( QWidget* _parent=0, const char* _name=0 );
00075 
00076   // Destructor.
00077   virtual ~QGLViewerWidget();
00078 
00079   /* Sets the center and size of the whole scene. 
00080      The _center is used as fixpoint for rotations and for adjusting
00081      the camera/viewer (see view_all()). */
00082   void set_scene_pos( const OpenMesh::Vec3f& _center, float _radius );  
00083 
00084   /* view the whole scene: the eye point is moved far enough from the
00085      center so that the whole scene is visible. */
00086   void view_all();
00087 
00089   void add_draw_mode(std::string _s);
00090 
00091   float radius() const { return radius_; }
00092   const OpenMesh::Vec3f& center() const { return center_; }
00093 
00094   const GLdouble* modelview_matrix() const  { return modelview_matrix_;  }
00095   const GLdouble* projection_matrix() const { return projection_matrix_; }
00096   void set_modelview_matrix(const GLdouble _modelview_matrix[16])
00097   { memcpy(modelview_matrix_, _modelview_matrix, 16*sizeof(GLdouble)); }
00098   void set_projection_matrix(const GLdouble _projection_matrix[16])
00099   { memcpy(projection_matrix_, _projection_matrix, 16*sizeof(GLdouble)); }
00100 
00101   float fovy() const { return 45.0f; }
00102 
00103 protected:
00104 
00105   // draw the scene: will be called by the painGL() method.
00106   virtual void draw_scene(const std::string& _draw_mode);
00107 
00108   double performance(void);
00109 
00110 private slots:  
00111 
00112   // popup menu clicked
00113   void slotPopupMenu(int _id);  
00114   
00115 private: // inherited
00116 
00117   // initialize OpenGL states (triggered by Qt)
00118   void initializeGL();
00119 
00120   // draw the scene (triggered by Qt)
00121   void paintGL();
00122 
00123   // handle resize events (triggered by Qt)
00124   void resizeGL( int w, int h );
00125 
00126 protected:
00127    
00128   // Qt mouse events
00129   virtual void mousePressEvent( QMouseEvent* );
00130   virtual void mouseReleaseEvent( QMouseEvent* );
00131   virtual void mouseMoveEvent( QMouseEvent* );
00132   virtual void wheelEvent( QWheelEvent* );
00133   virtual void keyPressEvent( QKeyEvent* );
00134 
00135 private:
00136    
00137   // updates projection matrix
00138   void update_projection_matrix();
00139 
00140 protected:
00141   // translate the scene and update modelview matrix
00142   void translate(const OpenMesh::Vec3f& _trans);
00143 
00144   // rotate the scene (around its center) and update modelview matrix
00145   void rotate(const OpenMesh::Vec3f& _axis, float _angle);
00146 
00147   OpenMesh::Vec3f  center_;
00148   float            radius_;
00149               
00150   GLdouble    projection_matrix_[16],
00151               modelview_matrix_[16];
00152 
00153 
00154   // popup menu for draw mode selection
00155   QMenu*                    popup_menu_;
00156   unsigned int              draw_mode_;
00157   unsigned int              n_draw_modes_;
00158   std::vector<std::string>  draw_mode_names_;
00159 
00160 
00161 
00162   // virtual trackball: map 2D screen point to unit sphere
00163   bool map_to_sphere(const QPoint& _point, OpenMesh::Vec3f& _result);
00164 
00165   QPoint           last_point_2D_;
00166   OpenMesh::Vec3f  last_point_3D_;
00167   bool             last_point_ok_;
00168 
00169 };
00170 
00171 
00172 //=============================================================================
00173 #endif // OPENMESHAPPS_QGLVIEWERWIDGET_HH
00174 //=============================================================================
00175