MeshNodeT.hh 9.97 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
   
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106




//=============================================================================
//
//  CLASS MeshNodeT
//
//=============================================================================

#ifndef ACG_MESHNODE_HH
#define ACG_MESHNODE_HH

//== INCLUDES =================================================================

#include "BaseNode.hh"
#include <vector>
#include <string>
#include "../ShaderUtils/GLSLShader.hh"


//== NAMESPACES ===============================================================


namespace ACG {
namespace SceneGraph {


//== CLASS DEFINITION =========================================================


/** \class MeshNodeT MeshNodeT.hh <ACG/Scenegraph/MeshNodeT.hh>

    This node is used to draw an ACG mesh.

    It uses compile-time-tags for recognizing whether
    the mesh is a TriMeshT or a PolyMeshT.

    The available draw modes are collected based on these mesh
    properties like normal or color for faces or vertices.
*/

template <class Mesh>
class MeshNodeT : public BaseNode
{

public:

  /** Default constructor.
   * @param _textureMap if the mesh has a face_texture_index and a map is provided textures are switched based on the given ids
   */
  MeshNodeT(const Mesh&  _mesh,
	    BaseNode*    _parent=0,
	    std::string  _name="<MeshNode>");

  /// Destructor
  virtual ~MeshNodeT();


  ACG_CLASSNAME(MeshNode);


  /// return available draw modes
  unsigned int availableDrawModes() const;
  /// update bounding box
107
  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax);
Jan Möbius's avatar
   
Jan Möbius committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  /// drawing
  void draw(GLState& _state, unsigned int _drawMode);
  /// picking
  void pick(GLState& _state, PickTarget _target);


  /// get mesh
  const Mesh& mesh() const { return mesh_; }


  /// update geometry buffers (vertex buffer objects)
  void update_geometry();
  /** update face indices (only for triangle meshes).  will be
      overridden by TriStripNodeT. */
  virtual void update_topology();

Dirk Wilden's avatar
Dirk Wilden committed
124
  /** \brief Setup a mapping between internal texture ids on the mesh and the ids for the loaded textures in opengl
125
126
127
128
129
   *
   * @param _map maps between an int index stored in the Mesh describing which texture to use for a face,
   *             and the GluInt name of the texture bound by the TextureNode. \n
   *             If such a map is not available ( =0 ), assume TextureNode has already bound a texture
   *             And render without switching textures
Jan Möbius's avatar
   
Jan Möbius committed
130
   */
131
132
133
134
135
136
137
138
139
140
141
  void set_texture_map( std::map< int, GLuint>* _map){ textureMap_ = _map; };

  /** \brief Setup a mapping between internal texture ids on the mesh and the properties containing texture coordinates
   *
   * @param _map maps between an int index stored in the Mesh describing which texture to use
   *             and a property name giving 2D Texture coordinates for halfedges ( texcoords for to vertex )
   */
  void set_property_map( std::map< int, std::string>* _map){ propertyMap_ = _map; };

  /** \brief Set default property name to get texture coordinates for per face texcoords
   *
Jan Möbius's avatar
Jan Möbius committed
142
   * Property has to be Mesh::TexCoord2D as a halfedge property describing vertex coordinates for
143
144
145
146
147
148
149
150
151
152
153
154
155
   * to Vertex per face. Defaults to h:texcoords2D
   */
  void set_default_halfedge_textcoord_property( std::string _default_halfedge_textcoord_property )
    { default_halfedge_textcoord_property_ = _default_halfedge_textcoord_property; };

  /** \brief Property to use when switching between multiple textures
   *
   * The given mesh property has to contain an int index of textures to use per face.
   * If it is not given, the default face_texture_index property of OpenMesh will be used.
   * If there is no texture map given via set_texture_map, texture switching will be disabled and
   * only the currently bound texture will be used.
   */
  void set_index_property_name( std::string _index_property_name) { indexPropertyName_ = _index_property_name; };
Jan Möbius's avatar
   
Jan Möbius committed
156
157
158
159
160
161
162
163
164
165
166
167
168

protected:


