1 | /* ==========================================================================
|
---|
2 | * (C) 2005 Universitat Jaume I
|
---|
3 | * ==========================================================================
|
---|
4 | * PROYECT: GAME TOOLS
|
---|
5 | * ==========================================================================*/
|
---|
6 | /** CONTENT: Make triangle strip meshes from triangle list meshes.
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * @file GeoMeshStripifier.cpp
|
---|
10 | *===========================================================================*/
|
---|
11 |
|
---|
12 | #include "GeoMeshStripifier.h"
|
---|
13 | #include "tri_stripper.h"
|
---|
14 |
|
---|
15 | using namespace Geometry;
|
---|
16 | using namespace std;
|
---|
17 | using namespace triangle_stripper;
|
---|
18 |
|
---|
19 | //-----------------------------------------------------------------------------
|
---|
20 | // Constructors.
|
---|
21 | //-----------------------------------------------------------------------------
|
---|
22 |
|
---|
23 | MeshStripifier::MeshStripifier()
|
---|
24 | {
|
---|
25 | }
|
---|
26 |
|
---|
27 | MeshStripifier::MeshStripifier( const Geometry::Mesh *geoMesh)
|
---|
28 | {
|
---|
29 | }
|
---|
30 |
|
---|
31 | CustomStripifier::CustomStripifier()
|
---|
32 | {
|
---|
33 | }
|
---|
34 |
|
---|
35 | CustomStripifier::CustomStripifier( const Geometry::Mesh *geoMesh)
|
---|
36 | {
|
---|
37 | mGeoMesh = new Mesh();
|
---|
38 | *mGeoMesh = *geoMesh;
|
---|
39 |
|
---|
40 | // Sets the actual progress bar function.
|
---|
41 | mUPB = NULL;
|
---|
42 |
|
---|
43 | // Initialize the leaves submesh index.
|
---|
44 | mSubMeshLeaves = -1;
|
---|
45 | }
|
---|
46 |
|
---|
47 | // Set the progress bar function.
|
---|
48 | void CustomStripifier::SetProgressFunc(TIPOFUNC upb)
|
---|
49 | {
|
---|
50 | // Sets the actual progress bar function.
|
---|
51 | mUPB = upb;
|
---|
52 | }
|
---|
53 |
|
---|
54 | //-----------------------------------------------------------------------------
|
---|
55 | // Destroyers.
|
---|
56 | //-----------------------------------------------------------------------------
|
---|
57 | CustomStripifier::~CustomStripifier()
|
---|
58 | {
|
---|
59 | delete mGeoMesh;
|
---|
60 | }
|
---|
61 |
|
---|
62 | MeshStripifier::~MeshStripifier()
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | //-----------------------------------------------------------------------------
|
---|
67 | // Public.
|
---|
68 | //-----------------------------------------------------------------------------
|
---|
69 |
|
---|
70 | /// Stripify.
|
---|
71 | int CustomStripifier::Stripify()
|
---|
72 | {
|
---|
73 | size_t index_count;
|
---|
74 | size_t strip_count;
|
---|
75 | SubMesh *geosubmesh;
|
---|
76 | primitive_vector PrimitivesVector;
|
---|
77 | indices Indices;
|
---|
78 | float increment;
|
---|
79 | float update;
|
---|
80 |
|
---|
81 | // For each submesh.
|
---|
82 | for (int submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++)
|
---|
83 | {
|
---|
84 | // If is not the Leaveas submesh.
|
---|
85 | if (submesh != mSubMeshLeaves)
|
---|
86 | {
|
---|
87 |
|
---|
88 | // Gets the submesh.
|
---|
89 | geosubmesh = &mGeoMesh->mSubMesh[submesh];
|
---|
90 |
|
---|
91 | // Free index vector.
|
---|
92 | Indices.clear();
|
---|
93 |
|
---|
94 | // Progress bar increment.
|
---|
95 | increment = 20 / mGeoMesh->mSubMeshCount;
|
---|
96 | increment = increment / geosubmesh->mIndexCount;
|
---|
97 | update = 0.0;
|
---|
98 |
|
---|
99 | // For each index.
|
---|
100 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
101 | {
|
---|
102 | // Update progress bar.
|
---|
103 | if (mUPB)
|
---|
104 | {
|
---|
105 | update = update + increment;
|
---|
106 |
|
---|
107 | if (update > 0.9)
|
---|
108 | {
|
---|
109 | mUPB(1.0);
|
---|
110 |
|
---|
111 | update = 0;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | // Add an index.
|
---|
116 | Indices.push_back(geosubmesh->mIndex[index]);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // we want to time the tri_stripper object destruction as well.
|
---|
120 | tri_stripper TriStripper(Indices);
|
---|
121 |
|
---|
122 | // Sets the progress bar function for stripper algorithm.
|
---|
123 | TriStripper.SetProgressFunc(mUPB,60/mGeoMesh->mSubMeshCount);
|
---|
124 |
|
---|
125 | TriStripper.SetMinStripSize(2);
|
---|
126 | TriStripper.SetCacheSize(32);
|
---|
127 | //TriStripper.SetBackwardSearch(false);
|
---|
128 |
|
---|
129 | TriStripper.Strip(&PrimitivesVector);
|
---|
130 |
|
---|
131 | // Free submesh indices.
|
---|
132 | delete [] geosubmesh->mIndex;
|
---|
133 |
|
---|
134 | // Initialize index count.
|
---|
135 | geosubmesh->mIndexCount = 0;
|
---|
136 |
|
---|
137 | // Initialize strip count.
|
---|
138 | strip_count = 0;
|
---|
139 |
|
---|
140 | // For each strip or triangle list.
|
---|
141 | for (size_t i = 0; i < PrimitivesVector.size(); ++i)
|
---|
142 | {
|
---|
143 | if (PrimitivesVector[i].Type == TRIANGLE_STRIP)
|
---|
144 | {
|
---|
145 | strip_count++;
|
---|
146 | geosubmesh->mIndexCount += PrimitivesVector[i].Indices.size();
|
---|
147 |
|
---|
148 | // Degenerate triangles.
|
---|
149 | geosubmesh->mIndexCount += 2;
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | strip_count += PrimitivesVector[i].Indices.size()/3;
|
---|
154 |
|
---|
155 | geosubmesh->mIndexCount += PrimitivesVector[i].Indices.size();
|
---|
156 |
|
---|
157 | // Degenerate triangles.
|
---|
158 | geosubmesh->mIndexCount += 2*(PrimitivesVector[i].Indices.size()/3);
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Reserve memory for strips list.
|
---|
164 | geosubmesh->mStrip = new Index*[strip_count];
|
---|
165 | geosubmesh->mStripCount = strip_count;
|
---|
166 |
|
---|
167 | // Adjust the index count.
|
---|
168 | geosubmesh->mIndexCount -= 2;
|
---|
169 |
|
---|
170 | // Reserve memory for indices.
|
---|
171 | geosubmesh->mIndex = new Index[geosubmesh->mIndexCount];
|
---|
172 |
|
---|
173 | // Initialize index count.
|
---|
174 | index_count = 0;
|
---|
175 |
|
---|
176 | // For each strip.
|
---|
177 | for (size_t strip = 0; strip < PrimitivesVector.size(); strip++)
|
---|
178 | {
|
---|
179 | if (PrimitivesVector[strip].Type == TRIANGLE_STRIP)
|
---|
180 | {
|
---|
181 | // Asigns the beginning of the strip.
|
---|
182 | geosubmesh->mStrip[strip] = &geosubmesh->mIndex[index_count];
|
---|
183 |
|
---|
184 | // If is not the first strip.
|
---|
185 | if (strip != 0)
|
---|
186 | {
|
---|
187 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[0];
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Fill up the index array.
|
---|
191 | for ( size_t index = 0;
|
---|
192 | index < PrimitivesVector[strip].Indices.size();
|
---|
193 | index++)
|
---|
194 | {
|
---|
195 | geosubmesh->mIndex[index + index_count] = PrimitivesVector[strip].Indices[index];
|
---|
196 | }
|
---|
197 |
|
---|
198 | // Adds the current strip size to index count.
|
---|
199 | index_count += PrimitivesVector[strip].Indices.size();
|
---|
200 |
|
---|
201 | if ((strip + 1) != strip_count)
|
---|
202 | {
|
---|
203 | // Degenerate triangle.
|
---|
204 | geosubmesh->mIndex[index_count++] = geosubmesh->mIndex[index_count - 1];
|
---|
205 | }
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | // For each index.
|
---|
210 | for ( size_t itri = 0;
|
---|
211 | itri < PrimitivesVector[strip].Indices.size() / 3;
|
---|
212 | itri ++)
|
---|
213 | {
|
---|
214 | // Asigns the beginning of the strip.
|
---|
215 | geosubmesh->mStrip[strip + itri] = &geosubmesh->mIndex[index_count];
|
---|
216 |
|
---|
217 | // Degenerate triangle.
|
---|
218 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+0];
|
---|
219 |
|
---|
220 | // Triangle indeces.
|
---|
221 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+0];
|
---|
222 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+1];
|
---|
223 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+2];
|
---|
224 |
|
---|
225 | // If is not the last strip.
|
---|
226 | if ((strip + itri + 1) != strip_count)
|
---|
227 | {
|
---|
228 | geosubmesh->mIndex[index_count++] = geosubmesh->mIndex[index_count - 1];
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Sets the actual submesh type to triangle strips.
|
---|
236 | geosubmesh->mType = GEO_TRIANGLE_STRIPS;
|
---|
237 |
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | // Update progress bar function.
|
---|
242 | if (mUPB)
|
---|
243 | {
|
---|
244 | mUPB(100);
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | // All wright.
|
---|
249 | return 1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /// GetMesh: Return de current Mesh.
|
---|
253 | Mesh* CustomStripifier::GetMesh()
|
---|
254 | {
|
---|
255 | Mesh *mesh_stripified;
|
---|
256 |
|
---|
257 | mesh_stripified = new Mesh();
|
---|
258 | *mesh_stripified = *mGeoMesh;
|
---|
259 |
|
---|
260 | return mesh_stripified;
|
---|
261 | }
|
---|
262 |
|
---|
263 | // Sets what is the submesh that stores the leaves.
|
---|
264 | void CustomStripifier::SetSubMeshLeaves(size_t submesh)
|
---|
265 | {
|
---|
266 | mSubMeshLeaves = submesh;
|
---|
267 | }
|
---|
268 |
|
---|