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-2013 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 | |
---|
38 | namespace Ogre |
---|
39 | { |
---|
40 | |
---|
41 | class _OgreExport ProgressiveMeshGeneratorBase |
---|
42 | { |
---|
43 | public: |
---|
44 | /** |
---|
45 | * @brief Generates the LOD levels for a mesh. |
---|
46 | * |
---|
47 | * @param lodConfig Specification of the requested LOD levels. |
---|
48 | */ |
---|
49 | virtual void generateLodLevels(LodConfig& lodConfig) = 0; |
---|
50 | |
---|
51 | /** |
---|
52 | * @brief Generates the LOD levels for a mesh without configuring it. |
---|
53 | * |
---|
54 | * @param mesh Generate the LOD for this mesh. |
---|
55 | */ |
---|
56 | virtual void generateAutoconfiguredLodLevels(MeshPtr& mesh); |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief Fills LOD Config with a config, which works on any mesh. |
---|
60 | * |
---|
61 | * @param inMesh Optimize for this mesh. |
---|
62 | * @param outLodConfig LOD configuration storing the output. |
---|
63 | */ |
---|
64 | virtual void getAutoconfig(MeshPtr& inMesh, LodConfig& outLodConfig); |
---|
65 | |
---|
66 | virtual ~ProgressiveMeshGeneratorBase() { } |
---|
67 | }; |
---|
68 | |
---|
69 | /** |
---|
70 | * @brief Improved version of ProgressiveMesh. |
---|
71 | */ |
---|
72 | class _OgreExport ProgressiveMeshGenerator : |
---|
73 | public ProgressiveMeshGeneratorBase |
---|
74 | { |
---|
75 | public: |
---|
76 | |
---|
77 | ProgressiveMeshGenerator(); |
---|
78 | virtual ~ProgressiveMeshGenerator(); |
---|
79 | |
---|
80 | /// @copydoc ProgressiveMeshGeneratorBase::generateLodLevels |
---|
81 | void generateLodLevels(LodConfig& lodConfig); |
---|
82 | |
---|
83 | protected: |
---|
84 | |
---|
85 | // VectorSet is basically a helper to use a vector as a small set container. |
---|
86 | // Also these functions keep the code clean and fast. |
---|
87 | // You can insert in O(1) time, if you know that it doesn't exists. |
---|
88 | // You can remove in O(1) time, if you know the position of the item. |
---|
89 | template<typename T, unsigned S> |
---|
90 | struct _OgrePrivate VectorSet : |
---|
91 | public SmallVector<T, S> { |
---|
92 | typedef typename SmallVector<T, S>::iterator iterator; |
---|
93 | |
---|
94 | void addNotExists(const T& item); // Complexity: O(1)!! |
---|
95 | void remove(iterator it); // Complexity: O(1)!! |
---|
96 | iterator add(const T& item); // Complexity: O(N) |
---|
97 | void removeExists(const T& item); // Complexity: O(N) |
---|
98 | bool remove(const T& item); // Complexity: O(N) |
---|
99 | void replaceExists(const T& oldItem, const T& newItem); // Complexity: O(N) |
---|
100 | bool has(const T& item); // Complexity: O(N) |
---|
101 | iterator find(const T& item); // Complexity: O(N) |
---|
102 | iterator findExists(const T& item); // Complexity: O(N) |
---|
103 | }; |
---|
104 | |
---|
105 | struct PMEdge; |
---|
106 | struct PMVertex; |
---|
107 | struct PMTriangle; |
---|
108 | struct PMVertexHash; |
---|
109 | struct PMVertexEqual; |
---|
110 | struct PMCollapseCostLess; |
---|
111 | struct PMCollapsedEdge; |
---|
112 | struct PMIndexBufferInfo; |
---|
113 | |
---|
114 | typedef vector<PMVertex>::type VertexList; |
---|
115 | typedef vector<PMTriangle>::type TriangleList; |
---|
116 | typedef HashSet<PMVertex*, PMVertexHash, PMVertexEqual> UniqueVertexSet; |
---|
117 | typedef multimap<Real, PMVertex*>::type CollapseCostHeap; |
---|
118 | typedef vector<PMVertex*>::type VertexLookupList; |
---|
119 | |
---|
120 | typedef VectorSet<PMEdge, 8> VEdges; |
---|
121 | typedef VectorSet<PMTriangle*, 7> VTriangles; |
---|
122 | |
---|
123 | typedef vector<PMCollapsedEdge>::type CollapsedEdges; |
---|
124 | typedef vector<PMIndexBufferInfo>::type IndexBufferInfoList; |
---|
125 | |
---|
126 | // Hash function for UniqueVertexSet. |
---|
127 | struct _OgrePrivate PMVertexHash { |
---|
128 | ProgressiveMeshGenerator* mGen; |
---|
129 | |
---|
130 | PMVertexHash() { assert(0); } |
---|
131 | PMVertexHash(ProgressiveMeshGenerator* gen) { mGen = gen; } |
---|
132 | size_t operator() (const PMVertex* v) const; |
---|
133 | }; |
---|
134 | |
---|
135 | // Equality function for UniqueVertexSet. |
---|
136 | struct _OgrePrivate PMVertexEqual { |
---|
137 | bool operator() (const PMVertex* lhs, const PMVertex* rhs) const; |
---|
138 | }; |
---|
139 | |
---|
140 | // Directed edge |
---|
141 | struct _OgrePrivate PMEdge { |
---|
142 | PMVertex* dst; |
---|
143 | Real collapseCost; |
---|
144 | int refCount; |
---|
145 | |
---|
146 | explicit PMEdge(PMVertex* destination); |
---|
147 | bool operator== (const PMEdge& other) const; |
---|
148 | PMEdge& operator= (const PMEdge& b); |
---|
149 | PMEdge(const PMEdge& b); |
---|
150 | bool operator< (const PMEdge& other) const; |
---|
151 | }; |
---|
152 | |
---|
153 | struct _OgrePrivate PMVertex { |
---|
154 | Vector3 position; |
---|
155 | VEdges edges; |
---|
156 | VTriangles triangles; /// Triangle ID set, which are using this vertex. |
---|
157 | |
---|
158 | PMVertex* collapseTo; |
---|
159 | bool seam; |
---|
160 | CollapseCostHeap::iterator costHeapPosition; /// Iterator pointing to the position in the mCollapseCostSet, which allows fast remove. |
---|
161 | }; |
---|
162 | |
---|
163 | struct _OgrePrivate PMTriangle { |
---|
164 | PMVertex* vertex[3]; |
---|
165 | Vector3 normal; |
---|
166 | bool isRemoved; |
---|
167 | unsigned short submeshID; /// ID of the submesh. Usable with mMesh.getSubMesh() function. |
---|
168 | unsigned int vertexID[3]; /// Vertex ID in the buffer associated with the submeshID. |
---|
169 | |
---|
170 | void computeNormal(); |
---|
171 | bool hasVertex(const PMVertex* v) const; |
---|
172 | unsigned int getVertexID(const PMVertex* v) const; |
---|
173 | bool isMalformed(); |
---|
174 | }; |
---|
175 | |
---|
176 | struct _OgrePrivate PMIndexBufferInfo { |
---|
177 | size_t indexSize; |
---|
178 | size_t indexCount; |
---|
179 | }; |
---|
180 | |
---|
181 | union _OgrePrivate IndexBufferPointer { |
---|
182 | unsigned short* pshort; |
---|
183 | unsigned int* pint; |
---|
184 | }; |
---|
185 | |
---|
186 | struct _OgrePrivate PMCollapsedEdge { |
---|
187 | unsigned int srcID; |
---|
188 | unsigned int dstID; |
---|
189 | unsigned short submeshID; |
---|
190 | }; |
---|
191 | |
---|
192 | VertexLookupList mSharedVertexLookup; |
---|
193 | VertexLookupList mVertexLookup; |
---|
194 | VertexList mVertexList; |
---|
195 | TriangleList mTriangleList; |
---|
196 | UniqueVertexSet mUniqueVertexSet; |
---|
197 | CollapseCostHeap mCollapseCostHeap; |
---|
198 | CollapsedEdges tmpCollapsedEdges; // Tmp container used in collapse(). |
---|
199 | IndexBufferInfoList mIndexBufferInfoList; |
---|
200 | |
---|
201 | MeshPtr mMesh; |
---|
202 | |
---|
203 | #ifndef NDEBUG |
---|
204 | /** |
---|
205 | * @brief The name of the mesh being processed. |
---|
206 | * |
---|
207 | * This is separate from mMesh in order to allow for access from background threads. |
---|
208 | */ |
---|
209 | String mMeshName; |
---|
210 | #endif |
---|
211 | Real mMeshBoundingSphereRadius; |
---|
212 | Real mCollapseCostLimit; |
---|
213 | |
---|
214 | size_t calcLodVertexCount(const LodLevel& lodConfig); |
---|
215 | void tuneContainerSize(); |
---|
216 | void addVertexData(VertexData* vertexData, bool useSharedVertexLookup); |
---|
217 | template<typename IndexType> |
---|
218 | void addIndexDataImpl(IndexType* iPos, const IndexType* iEnd, VertexLookupList& lookup, unsigned short submeshID); |
---|
219 | void addIndexData(IndexData* indexData, bool useSharedVertexLookup, unsigned short submeshID); |
---|
220 | |
---|
221 | void computeCosts(); |
---|
222 | bool isBorderVertex(const PMVertex* vertex) const; |
---|
223 | PMEdge* getPointer(VEdges::iterator it); |
---|
224 | void computeVertexCollapseCost(PMVertex* vertex); |
---|
225 | Real computeEdgeCollapseCost(PMVertex* src, PMEdge* dstEdge); |
---|
226 | virtual void bakeLods(); |
---|
227 | void collapse(PMVertex* vertex); |
---|
228 | void initialize(); |
---|
229 | void computeLods(LodConfig& lodConfigs); |
---|
230 | void updateVertexCollapseCost(PMVertex* src); |
---|
231 | |
---|
232 | bool hasSrcID(unsigned int srcID, unsigned short submeshID); |
---|
233 | size_t findDstID(unsigned int srcID, unsigned short submeshID); |
---|
234 | void replaceVertexID(PMTriangle* triangle, unsigned int oldID, unsigned int newID, PMVertex* dst); |
---|
235 | |
---|
236 | #ifndef NDEBUG |
---|
237 | void assertValidVertex(PMVertex* v); |
---|
238 | void assertValidMesh(); |
---|
239 | void assertOutdatedCollapseCost(PMVertex* vertex); |
---|
240 | #endif // ifndef NDEBUG |
---|
241 | |
---|
242 | void addTriangleToEdges(PMTriangle* triangle); |
---|
243 | void removeTriangleFromEdges(PMTriangle* triangle, PMVertex* skip = NULL); |
---|
244 | void addEdge(PMVertex* v, const PMEdge& edge); |
---|
245 | void removeEdge(PMVertex* v, const PMEdge& edge); |
---|
246 | void printTriangle(PMTriangle* triangle, stringstream& str); |
---|
247 | PMTriangle* findSideTriangle(const PMVertex* v1, const PMVertex* v2); |
---|
248 | bool isDuplicateTriangle(PMTriangle* triangle, PMTriangle* triangle2); |
---|
249 | PMTriangle* isDuplicateTriangle(PMTriangle* triangle); |
---|
250 | int getTriangleID(PMTriangle* triangle); |
---|
251 | void cleanupMemory(); |
---|
252 | }; |
---|
253 | |
---|
254 | } |
---|
255 | #endif |
---|