Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DrawMesh.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openflipper.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenFlipper. *
9  * *
10  * OpenFlipper is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenFlipper is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenFlipper. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36 * *
37 * $Revision: 15046 $ *
38 * $LastChangedBy: lim $ *
39 * $Date: 2012-07-16 15:30:37 +0200 (Mo, 16 Jul 2012) $ *
40 * *
41 \*===========================================================================*/
42 
43 
44 
45 //=============================================================================
46 //
47 // CLASS DrawMeshT
48 //
49 //=============================================================================
50 
51 
52 #ifndef ACG_DRAW_MESH_HH
53 #define ACG_DRAW_MESH_HH
54 
55 
56 //== INCLUDES =================================================================
57 
58 #include <vector>
59 #include <list>
60 #include <OpenMesh/Core/Utils/Property.hh>
61 #include <OpenMesh/Core/IO/MeshIO.hh>
62 
63 #include <ACG/Config/ACGDefines.hh>
64 
65 #include <ACG/GL/GLState.hh>
66 
67 #include <ACG/GL/IRenderer.hh>
68 
69 //== FORWARDDECLARATIONS ======================================================
70 
71 
72 //== NAMESPACES ===============================================================
73 
74 namespace ACG {
75 
76 //== CLASS DEFINITION =========================================================
77 
78 
103 template <class Mesh>
105 {
106 private:
107 
108 #pragma pack(push, 1)
109 
110  struct Vertex
111  {
112  Vertex();
113 
114  float pos[3];
115  float tex[2];
116  float n[3];
117 // float tan[4]; /*!< tangent vector + parity */
118 
119  unsigned int vcol;
120  unsigned int fcol;
122  int equals(const Vertex& r);
123  };
124 
126  struct VertexC
127  {
128  unsigned short pos[3];
129  unsigned short u, v;
130  unsigned int n;
131 // unsigned int tan;
132 
133  unsigned int vcol;
134  unsigned int fcol;
135  };
136 #pragma pack(pop)
137 
138  struct Subset
139  {
140  int materialID;
141  unsigned long startIndex;
142  unsigned long numTris;
143  };
144 
145  enum REBUILD_TYPE {REBUILD_NONE = 0, REBUILD_FULL = 1, REBUILD_GEOMETRY = 2, REBUILD_TOPOLOGY = 4, REBUILD_TEXTURES = 5};
146 
147 
148 public:
149 
150  DrawMeshT(Mesh& _mesh);
151  virtual ~DrawMeshT();
152 
153  void disableColors() {colorMode_ = 0;}
154  void usePerVertexColors() {colorMode_ = 1;}
155  void usePerFaceColors() {colorMode_ = 2;}
156 
157  void setFlatShading() {flatMode_ = 1;}
158  void setSmoothShading() {flatMode_ = 0;}
159 
160  void usePerVertexTexcoords() {textureMode_ = 0;}
161  void usePerHalfedgeTexcoords() {textureMode_ = 1;}
162  void usePerVertexNormals() {halfedgeNormalMode_ = 0;}
163  void usePerHalfedgeNormals() {halfedgeNormalMode_ = 1;}
164 
167  void bindBuffers();
168 
171  void bindBuffersToRenderObject(RenderObject* _obj);
172 
175  void unbindBuffers();
176 
182  void draw(std::map< int, GLuint>* _textureMap);
183 
191  void addTriRenderObjects(IRenderer* _renderer, const RenderObject* _baseObj, std::map< int, GLuint>* _textureMap);
192 
195  void drawLines();
196 
199  void addLineRenderObjects(IRenderer* _renderer, const RenderObject* _baseObj);
200 
201 
204  void drawVertices();
205 
208  void addPointRenderObjects(IRenderer* _renderer, const RenderObject* _baseObj);
209 
210 
211  unsigned int getNumTris() const {return numTris_;}
212  unsigned int getNumVerts() const {return numVerts_;}
213  unsigned int getNumSubsets() const {return numSubsets_;}
214 
215 
219  unsigned int getMemoryUsage(bool _printReport = false);
220 
221  // The updateX functions give a hint on what to update.
222  // may perform a full rebuild internally!
223 
226  void updateTopology() {rebuild_ |= REBUILD_TOPOLOGY;}
227 
230  void updateGeometry() {rebuild_ |= REBUILD_GEOMETRY;}
231 
234  void updateTextures() {rebuild_ |= REBUILD_TEXTURES;}
235 
239  void updateFull() {rebuild_ |= REBUILD_FULL;}
240 
245  unsigned int getNumTextures();
246 
255  void setTextureIndexPropertyName( std::string _indexPropertyName );
256 
261  const std::string& getTextureIndexPropertyName() const { return textureIndexPropertyName_; };
262 
271  void setPerFaceTextureCoordinatePropertyName( std::string _perFaceTextureCoordinatePropertyName );
272 
280 
289 
290 private:
291  // processing pipeline:
292 
296  void rebuild();
297 
298 
307  unsigned int convertToTriangleMesh(unsigned int* _dstIndexBuf, unsigned int _maxFaceVertexCount);
308 
315  void createBigVertexBuf(Vertex* _dstVertexBuf, unsigned int* _dstVertexMap, const unsigned int* _indexBuf);
316 
324  void readVertex(Vertex* _pDst,
325  typename Mesh::VertexHandle _vh,
326  typename Mesh::HalfedgeHandle _hh,
327  typename Mesh::FaceHandle _fh);
328 
332  void removeIsolatedVerts();
333 
344  unsigned int weldVertices(Vertex* _dstVertexBuf,
345  const Vertex* _srcVertexBuf,
346  unsigned int* _dstIndexBuf,
347  unsigned int* _dstVertexMap,
348  const unsigned int* _srcVertexMap,
349  std::list< std::pair<unsigned int, unsigned int> >& _duplicatesMap);
350 
361  void sortTrisByMaterial(unsigned int* _dstIndexBuf, const unsigned int* _srcIndexBuf);
362 
373  void optimizeTris(unsigned int* _dstIndexBuf, unsigned int* _srcIndexBuf);
374 
387  void optimizeVerts(Vertex* _dstVertexBuf,
388  const Vertex* _srcVertexBuf,
389  unsigned int* _inOutIndexBuf,
390  const unsigned int* _srcVertexMap);
391 
392 
396  void createVBO();
397 
401  void createIBO();
402 
407 
408 public:
409  // color picking
410 
418  void updatePickingVertices(ACG::GLState& _state , uint _offset = 0);
419 
429  if ( !pickVertColBuf_.empty() )
430  return &(pickVertColBuf_)[0];
431  else {
432  std::cerr << "Illegal request to pickVertexColorBuffer when buffer is empty!" << std::endl;
433  return 0;
434  }
435  };
436 
446  if ( !pickVertBuf_.empty() )
447  return &(pickVertBuf_)[0];
448  else {
449  std::cerr << "Illegal request to pickVertexBuffer when buffer is empty!" << std::endl;
450  return 0;
451  }
452  };
453 
454 private:
455 
457  std::vector< ACG::Vec3f > pickVertBuf_;
459  std::vector< ACG::Vec4uc > pickVertColBuf_;
460 
461 
462 
463 public:
464 
474  void updatePickingEdges(ACG::GLState& _state , uint _offset = 0 );
475 
485  if ( !pickEdgeBuf_.empty() )
486  return &(pickEdgeBuf_)[0];
487  else {
488  std::cerr << "Illegal request to pickEdgeColorBuffer when buffer is empty!" << std::endl;
489  return 0;
490  }
491  }
492 
493 private:
494 
495  std::vector< ACG::Vec4uc > pickEdgeBuf_;
496 
497 
498 
499 public:
500 
505  void updatePickingFaces(ACG::GLState& _state );
506 
516  if ( !pickFaceColBuf_.empty() )
517  return &(pickFaceColBuf_)[0];
518  else {
519  std::cerr << "Illegal request to pickFaceColorBuffer when buffer is empty!" << std::endl;
520  return 0;
521  }
522  }
523 
533  if ( !pickFaceVertexBuf_.empty() )
534  return &(pickFaceVertexBuf_)[0];
535  else {
536  std::cerr << "Illegal request to pickFaceVertexBuffer when buffer is empty!" << std::endl;
537  return 0;
538  }
539  }
540 
541 private:
542 
543  std::vector< ACG::Vec3f > pickFaceVertexBuf_;
544  std::vector< ACG::Vec4uc > pickFaceColBuf_;
545 
546 
547 public:
555  void updatePickingAny(ACG::GLState& _state );
556 
566  if ( !pickAnyFaceColBuf_.empty() )
567  return &(pickAnyFaceColBuf_)[0];
568  else {
569  std::cerr << "Illegal request to pickAnyFaceColorBuffer when buffer is empty!" << std::endl;
570  return 0;
571  }
572  }
573 
583  if ( !pickAnyEdgeColBuf_.empty() )
584  return &(pickAnyEdgeColBuf_)[0];
585  else {
586  std::cerr << "Illegal request to pickAnyEdgeColorBuffer when buffer is empty!" << std::endl;
587  return 0;
588  }
589  }
590 
600  if ( !pickAnyVertexColBuf_.empty() )
601  return &(pickAnyVertexColBuf_)[0];
602  else {
603  std::cerr << "Illegal request to pickAnyVertexColorBuffer when buffer is empty!" << std::endl;
604  return 0;
605  }
606  }
607 
608 private:
609 
610  std::vector< ACG::Vec4uc > pickAnyFaceColBuf_;
611  std::vector< ACG::Vec4uc > pickAnyEdgeColBuf_;
612  std::vector< ACG::Vec4uc > pickAnyVertexColBuf_;
613 
614 
615 private:
616 
617  // small helper functions
618 
628  unsigned int countTris(unsigned int* _pOutMaxPolyVerts = 0);
629 
637  int getTextureIDofTri(unsigned int _tri);
638 
639 private:
640 
642  Mesh& mesh_;
643 
644  unsigned int numTris_, numVerts_;
645 
647  unsigned int* indices_;
648 
651 
653  unsigned int rebuild_;
654 
658  unsigned int prevNumFaces_,prevNumVerts_;
659 
660 
661  // per material/texture subsets
662  unsigned int numSubsets_;
663  Subset* subsets_;
664 
665  GLuint vbo_,
666  ibo_;
667 
669  GLuint lineIBO_;
670 
672  GLenum indexType_;
673 
678 
681 
684 
687 
690 
693 
694 
698  unsigned int* triToFaceMap_;
699 
701  unsigned int* vertexMap_;
702 
706  unsigned int* invVertexMap_;
707 
708 
711 
714 
717 
720 
723 
724 
725  //========================================================================
726  // internal processing
727  // temporal buffer allocations to avoid memory requests while updating
728  //========================================================================
729 
730  unsigned int* indicesTmp_;
731  Vertex* verticesTmp_;
732 
733  //========================================================================
734  // texture handling
735  //========================================================================
736 
745 
752 
753 
754 public:
755  //========================================================================
756  // per edge buffers
757 
763  void invalidatePerEdgeBuffers() {updatePerEdgeBuffers_ = 1;}
764 
769  void updatePerEdgeBuffers();
770 
776 
782 
788  void invalidatePerHalfedgeBuffers() {updatePerHalfedgeBuffers_ = 1;}
789 
795 
801 
807 
808 
812 
813 
817 
821 
825 
826 
827 private:
828  int updatePerEdgeBuffers_;
829  std::vector<ACG::Vec3f> perEdgeVertexBuf_;
830  std::vector<ACG::Vec4f> perEdgeColorBuf_;
831 
832  int updatePerHalfedgeBuffers_;
833  std::vector<ACG::Vec3f> perHalfedgeVertexBuf_;
834  std::vector<ACG::Vec4f> perHalfedgeColorBuf_;
835 
843  typename Mesh::Point halfedge_point(const typename Mesh::HalfedgeHandle _heh);
844 
845 };
846 
847 
848 
849 //=============================================================================
850 } // namespace ACG
851 //=============================================================================
852 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_DRAW_MESH_C)
853 #define ACG_DRAW_MESH_TEMPLATES
854 #include "DrawMesh.cc"
855 #endif
856 //=============================================================================
857 #endif // ACG_DRAW_MESH_HH defined
858 //=============================================================================