OGRE  1.9
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreProgressiveMeshGenerator.h
Go to the documentation of this file.
1 /*
2  * -----------------------------------------------------------------------------
3  * This source file is part of OGRE
4  * (Object-oriented Graphics Rendering Engine)
5  * For the latest info, see http://www.ogre3d.org/
6  *
7  * Copyright (c) 2000-2014 Torus Knot Software Ltd
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  * -----------------------------------------------------------------------------
27  */
28 
29 #ifndef __ProgressiveMeshGenerator_H_
30 #define __ProgressiveMeshGenerator_H_
31 
32 #include "OgrePrerequisites.h"
33 #include "OgreVector3.h"
34 #include "OgreSmallVector.h"
35 #include "OgreMesh.h"
36 #include "OgreLodConfig.h"
37 #include "OgreLogManager.h"
38 
39 namespace Ogre
40 {
41 
43 {
44 public:
50  virtual void generateLodLevels(LodConfig& lodConfig) = 0;
51 
57  virtual void generateAutoconfiguredLodLevels(MeshPtr& mesh);
58 
65  virtual void getAutoconfig(MeshPtr& inMesh, LodConfig& outLodConfig);
66 
68 };
69 
75 {
76 public:
77 
79  virtual ~ProgressiveMeshGenerator();
80 
82  void generateLodLevels(LodConfig& lodConfig);
83 
84 protected:
85 
86  // VectorSet is basically a helper to use a vector as a small set container.
87  // Also these functions keep the code clean and fast.
88  // You can insert in O(1) time, if you know that it doesn't exists.
89  // You can remove in O(1) time, if you know the position of the item.
90  template<typename T, unsigned S>
92  public SmallVector<T, S> {
94 
95  void addNotExists(const T& item); // Complexity: O(1)!!
96  void remove(iterator it); // Complexity: O(1)!!
97  iterator add(const T& item); // Complexity: O(N)
98  void removeExists(const T& item); // Complexity: O(N)
99  bool remove(const T& item); // Complexity: O(N)
100  void replaceExists(const T& oldItem, const T& newItem); // Complexity: O(N)
101  bool has(const T& item); // Complexity: O(N)
102  iterator find(const T& item); // Complexity: O(N)
103  iterator findExists(const T& item); // Complexity: O(N)
104  };
105 
106  struct PMEdge;
107  struct PMVertex;
108  struct PMTriangle;
109  struct PMVertexHash;
110  struct PMVertexEqual;
111  struct PMCollapseCostLess;
112  struct PMCollapsedEdge;
114 
117  typedef HashSet<PMVertex*, PMVertexHash, PMVertexEqual> UniqueVertexSet;
120 
123 
126 
127  // Hash function for UniqueVertexSet.
130 
131  PMVertexHash() { assert(0); }
133  size_t operator() (const PMVertex* v) const;
134  };
135 
136  // Equality function for UniqueVertexSet.
138  bool operator() (const PMVertex* lhs, const PMVertex* rhs) const;
139  };
140 
141  // Directed edge
145  int refCount;
146 
147  explicit PMEdge(PMVertex* destination);
148  bool operator== (const PMEdge& other) const;
149  PMEdge& operator= (const PMEdge& b);
150  PMEdge(const PMEdge& b);
151  bool operator< (const PMEdge& other) const;
152  };
153 
158 
160  bool seam;
161  CollapseCostHeap::iterator costHeapPosition;
162  };
163 
165  PMVertex* vertex[3];
167  bool isRemoved;
168  unsigned short submeshID;
169  unsigned int vertexID[3];
170 
171  void computeNormal();
172  bool hasVertex(const PMVertex* v) const;
173  unsigned int getVertexID(const PMVertex* v) const;
174  bool isMalformed();
175  };
176 
178  size_t indexSize;
179  size_t indexCount;
180  };
181 
183  unsigned short* pshort;
184  unsigned int* pint;
185  };
186 
188  unsigned int srcID;
189  unsigned int dstID;
190  unsigned short submeshID;
191  };
192 
199  CollapsedEdges tmpCollapsedEdges; // Tmp container used in collapse().
201 
203 
204 #ifndef NDEBUG
205 
211 #endif
214 
215  size_t calcLodVertexCount(const LodLevel& lodConfig);
216  void tuneContainerSize();
217  void addVertexData(VertexData* vertexData, bool useSharedVertexLookup);
218  void addIndexData(IndexData* indexData, bool useSharedVertexLookup, unsigned short submeshID);
219  template<typename IndexType>
220  void addIndexDataImpl(IndexType* iPos, const IndexType* iEnd,
221  VertexLookupList& lookup,
222  unsigned short submeshID)
223  {
224 
225  // Loop through all triangles and connect them to the vertices.
226  for (; iPos < iEnd; iPos += 3) {
227  // It should never reallocate or every pointer will be invalid.
228  OgreAssert(mTriangleList.capacity() > mTriangleList.size(), "");
229  mTriangleList.push_back(PMTriangle());
230  PMTriangle* tri = &mTriangleList.back();
231  tri->isRemoved = false;
232  tri->submeshID = submeshID;
233  for (int i = 0; i < 3; i++) {
234  // Invalid index: Index is bigger then vertex buffer size.
235  OgreAssert(iPos[i] < lookup.size(), "");
236  tri->vertexID[i] = iPos[i];
237  tri->vertex[i] = lookup[iPos[i]];
238  }
239  if (tri->isMalformed()) {
240 #if OGRE_DEBUG_MODE
241  stringstream str;
242  str << "In " << mMeshName << " malformed triangle found with ID: " << getTriangleID(tri) << ". " <<
243  std::endl;
244  printTriangle(tri, str);
245  str << "It will be excluded from LOD level calculations.";
246  LogManager::getSingleton().stream() << str.str();
247 #endif
248  tri->isRemoved = true;
249  mIndexBufferInfoList[tri->submeshID].indexCount -= 3;
250  continue;
251  }
252  tri->computeNormal();
253  addTriangleToEdges(tri);
254  }
255  }
256 
257  void computeCosts();
258  bool isBorderVertex(const PMVertex* vertex) const;
259  PMEdge* getPointer(VEdges::iterator it);
260  void computeVertexCollapseCost(PMVertex* vertex);
261  Real computeEdgeCollapseCost(PMVertex* src, PMEdge* dstEdge);
262  virtual void bakeLods();
263  void collapse(PMVertex* vertex);
264  void initialize();
265  void computeLods(LodConfig& lodConfigs);
266  void updateVertexCollapseCost(PMVertex* src);
267 
268  bool hasSrcID(unsigned int srcID, unsigned short submeshID);
269  size_t findDstID(unsigned int srcID, unsigned short submeshID);
270  void replaceVertexID(PMTriangle* triangle, unsigned int oldID, unsigned int newID, PMVertex* dst);
271 
272 #ifndef NDEBUG
273  void assertValidVertex(PMVertex* v);
274  void assertValidMesh();
275  void assertOutdatedCollapseCost(PMVertex* vertex);
276 #endif // ifndef NDEBUG
277 
278  void addTriangleToEdges(PMTriangle* triangle);
279  void removeTriangleFromEdges(PMTriangle* triangle, PMVertex* skip = NULL);
280  void addEdge(PMVertex* v, const PMEdge& edge);
281  void removeEdge(PMVertex* v, const PMEdge& edge);
282  void printTriangle(PMTriangle* triangle, stringstream& str);
283  PMTriangle* findSideTriangle(const PMVertex* v1, const PMVertex* v2);
284  bool isDuplicateTriangle(PMTriangle* triangle, PMTriangle* triangle2);
285  PMTriangle* isDuplicateTriangle(PMTriangle* triangle);
286  int getTriangleID(PMTriangle* triangle);
287  void cleanupMemory();
288 };
289 
290 }
291 #endif
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:260
vector< PMCollapsedEdge >::type CollapsedEdges
void computeNormal()
Vertex ID in the buffer associated with the submeshID.
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
#define OgreAssert(a, b)
Definition: OgreException.h:61
PMVertex * collapseTo
Triangle ID set, which are using this vertex.
Structure for automatic LOD configuration.
Definition: OgreLodConfig.h:41
VectorSet< PMTriangle *, 7 > VTriangles
vector< PMVertex * >::type VertexLookupList
multimap< Real, PMVertex * >::type CollapseCostHeap
Improved version of ProgressiveMesh.
SmallVector - This is a 'vector' (really, a variable-sized array), optimized for the case when the ar...
HashSet< PMVertex *, PMVertexHash, PMVertexEqual > UniqueVertexSet
Log::Stream stream(LogMessageLevel lml=LML_NORMAL, bool maskDebug=false)
Get a stream on the default log.
#define _OgrePrivate
Definition: OgrePlatform.h:261
String mMeshName
The name of the mesh being processed.
void addIndexDataImpl(IndexType *iPos, const IndexType *iEnd, VertexLookupList &lookup, unsigned short submeshID)
Summary class collecting together index data source information.
StringStream stringstream
unsigned int vertexID[3]
ID of the submesh. Usable with mMesh.getSubMesh() function.
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Summary class collecting together vertex source information.
vector< PMIndexBufferInfo >::type IndexBufferInfoList
_StringBase String
static LogManager & getSingleton(void)
Override standard Singleton retrieval.
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)