Developer Documentation
MeshViewerWidgetT.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 #pragma once
46 
47 
48 //== INCLUDES =================================================================
49 
50 #include <string>
51 #include <OpenMesh/Core/IO/MeshIO.hh>
52 #include <OpenMesh/Core/IO/Options.hh>
53 #include <OpenMesh/Core/Utils/GenProg.hh>
54 #include <OpenMesh/Core/Utils/color_cast.hh>
56 #include <OpenMesh/Tools/Utils/StripifierT.hh>
58 #include <OpenMesh/Apps/QtViewer/QGLViewerWidget.hh>
59 
60 
61 //== FORWARDS =================================================================
62 
63 class QImage;
64 
65 
66 //== CLASS DEFINITION =========================================================
67 
68 
69 template <typename M>
71 {
72 
73 public:
74 
75  typedef M Mesh;
77 public:
78 
80  explicit MeshViewerWidgetT(QWidget* _parent=0)
81  : QGLViewerWidget(_parent),
82  f_strips_(false),
83  tex_id_(0),
84  tex_mode_(GL_MODULATE),
85  strips_(mesh_),
86  use_color_(true),
87  show_vnormals_(false),
88  show_fnormals_(false),
89  normal_scale_(1.0)
90  {
91  add_draw_mode("Points");
92  add_draw_mode("Hidden-Line");
93 #if defined(OM_USE_OSG) && OM_USE_OSG
94  add_draw_mode("OpenSG Indices");
95 #endif
96  }
97 
100 
101 public:
102 
104  virtual bool open_mesh(const char* _filename, OpenMesh::IO::Options _opt);
105 
107  virtual bool open_texture( const char *_filename );
108  bool set_texture( QImage& _texsrc );
109 
110  void enable_strips();
111  void disable_strips();
112 
113 
114  Mesh& mesh() { return mesh_; }
115  const Mesh& mesh() const { return mesh_; }
116 
117 protected:
118 
120  virtual void draw_scene(const std::string& _draw_mode) override;
121 
122 protected:
123 
125  virtual void draw_openmesh(const std::string& _drawmode);
126 
127 
128  void glVertex( const typename Mesh::VertexHandle _vh )
129  { glVertex3fv( &mesh_.point( _vh )[0] ); }
130 
131  void glVertex( const typename Mesh::Point& _p )
132  { glVertex3fv( &_p[0] ); }
133 
134  void glNormal( const typename Mesh::VertexHandle _vh )
135  { glNormal3fv( &mesh_.normal( _vh )[0] ); }
136 
137  void glTexCoord( const typename Mesh::VertexHandle _vh )
138  { glTexCoord2fv( &mesh_.texcoord(_vh)[0] ); }
139 
140  void glColor( const typename Mesh::VertexHandle _vh )
141  { glColor3ubv( &mesh_.color(_vh)[0] ); }
142 
143  // face properties
144 
145  void glNormal( const typename Mesh::FaceHandle _fh )
146  { glNormal3fv( &mesh_.normal( _fh )[0] ); }
147 
148  void glColor( const typename Mesh::FaceHandle _fh )
149  { glColor3ubv( &mesh_.color(_fh)[0] ); }
150 
151  void glMaterial( const typename Mesh::FaceHandle _fh,
152  int _f=GL_FRONT_AND_BACK, int _m=GL_DIFFUSE )
153  {
154  OpenMesh::Vec3f c=OpenMesh::color_cast<OpenMesh::Vec3f>(mesh_.color(_fh));
155  OpenMesh::Vec4f m( c[0], c[1], c[2], 1.0f );
156 
157  glMaterialfv(_f, _m, &m[0]);
158  }
159 
160 
161 protected: // Strip support
162 
163  void compute_strips(void)
164  {
165  if (f_strips_)
166  {
167  strips_.clear();
168  strips_.stripify();
169  }
170  }
171 
172 protected: // inherited
173 
174  virtual void keyPressEvent( QKeyEvent* _event) override;
175 
176 protected:
177 
178  bool f_strips_; // enable/disable strip usage
179  GLuint tex_id_;
180  GLint tex_mode_;
181  OpenMesh::IO::Options opt_; // mesh file contained texcoords?
182 
183  Mesh mesh_;
184  MyStripifier strips_;
185  bool use_color_;
186  bool show_vnormals_;
187  bool show_fnormals_;
188  float normal_scale_;
190 };
191 
192 
193 //=============================================================================
194 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESHAPPS_MESHVIEWERWIDGET_CC)
195 # define OPENMESH_MESHVIEWERWIDGET_TEMPLATES
196 # include "MeshViewerWidgetT_impl.hh"
197 #endif
198 //=============================================================================
199 
QAction * add_draw_mode(const std::string &_s)
add draw mode to popup menu, and return the QAction created
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
virtual void draw_scene(const std::string &_draw_mode) override
inherited drawing method
void clear()
delete all strips
Definition: StripifierT.hh:100
virtual bool open_texture(const char *_filename)
load texture
virtual bool open_mesh(const char *_filename, OpenMesh::IO::Options _opt)
open mesh
size_t stripify()
Compute triangle strips, returns number of strips.
MeshViewerWidgetT(QWidget *_parent=0)
default constructor
Set options for reader/writer modules.
Definition: Options.hh:90
virtual void draw_openmesh(const std::string &_drawmode)
draw the mesh
~MeshViewerWidgetT()
destructor
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136