  // types
  enum FaceMode { FACE_NORMALS, FACE_COLORS, PER_VERTEX, FACE_NORMALS_COLORS, FACE_HALFEDGE_TEXTURED };

  // draw vertices
  void draw_vertices();
  // draw polygons. to be overridden by TriStripNodeT
  virtual void draw_faces(FaceMode _mode);

  // pick vertices
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
169
  void pick_vertices(GLState& _state, bool _front = false);
Jan Möbius's avatar
   
Jan Möbius committed
170
  // pick polygons. to be overridden by TriMeshNodeT
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
171
  void pick_faces(GLState& _state);
Jan Möbius's avatar
Jan Möbius committed
172
  // pick edges
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
173
  void pick_edges(GLState& _state, bool _front = false);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
174
175
  // pick anything
  void pick_any(GLState& _state);
Jan Möbius's avatar
   
Jan Möbius committed
176

177
178
179
  // update pick buffer sizes
  void update_pick_buffers ();

Jan Möbius's avatar
   
Jan Möbius committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  // opengl vertex array stuff
  enum ArrayType
  {
    NONE                    = 0,
    VERTEX_ARRAY            = 1,
    NORMAL_ARRAY            = 2,
    COLOR_ARRAY             = 4,
    TEXTURE_COORD_1D_ARRAY  = 8,
    TEXTURE_COORD_2D_ARRAY  = 16,
    TEXTURE_COORD_3D_ARRAY  = 32
  };

  // enable/disable vertex arrays according to the bits in _arrays
  void enable_arrays(unsigned int _arrays);



protected:

  // mesh reference
  const Mesh&  mesh_;

  // which arrays are currently enabled?
  unsigned int enabled_arrays_;

  // vertex buffer objects
  unsigned int  face_index_buffer_, vertex_buffer_, normal_buffer_;

  // index list for fast rendering (will be accessed by TriStripNodeT
  std::vector<unsigned int>  indices_;

private:

  bool vertexBufferInitialized_;
  bool normalBufferInitialized_;
  bool faceIndexBufferInitialized_;

  // Internal buffer used when rendering non float vertex coordinates
  std::vector< ACG::Vec3f > vertices_;

  // Internal buffer used when rendering non float normals
  std::vector< ACG::Vec3f > normals_;

223
224
225
226
227
228
229
230
231
232
233
  // Mapping of mesh face texture indices to gltexture id ( has to be provided externally )
  std::map< int, GLuint>* textureMap_;

  // Mapping of mesh face texture indices to coordinate property names ( has to be provided externally )
  std::map< int, std::string>* propertyMap_;

  // Property name of the per face texture coordinates
  std::string default_halfedge_textcoord_property_;

  // Property name of the per face texture index.
  std::string indexPropertyName_;
Jan Möbius's avatar
   
Jan Möbius committed
234

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
235
236
237
  // display Lists used for picking
  GLuint faceList_;
  GLuint vertexList_;
Jan Möbius's avatar
Jan Möbius committed
238
  GLuint edgeList_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
239
  GLuint anyList_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
240
241
242
243

  // do we need to update our display lists?
  bool updateFaceList_;
  bool updateVertexList_;
Jan Möbius's avatar
Jan Möbius committed
244
  bool updateEdgeList_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
245
  bool updateAnyList_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
246
247
  unsigned int faceBaseIndex_;
  unsigned int vertexBaseIndex_;
Jan Möbius's avatar
Jan Möbius committed
248
  unsigned int edgeBaseIndex_;
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
249
  unsigned int anyBaseIndex_;
250

251
252
253
254
  // buffers for draw arrays during picking
  std::vector< ACG::Vec3f > pickVertexBuf_;
  std::vector< ACG::Vec4uc > pickColorBuf_;

255
  // bounding box
256
257
  Vec3d bbMin_;
  Vec3d bbMax_;
Jan Möbius's avatar
   
Jan Möbius committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
};


//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(ACG_MESHNODE_C)
#define ACG_MESHNODE_TEMPLATES
#include "MeshNodeT.cc"
#endif
//=============================================================================
#endif // ACG_MESHNODE_HH defined
//=============================================================================