OpenMesh
OpenMesh/Apps/QtViewer/MeshViewerWidgetT.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_MESHVIEWERWIDGETT_HH
00044 #define OPENMESHAPPS_MESHVIEWERWIDGETT_HH
00045 
00046 
00047 //== INCLUDES =================================================================
00048 
00049 #include <string>
00050 #include <OpenMesh/Core/IO/MeshIO.hh>
00051 #include <OpenMesh/Core/IO/Options.hh>
00052 #include <OpenMesh/Core/Utils/GenProg.hh>
00053 #include <OpenMesh/Core/Utils/color_cast.hh>
00054 #include <OpenMesh/Core/Mesh/Attributes.hh>
00055 #include <OpenMesh/Tools/Utils/StripifierT.hh>
00056 #include <OpenMesh/Tools/Utils/Timer.hh>
00057 #include <OpenMesh/Apps/QtViewer/QGLViewerWidget.hh>
00058 
00059 
00060 //== FORWARDS =================================================================
00061 
00062 class QImage;
00063 
00064 
00065 //== CLASS DEFINITION =========================================================
00066 
00067               
00068 template <typename M>
00069 class MeshViewerWidgetT : public QGLViewerWidget
00070 {
00071 public:
00072 
00073   typedef M                             Mesh;
00074   typedef OpenMesh::StripifierT<Mesh>   MyStripifier;
00075 public:
00076 
00078   MeshViewerWidgetT(QWidget* _parent=0)
00079     : QGLViewerWidget(_parent),
00080       f_strips_(false), 
00081       tex_id_(0),
00082       tex_mode_(GL_MODULATE),
00083       strips_(mesh_),
00084       use_color_(true),
00085       show_vnormals_(false),
00086       show_fnormals_(false)
00087   {
00088     add_draw_mode("Points");
00089     add_draw_mode("Hidden-Line");
00090 #if defined(OM_USE_OSG) && OM_USE_OSG
00091     add_draw_mode("OpenSG Indices");
00092 #endif
00093   }
00094   
00096   ~MeshViewerWidgetT() {}
00097   
00098 public:
00099 
00101   virtual bool open_mesh(const char* _filename, OpenMesh::IO::Options _opt);
00102   
00104   virtual bool open_texture( const char *_filename );
00105   bool set_texture( QImage& _texsrc );
00106  
00107   void enable_strips();
00108   void disable_strips();  
00109   
00110 
00111   Mesh& mesh() { return mesh_; }
00112   const Mesh& mesh() const { return mesh_; }
00113   
00114 protected:
00115   
00117   virtual void draw_scene(const std::string& _draw_mode);
00118   
00119 protected:
00120   
00122   virtual void draw_openmesh(const std::string& _drawmode);
00123 
00124 
00125   void glVertex( const typename Mesh::VertexHandle _vh )
00126   { glVertex3fv( &mesh_.point( _vh )[0] ); }
00127 
00128   void glVertex( const typename Mesh::Point& _p )
00129   { glVertex3fv( &_p[0] ); }
00130   
00131   void glNormal( const typename Mesh::VertexHandle _vh )
00132   { glNormal3fv( &mesh_.normal( _vh )[0] ); }
00133 
00134   void glTexCoord( const typename Mesh::VertexHandle _vh )
00135   { glTexCoord2fv( &mesh_.texcoord(_vh)[0] ); }
00136   
00137   void glColor( const typename Mesh::VertexHandle _vh )
00138   { glColor3ubv( &mesh_.color(_vh)[0] ); }
00139   
00140   // face properties
00141 
00142   void glNormal( const typename Mesh::FaceHandle _fh )
00143   { glNormal3fv( &mesh_.normal( _fh )[0] ); }
00144 
00145   void glColor( const typename Mesh::FaceHandle _fh )
00146   { glColor3ubv( &mesh_.color(_fh)[0] ); }
00147 
00148   void glMaterial( const typename Mesh::FaceHandle _fh, 
00149                    int _f=GL_FRONT_AND_BACK, int _m=GL_DIFFUSE )
00150   { 
00151     OpenMesh::Vec3f c=OpenMesh::color_cast<OpenMesh::Vec3f>(mesh_.color(_fh));
00152     OpenMesh::Vec4f m( c[0], c[1], c[2], 1.0f );
00153 
00154     glMaterialfv(_f, _m, &m[0]); 
00155   }
00156 
00157 
00158 protected: // Strip support
00159   
00160   void compute_strips(void)
00161   {
00162     if (f_strips_)
00163     {
00164       strips_.clear();
00165       strips_.stripify();
00166     }
00167   }    
00168 
00169 protected: // inherited
00170    
00171   virtual void keyPressEvent( QKeyEvent* _event);
00172 
00173 protected:
00174    
00175   bool                   f_strips_; // enable/disable strip usage
00176   GLuint                 tex_id_;
00177   GLint                  tex_mode_;
00178   OpenMesh::IO::Options  opt_; // mesh file contained texcoords?
00179   
00180   Mesh                   mesh_;
00181   MyStripifier           strips_;
00182   bool                   use_color_;
00183   bool                   show_vnormals_;
00184   bool                   show_fnormals_;
00185   float                  normal_scale_;
00186   OpenMesh::FPropHandleT< typename Mesh::Point > fp_normal_base_;
00187 };
00188 
00189 
00190 //=============================================================================
00191 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESHAPPS_MESHVIEWERWIDGET_CC)
00192 #  define OPENMESH_MESHVIEWERWIDGET_TEMPLATES
00193 #  include "MeshViewerWidgetT.cc"
00194 #endif
00195 //=============================================================================
00196 #endif // OPENMESHAPPS_MESHVIEWERWIDGETT_HH defined
00197 //=============================================================================
00198