GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_mesh.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #ifndef GLC_MESH_H_
26 #define GLC_MESH_H_
27 
28 #include <QHash>
29 #include <QList>
30 #include "../maths/glc_vector2df.h"
31 #include "../maths/glc_vector3df.h"
32 #include "../glc_global.h"
33 #include "../shading/glc_material.h"
34 #include "glc_meshdata.h"
35 #include "glc_geometry.h"
36 #include "glc_primitivegroup.h"
37 #include "../glc_state.h"
38 #include "../shading/glc_selectionmaterial.h"
39 
40 #include "../glc_config.h"
41 
44 
48 
50 {
51  friend QDataStream &operator<<(QDataStream &, const GLC_Mesh &);
52  friend QDataStream &operator>>(QDataStream &, GLC_Mesh &);
53 
54 public:
55  typedef QHash<GLC_uint, GLC_PrimitiveGroup*> LodPrimitiveGroups;
56  typedef QHash<const int, LodPrimitiveGroups*> PrimitiveGroupsHash;
57 
59 
61 
62 public:
64  GLC_Mesh();
65 
67  GLC_Mesh(const GLC_Mesh&);
68 
70  GLC_Mesh& operator=(const GLC_Mesh&);
71 
73  virtual ~GLC_Mesh();
75 
76 
78 
79 public:
81  static quint32 chunckID();
82 
84  virtual unsigned int faceCount(int lod) const;
85 
87  virtual unsigned int VertexCount() const;
88 
90  inline unsigned int numberOfNormals() const
91  { return m_NumberOfNormals;}
92 
94  virtual const GLC_BoundingBox& boundingBox(void);
95 
97  virtual GLC_Geometry* clone() const;
98 
100  inline bool ColorPearVertexIsAcivated() const
101  {return m_ColorPearVertex;}
102 
104  inline int lodCount() const
105  {return m_MeshData.lodCount();}
106 
108  inline GLfloatVector positionVector() const
109  {return m_MeshData.positionVector();}
110 
112  inline GLfloatVector normalVector() const
113  {return m_MeshData.normalVector();}
114 
116  inline GLfloatVector texelVector() const
117  {return m_MeshData.texelVector();}
118 
120  bool containsTriangles(int lod, GLC_uint materialId) const;
121 
123 
124  QVector<GLuint> getTrianglesIndex(int lod, GLC_uint materialId) const;
125 
127  IndexList getEquivalentTrianglesStripsFansIndex(int lod, GLC_uint materialId);
128 
130  int numberOfTriangles(int lod, GLC_uint materialId) const;
131 
133  bool containsStrips(int lod, GLC_uint materialId) const;
134 
136 
137  QList<QVector<GLuint> > getStripsIndex(int lod, GLC_uint materialId) const;
138 
140  int numberOfStrips(int lod, GLC_uint materialId) const;
141 
143  bool containsFans(int lod, GLC_uint materialId) const;
144 
146 
147  QList<QVector<GLuint> > getFansIndex(int lod, GLC_uint materialId) const;
148 
150  int numberOfFans(int lod, GLC_uint materialId) const;
151 
153  inline bool containsLod(int lod) const
154  {return (NULL != m_MeshData.getLod(lod));}
155 
157  inline bool lodContainsMaterial(int lod, GLC_uint materialId) const
158  {
159  if (!m_PrimitiveGroups.contains(lod))return false;
160  else return m_PrimitiveGroups.value(lod)->contains(materialId);
161  }
162 
164 
165  inline double getLodAccuracy(int lod) const
166  {
167  Q_ASSERT(containsLod(lod));
168  return m_MeshData.getLod(lod)->accuracy();
169  }
170 
172  inline GLC_uint nextPrimitiveLocalId() const
173  {return m_NextPrimitiveLocalId;}
174 
176  GLC_Material* MaterialOfPrimitiveId(GLC_uint id, int lod= 0) const;
177 
179  QSet<GLC_uint> setOfPrimitiveId() const;
180 
182  inline bool isEmpty() const
183  {return m_MeshData.isEmpty();}
184 
186  inline QColor wireColor() const
187  {return m_WireColor;}
188 
190  GLC_Mesh* createMeshOfGivenLod(int lodIndex);
191 
193  GLC_Mesh* createMeshFromGivenLod(int lodIndex);
194 
196  GLC_Mesh& transformVertice(const GLC_Matrix4x4& matrix);
197 
199  virtual double volume();
200 
202 
203 
205 
206 public:
207 
209  virtual void clear();
210 
212  void clearMeshWireAndBoundingBox();
213 
215  inline void addVertice(const GLfloatVector& vertices)
216  {
217  *(m_MeshData.positionVectorHandle())+= vertices;
218  m_NumberOfVertice+= vertices.size() / 3;
219  }
220 
222  inline void addNormals(const GLfloatVector& normals)
223  {
224  *(m_MeshData.normalVectorHandle())+= normals;
225  m_NumberOfNormals+= normals.size() / 3;
226  }
227 
229  inline void addTexels(const GLfloatVector& texels)
230  {*(m_MeshData.texelVectorHandle())+= texels;}
231 
233  inline void addColors(const GLfloatVector& colors)
234  {*(m_MeshData.colorVectorHandle())+= colors;}
235 
237  GLC_uint addTriangles(GLC_Material*, const IndexList&, const int lod= 0, double accuracy= 0.0);
238 
240  GLC_uint addTrianglesStrip(GLC_Material*, const IndexList&, const int lod= 0, double accuracy= 0.0);
241 
243  GLC_uint addTrianglesFan(GLC_Material*, const IndexList&, const int lod= 0, double accuracy= 0.0);
244 
246  void reverseNormals();
247 
249  inline void setColorPearVertex(bool flag)
250  {m_ColorPearVertex= flag;}
251 
253  void finish();
254 
256  virtual void setCurrentLod(const int);
257 
259  virtual void replaceMasterMaterial(GLC_Material*);
260 
262  void replaceMaterial(const GLC_uint, GLC_Material*);
263 
265  inline void setNextPrimitiveLocalId(GLC_uint id)
266  {m_NextPrimitiveLocalId= id;}
267 
269  inline void setWireColor(const QColor& color)
270  {m_WireColor= color;}
271 
273  virtual void copyVboToClientSide();
274 
276  virtual void releaseVboClientSide(bool update);
277 
279  virtual void setVboUsage(bool usage);
280 
282 
284 
286 
287 public:
289 
293  void loadFromDataStream(QDataStream&, const MaterialHash&, const QHash<GLC_uint, GLC_uint>&);
294 
296  void saveToDataStream(QDataStream&) const;
297 
299 
300 
302 
303 protected:
304 
306 
307  virtual void glDraw(const GLC_RenderProperties&);
308 
310 
312 
314 
315 private:
317  GLC_uint setCurrentMaterial(GLC_Material*, const int, double);
318 
320  void fillVbosAndIbos();
321 
323  void finishSerialized();
324 
326  //void finishVbo();
327 
329  void moveIndexToMeshDataLod();
330 
332  inline void vboDrawPrimitivesOf(GLC_PrimitiveGroup*);
333 
335  inline void vertexArrayDrawPrimitivesOf(GLC_PrimitiveGroup*);
336 
338  inline void vboDrawInSelectionModePrimitivesOf(GLC_PrimitiveGroup*);
339 
341  inline void vertexArrayDrawInSelectionModePrimitivesOf(GLC_PrimitiveGroup*);
342 
344  inline void vboDrawPrimitivesGroupOf(GLC_PrimitiveGroup*, GLC_Material*, bool, bool, QHash<GLC_uint, GLC_Material*>*);
345 
347  inline void vertexArrayDrawPrimitivesGroupOf(GLC_PrimitiveGroup*, GLC_Material*, bool, bool, QHash<GLC_uint, GLC_Material*>*);
348 
350  inline void vboDrawSelectedPrimitivesGroupOf(GLC_PrimitiveGroup*, GLC_Material*, bool, bool, const GLC_RenderProperties&);
351 
353  inline void vertexArrayDrawSelectedPrimitivesGroupOf(GLC_PrimitiveGroup*, GLC_Material*, bool, bool, const GLC_RenderProperties&);
354 
356  inline void activateVboAndIbo();
357 
359  inline void activateVertexArray();
360 
362  void normalRenderLoop(const GLC_RenderProperties&, bool);
363 
365  void OverwriteMaterialRenderLoop(const GLC_RenderProperties&, bool);
366 
368  void OverwriteTransparencyRenderLoop(const GLC_RenderProperties&, bool);
369 
371  void OverwriteTransparencyAndMaterialRenderLoop(const GLC_RenderProperties&, bool);
372 
374  void bodySelectionRenderLoop(bool);
375 
377  void primitiveSelectionRenderLoop(bool);
378 
380  void primitiveRenderLoop(const GLC_RenderProperties&, bool);
381 
383  void primitiveSelectedRenderLoop(const GLC_RenderProperties&, bool);
384 
386  void outlineSilhouetteRenderLoop(const GLC_RenderProperties&, bool);
387 
389  void copyIndex(int lod, GLC_Mesh* pLodMesh, QHash<GLuint, GLuint>& sourceToTargetIndexMap, QHash<GLuint, GLuint>& tagetToSourceIndexMap, int& maxIndex, int targetLod);
390 
392  void copyBulkData(GLC_Mesh* pLodMesh, const QHash<GLuint, GLuint>& tagetToSourceIndexMap, int maxIndex);
393 
395  IndexList equivalentTrianglesIndexOfstripsIndex(int lodIndex, GLC_uint materialId);
396 
398  IndexList equivalentTrianglesIndexOfFansIndex(int lodIndex, GLC_uint materialId);
399 
400 
402 
403 
405 // Private members
407 private:
410 
413 
416 
418  unsigned int m_NumberOfVertice;
419 
421  unsigned int m_NumberOfNormals;
422 
425 
428 
431 
433  static quint32 m_ChunkId;
434 
435 };
436 
437 // Inline functions
438 
439 // Use VBO to Draw triangles from the specified GLC_PrimitiveGroup
441 {
442  // Draw triangles
443  if (pCurrentGroup->containsTriangles())
444  {
445  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSize(), GL_UNSIGNED_INT, pCurrentGroup->trianglesIndexOffset());
446  }
447 
448  // Draw Triangles strip
449  if (pCurrentGroup->containsStrip())
450  {
451  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffset().size());
452  for (GLint i= 0; i < stripsCount; ++i)
453  {
454  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
455  }
456  }
457 
458  // Draw Triangles fan
459  if (pCurrentGroup->containsFan())
460  {
461  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffset().size());
462  for (GLint i= 0; i < fansCount; ++i)
463  {
464  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
465  }
466  }
467 }
468 // Use Vertex Array to Draw triangles from the specified GLC_PrimitiveGroup
470 {
471  // Draw triangles
472  if (pCurrentGroup->containsTriangles())
473  {
474  GLvoid* pOffset= &(m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesIndexOffseti()]);
475  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSize(), GL_UNSIGNED_INT, pOffset);
476  }
477 
478  // Draw Triangles strip
479  if (pCurrentGroup->containsStrip())
480  {
481  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffseti().size());
482  for (GLint i= 0; i < stripsCount; ++i)
483  {
484  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
485  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
486  }
487  }
488 
489  // Draw Triangles fan
490  if (pCurrentGroup->containsFan())
491  {
492  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffseti().size());
493  for (GLint i= 0; i < fansCount; ++i)
494  {
495  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
496  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
497  }
498  }
499 }
500 
501 // Use VBO to Draw primitives in selection mode from the specified GLC_PrimitiveGroup
503 {
504  GLubyte colorId[4];
505  // Draw triangles
506  if (pCurrentGroup->containsTrianglesGroupId())
507  {
508  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffset().size());
509  for (GLint i= 0; i < trianglesGroupCount; ++i)
510  {
511  glc::encodeRgbId(pCurrentGroup->triangleGroupId(i), colorId);
512  glColor3ubv(colorId);
513  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->trianglesGroupOffset().at(i));
514  }
515  }
516 
517  // Draw Triangles strip
518  if (pCurrentGroup->containsStripGroupId())
519  {
520  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffset().size());
521  for (GLint i= 0; i < stripsCount; ++i)
522  {
523  glc::encodeRgbId(pCurrentGroup->stripGroupId(i), colorId);
524  glColor3ubv(colorId);
525  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
526  }
527  }
528 
529  // Draw Triangles fan
530  if (pCurrentGroup->containsFanGroupId())
531  {
532  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffset().size());
533  for (GLint i= 0; i < fansCount; ++i)
534  {
535  glc::encodeRgbId(pCurrentGroup->fanGroupId(i), colorId);
536  glColor3ubv(colorId);
537 
538  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
539  }
540  }
541 
542 }
543 
544 // Use Vertex Array to Draw primitives in selection mode from the specified GLC_PrimitiveGroup
546 {
547  GLubyte colorId[4];
548  // Draw triangles
549  if (pCurrentGroup->containsTrianglesGroupId())
550  {
551  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffseti().size());
552  for (GLint i= 0; i < trianglesGroupCount; ++i)
553  {
554  glc::encodeRgbId(pCurrentGroup->triangleGroupId(i), colorId);
555  glColor3ubv(colorId);
556 
557  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesGroupOffseti().at(i)];
558  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pOffset);
559  }
560 
561  GLvoid* pOffset= &(m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesIndexOffseti()]);
562  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSize(), GL_UNSIGNED_INT, pOffset);
563  }
564 
565  // Draw Triangles strip
566  if (pCurrentGroup->containsStripGroupId())
567  {
568  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffseti().size());
569  for (GLint i= 0; i < stripsCount; ++i)
570  {
571  glc::encodeRgbId(pCurrentGroup->stripGroupId(i), colorId);
572  glColor3ubv(colorId);
573 
574  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
575  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
576  }
577  }
578 
579  // Draw Triangles fan
580  if (pCurrentGroup->containsFanGroupId())
581  {
582  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffseti().size());
583  for (GLint i= 0; i < fansCount; ++i)
584  {
585  glc::encodeRgbId(pCurrentGroup->fanGroupId(i), colorId);
586  glColor3ubv(colorId);
587 
588  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
589  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
590  }
591  }
592 }
593 
594 // Use VBO to Draw primitives with specific materials from the specified GLC_PrimitiveGroup
595 void GLC_Mesh::vboDrawPrimitivesGroupOf(GLC_PrimitiveGroup* pCurrentGroup, GLC_Material* pCurrentMaterial, bool materialIsRenderable
596  , bool isTransparent, QHash<GLC_uint, GLC_Material*>* pMaterialHash)
597 {
598  GLC_Material* pCurrentLocalMaterial= pCurrentMaterial;
599  // Draw triangles
600  if (pCurrentGroup->containsTriangles())
601  {
602  Q_ASSERT(pCurrentGroup->containsTrianglesGroupId());
603  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffset().size());
604  for (GLint i= 0; i < trianglesGroupCount; ++i)
605  {
606  GLC_uint currentPrimitiveId= pCurrentGroup->triangleGroupId(i);
607  if (pMaterialHash->contains(currentPrimitiveId))
608  {
609  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
610  {
611  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
612  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
613  }
614  }
615  else if (pCurrentLocalMaterial != pCurrentMaterial)
616  {
617  pCurrentLocalMaterial= pCurrentMaterial;
618  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
619  }
620  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
621  {
622  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->trianglesGroupOffset().at(i));
623  }
624  }
625  }
626 
627  // Draw Triangles strip
628  if (pCurrentGroup->containsStrip())
629  {
630  Q_ASSERT(pCurrentGroup->containsStripGroupId());
631  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffset().size());
632  for (GLint i= 0; i < stripsCount; ++i)
633  {
634  GLC_uint currentPrimitiveId= pCurrentGroup->stripGroupId(i);
635  if (pMaterialHash->contains(currentPrimitiveId))
636  {
637  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
638  {
639  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
640  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
641  }
642  }
643  else if (pCurrentLocalMaterial != pCurrentMaterial)
644  {
645  pCurrentLocalMaterial= pCurrentMaterial;
646  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
647  }
648  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
649  {
650  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
651  }
652  }
653  }
654 
655  // Draw Triangles fan
656  if (pCurrentGroup->containsFan())
657  {
658  Q_ASSERT(pCurrentGroup->containsFanGroupId());
659  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffset().size());
660  for (GLint i= 0; i < fansCount; ++i)
661  {
662  GLC_uint currentPrimitiveId= pCurrentGroup->fanGroupId(i);
663  if (pMaterialHash->contains(currentPrimitiveId))
664  {
665  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
666  {
667  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
668  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
669  }
670  }
671  else if (pCurrentLocalMaterial != pCurrentMaterial)
672  {
673  pCurrentLocalMaterial= pCurrentMaterial;
674  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
675  }
676  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
677  {
678  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
679  }
680  }
681  }
682 
683 }
684 
685 // Use Vertex Array to Draw primitives with specific materials from the specified GLC_PrimitiveGroup
686 void GLC_Mesh::vertexArrayDrawPrimitivesGroupOf(GLC_PrimitiveGroup* pCurrentGroup, GLC_Material* pCurrentMaterial, bool materialIsRenderable
687  , bool isTransparent, QHash<GLC_uint, GLC_Material*>* pMaterialHash)
688 {
689  GLC_Material* pCurrentLocalMaterial= pCurrentMaterial;
690  // Draw triangles
691  if (pCurrentGroup->containsTriangles())
692  {
693  Q_ASSERT(pCurrentGroup->containsTrianglesGroupId());
694  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffseti().size());
695  for (GLint i= 0; i < trianglesGroupCount; ++i)
696  {
697  GLC_uint currentPrimitiveId= pCurrentGroup->triangleGroupId(i);
698  if (pMaterialHash->contains(currentPrimitiveId))
699  {
700  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
701  {
702  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
703  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
704  }
705  }
706  else if (pCurrentLocalMaterial != pCurrentMaterial)
707  {
708  pCurrentLocalMaterial= pCurrentMaterial;
709  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
710  }
711  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
712  {
713  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesGroupOffseti().at(i)];
714  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pOffset);
715  }
716  }
717  }
718 
719  // Draw Triangles strip
720  if (pCurrentGroup->containsStrip())
721  {
722  Q_ASSERT(pCurrentGroup->containsStripGroupId());
723  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffseti().size());
724  for (GLint i= 0; i < stripsCount; ++i)
725  {
726  GLC_uint currentPrimitiveId= pCurrentGroup->stripGroupId(i);
727  if (pMaterialHash->contains(currentPrimitiveId))
728  {
729  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
730  {
731  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
732  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
733  }
734  }
735  else if (pCurrentLocalMaterial != pCurrentMaterial)
736  {
737  pCurrentLocalMaterial= pCurrentMaterial;
738  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
739  }
740  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
741  {
742  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
743  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
744  }
745  }
746  }
747 
748  // Draw Triangles fan
749  if (pCurrentGroup->containsFan())
750  {
751  Q_ASSERT(pCurrentGroup->containsFanGroupId());
752  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffseti().size());
753  for (GLint i= 0; i < fansCount; ++i)
754  {
755  GLC_uint currentPrimitiveId= pCurrentGroup->fanGroupId(i);
756  if (pMaterialHash->contains(currentPrimitiveId))
757  {
758  if (pCurrentLocalMaterial != pMaterialHash->value(currentPrimitiveId))
759  {
760  pCurrentLocalMaterial= pMaterialHash->value(currentPrimitiveId);
761  if (pCurrentLocalMaterial->isTransparent() == isTransparent) pCurrentLocalMaterial->glExecute();
762  }
763  }
764  else if (pCurrentLocalMaterial != pCurrentMaterial)
765  {
766  pCurrentLocalMaterial= pCurrentMaterial;
767  if (materialIsRenderable) pCurrentLocalMaterial->glExecute();
768  }
769  if (pCurrentLocalMaterial->isTransparent() == isTransparent)
770  {
771  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
772  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
773  }
774  }
775  }
776 
777 }
778 
779 // Use VBO to Draw primitives with specific materials from the specified GLC_PrimitiveGroup
780 void GLC_Mesh::vboDrawSelectedPrimitivesGroupOf(GLC_PrimitiveGroup* pCurrentGroup, GLC_Material* pCurrentMaterial, bool materialIsRenderable
781  , bool isTransparent, const GLC_RenderProperties& renderProperties)
782 {
783  QSet<GLC_uint>* pSelectedPrimitive= renderProperties.setOfSelectedPrimitivesId();
784  Q_ASSERT(NULL != pSelectedPrimitive);
785 
786  QHash<GLC_uint, GLC_Material*>* pMaterialHash= NULL;
787  if (!renderProperties.hashOfOverwritePrimitiveMaterialsIsEmpty())
788  {
789  pMaterialHash= renderProperties.hashOfOverwritePrimitiveMaterials();
790  }
791 
792  GLC_Material* pCurrentLocalMaterial= pCurrentMaterial;
793  // Draw triangles
794  if (pCurrentGroup->containsTriangles())
795  {
796  Q_ASSERT(pCurrentGroup->containsTrianglesGroupId());
797  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffset().size());
798  for (GLint i= 0; i < trianglesGroupCount; ++i)
799  {
800  GLC_uint currentPrimitiveId= pCurrentGroup->triangleGroupId(i);
801  if (pSelectedPrimitive->contains(currentPrimitiveId))
802  {
803  if (!isTransparent)
804  {
806  pCurrentLocalMaterial= NULL;
807  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->trianglesGroupOffset().at(i));
808  }
809  }
810  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
811  {
812  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
813  {
814  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
815  if (pMat != pCurrentLocalMaterial)
816  {
817  pCurrentLocalMaterial= pMat;
818  pCurrentLocalMaterial->glExecute();
819  }
820  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->trianglesGroupOffset().at(i));
821  }
822 
823  }
824  else if (materialIsRenderable)
825  {
826  if (pCurrentLocalMaterial != pCurrentMaterial)
827  {
828  pCurrentLocalMaterial= pCurrentMaterial;
829  pCurrentLocalMaterial->glExecute();
830  }
831  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->trianglesGroupOffset().at(i));
832  }
833  }
834  }
835 
836  // Draw Triangles strip
837  if (pCurrentGroup->containsStrip())
838  {
839  Q_ASSERT(pCurrentGroup->containsStripGroupId());
840  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffset().size());
841  for (GLint i= 0; i < stripsCount; ++i)
842  {
843  GLC_uint currentPrimitiveId= pCurrentGroup->stripGroupId(i);
844  if (pSelectedPrimitive->contains(currentPrimitiveId))
845  {
846  if (!isTransparent)
847  {
849  pCurrentLocalMaterial= NULL;
850  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
851  }
852  }
853  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
854  {
855  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
856  {
857  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
858  if (pMat != pCurrentLocalMaterial)
859  {
860  pCurrentLocalMaterial= pMat;
861  pCurrentLocalMaterial->glExecute();
862  }
863  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
864  }
865 
866  }
867  else if (materialIsRenderable)
868  {
869  if (pCurrentLocalMaterial != pCurrentMaterial)
870  {
871  pCurrentLocalMaterial= pCurrentMaterial;
872  pCurrentLocalMaterial->glExecute();
873  }
874  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->stripsOffset().at(i));
875  }
876  }
877  }
878 
879  // Draw Triangles fan
880  if (pCurrentGroup->containsFan())
881  {
882  Q_ASSERT(pCurrentGroup->containsFanGroupId());
883  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffset().size());
884  for (GLint i= 0; i < fansCount; ++i)
885  {
886  GLC_uint currentPrimitiveId= pCurrentGroup->fanGroupId(i);
887  if (pSelectedPrimitive->contains(currentPrimitiveId))
888  {
889  if (!isTransparent)
890  {
892  pCurrentLocalMaterial= NULL;
893  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
894  }
895  }
896  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
897  {
898  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
899  {
900  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
901  if (pMat != pCurrentLocalMaterial)
902  {
903  pCurrentLocalMaterial= pMat;
904  pCurrentLocalMaterial->glExecute();
905  }
906  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
907  }
908 
909  }
910  else if (materialIsRenderable)
911  {
912  if (pCurrentLocalMaterial != pCurrentMaterial)
913  {
914  pCurrentLocalMaterial= pCurrentMaterial;
915  pCurrentLocalMaterial->glExecute();
916  }
917  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pCurrentGroup->fansOffset().at(i));
918  }
919  }
920  }
921 
922 }
923 
924 // Use Vertex Array to Draw primitives with specific materials from the specified GLC_PrimitiveGroup
925 void GLC_Mesh::vertexArrayDrawSelectedPrimitivesGroupOf(GLC_PrimitiveGroup* pCurrentGroup, GLC_Material* pCurrentMaterial, bool materialIsRenderable
926  , bool isTransparent, const GLC_RenderProperties& renderProperties)
927 {
928  QSet<GLC_uint>* pSelectedPrimitive= renderProperties.setOfSelectedPrimitivesId();
929  Q_ASSERT(NULL != pSelectedPrimitive);
930 
931  QHash<GLC_uint, GLC_Material*>* pMaterialHash= NULL;
932  if (!renderProperties.hashOfOverwritePrimitiveMaterialsIsEmpty())
933  {
934  pMaterialHash= renderProperties.hashOfOverwritePrimitiveMaterials();
935  }
936 
937  GLC_Material* pCurrentLocalMaterial= pCurrentMaterial;
938  // Draw triangles
939  if (pCurrentGroup->containsTriangles())
940  {
941  Q_ASSERT(pCurrentGroup->containsTrianglesGroupId());
942  const GLsizei trianglesGroupCount= static_cast<GLsizei>(pCurrentGroup->trianglesGroupOffseti().size());
943  for (GLint i= 0; i < trianglesGroupCount; ++i)
944  {
945  GLC_uint currentPrimitiveId= pCurrentGroup->triangleGroupId(i);
946  if (pSelectedPrimitive->contains(currentPrimitiveId))
947  {
948  if (!isTransparent)
949  {
951  pCurrentLocalMaterial= NULL;
952  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesGroupOffseti().at(i)];
953  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pOffset);
954  }
955  }
956  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
957  {
958  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
959  {
960  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
961  if (pMat != pCurrentLocalMaterial)
962  {
963  pCurrentLocalMaterial= pMat;
964  pCurrentLocalMaterial->glExecute();
965  }
966  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesGroupOffseti().at(i)];
967  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pOffset);
968  }
969 
970  }
971  else if (materialIsRenderable)
972  {
973  if (pCurrentLocalMaterial != pCurrentMaterial)
974  {
975  pCurrentLocalMaterial= pCurrentMaterial;
976  pCurrentLocalMaterial->glExecute();
977  }
978  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->trianglesGroupOffseti().at(i)];
979  glDrawElements(GL_TRIANGLES, pCurrentGroup->trianglesIndexSizes().at(i), GL_UNSIGNED_INT, pOffset);
980  }
981  }
982  }
983 
984  // Draw Triangles strip
985  if (pCurrentGroup->containsStrip())
986  {
987  Q_ASSERT(pCurrentGroup->containsStripGroupId());
988  const GLsizei stripsCount= static_cast<GLsizei>(pCurrentGroup->stripsOffseti().size());
989  for (GLint i= 0; i < stripsCount; ++i)
990  {
991  GLC_uint currentPrimitiveId= pCurrentGroup->stripGroupId(i);
992  if (pSelectedPrimitive->contains(currentPrimitiveId))
993  {
994  if (!isTransparent)
995  {
997  pCurrentLocalMaterial= NULL;
998  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
999  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
1000  }
1001  }
1002  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
1003  {
1004  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
1005  {
1006  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
1007  if (pMat != pCurrentLocalMaterial)
1008  {
1009  pCurrentLocalMaterial= pMat;
1010  pCurrentLocalMaterial->glExecute();
1011  }
1012  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
1013  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
1014  }
1015 
1016  }
1017  else if (materialIsRenderable)
1018  {
1019  if (pCurrentLocalMaterial != pCurrentMaterial)
1020  {
1021  pCurrentLocalMaterial= pCurrentMaterial;
1022  pCurrentLocalMaterial->glExecute();
1023  }
1024  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->stripsOffseti().at(i)];
1025  glDrawElements(GL_TRIANGLE_STRIP, pCurrentGroup->stripsSizes().at(i), GL_UNSIGNED_INT, pOffset);
1026  }
1027  }
1028  }
1029 
1030  // Draw Triangles fan
1031  if (pCurrentGroup->containsFan())
1032  {
1033  Q_ASSERT(pCurrentGroup->containsFanGroupId());
1034  const GLsizei fansCount= static_cast<GLsizei>(pCurrentGroup->fansOffseti().size());
1035  for (GLint i= 0; i < fansCount; ++i)
1036  {
1037  GLC_uint currentPrimitiveId= pCurrentGroup->fanGroupId(i);
1038  if (pSelectedPrimitive->contains(currentPrimitiveId))
1039  {
1040  if (!isTransparent)
1041  {
1043  pCurrentLocalMaterial= NULL;
1044  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
1045  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
1046  }
1047  }
1048  else if ((NULL != pMaterialHash) && pMaterialHash->contains(currentPrimitiveId))
1049  {
1050  if (pMaterialHash->value(currentPrimitiveId)->isTransparent() == isTransparent)
1051  {
1052  GLC_Material* pMat= pMaterialHash->value(currentPrimitiveId);
1053  if (pMat != pCurrentLocalMaterial)
1054  {
1055  pCurrentLocalMaterial= pMat;
1056  pCurrentLocalMaterial->glExecute();
1057  }
1058  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
1059  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
1060  }
1061 
1062  }
1063  else if (materialIsRenderable)
1064  {
1065  if (pCurrentLocalMaterial != pCurrentMaterial)
1066  {
1067  pCurrentLocalMaterial= pCurrentMaterial;
1068  pCurrentLocalMaterial->glExecute();
1069  }
1070  GLvoid* pOffset= &m_MeshData.indexVectorHandle(m_CurrentLod)->data()[pCurrentGroup->fansOffseti().at(i)];
1071  glDrawElements(GL_TRIANGLE_FAN, pCurrentGroup->fansSizes().at(i), GL_UNSIGNED_INT, pOffset);
1072  }
1073  }
1074  }
1075 
1076 }
1077 
1078 // Activate mesh VBOs and IBO of the current LOD
1080 {
1081  // Activate Vertices VBO
1083  glVertexPointer(3, GL_FLOAT, 0, 0);
1084  glEnableClientState(GL_VERTEX_ARRAY);
1085 
1086  // Activate Normals VBO
1088  glNormalPointer(GL_FLOAT, 0, 0);
1089  glEnableClientState(GL_NORMAL_ARRAY);
1090 
1091  // Activate texel VBO if needed
1093  {
1094  glTexCoordPointer(2, GL_FLOAT, 0, 0);
1095  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1096  }
1097 
1098  // Activate Color VBO if needed
1100  {
1101  glEnable(GL_COLOR_MATERIAL);
1102  glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1103  glColorPointer(4, GL_FLOAT, 0, 0);
1104  glEnableClientState(GL_COLOR_ARRAY);
1105  }
1106 
1108 }
1109 
1110 // Activate vertex Array
1112 {
1113  // Use Vertex Array
1114  glVertexPointer(3, GL_FLOAT, 0, m_MeshData.positionVectorHandle()->data());
1115  glEnableClientState(GL_VERTEX_ARRAY);
1116 
1117  glNormalPointer(GL_FLOAT, 0, m_MeshData.normalVectorHandle()->data());
1118  glEnableClientState(GL_NORMAL_ARRAY);
1119 
1120  // Activate texel if needed
1121  if (!m_MeshData.texelVectorHandle()->isEmpty())
1122  {
1123  glTexCoordPointer(2, GL_FLOAT, 0, m_MeshData.texelVectorHandle()->data());
1124  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1125  }
1126 
1127  // Activate Color array if needed
1129  {
1130  glEnable(GL_COLOR_MATERIAL);
1131  glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1132  glColorPointer(4, GL_FLOAT, 0, m_MeshData.colorVectorHandle()->data());
1133  glEnableClientState(GL_COLOR_ARRAY);
1134  }
1135 }
1136 
1137 
1138 
1139 #endif /* GLC_MESH_H_ */

©2005-2013 Laurent Ribon