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 | size_t strip_index_count;
|
---|
76 | SubMesh *geosubmesh;
|
---|
77 | primitive_vector PrimitivesVector;
|
---|
78 | indices Indices;
|
---|
79 | float increment;
|
---|
80 | float update;
|
---|
81 |
|
---|
82 | // For each submesh.
|
---|
83 | for (int submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++)
|
---|
84 | {
|
---|
85 | // Gets the submesh.
|
---|
86 | geosubmesh = &mGeoMesh->mSubMesh[submesh];
|
---|
87 |
|
---|
88 | // Free index vector.
|
---|
89 | Indices.clear();
|
---|
90 |
|
---|
91 | // Progress bar increment.
|
---|
92 | increment = 20 / mGeoMesh->mSubMeshCount;
|
---|
93 | increment = increment / geosubmesh->mIndexCount;
|
---|
94 | update = 0.0;
|
---|
95 |
|
---|
96 | // For each index.
|
---|
97 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
98 | {
|
---|
99 | // Update progress bar.
|
---|
100 | if (mUPB)
|
---|
101 | {
|
---|
102 | update = update + increment;
|
---|
103 |
|
---|
104 | if (update > 0.9)
|
---|
105 | {
|
---|
106 | mUPB(1.0);
|
---|
107 |
|
---|
108 | update = 0;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Add an index.
|
---|
113 | Indices.push_back(geosubmesh->mIndex[index]);
|
---|
114 | }
|
---|
115 |
|
---|
116 | // we want to time the tri_stripper object destruction as well.
|
---|
117 | tri_stripper TriStripper(Indices);
|
---|
118 |
|
---|
119 | // Sets the progress bar function for stripper algorithm.
|
---|
120 | TriStripper.SetProgressFunc(mUPB,60/mGeoMesh->mSubMeshCount);
|
---|
121 |
|
---|
122 | TriStripper.SetMinStripSize(2);
|
---|
123 | TriStripper.SetCacheSize(32);
|
---|
124 | //TriStripper.SetBackwardSearch(false);
|
---|
125 |
|
---|
126 | TriStripper.Strip(&PrimitivesVector);
|
---|
127 |
|
---|
128 | // Free submesh indices.
|
---|
129 | delete [] geosubmesh->mIndex;
|
---|
130 |
|
---|
131 | // Initialize index count.
|
---|
132 | geosubmesh->mIndexCount = 0;
|
---|
133 |
|
---|
134 | // Initialize strip count.
|
---|
135 | strip_count = 0;
|
---|
136 |
|
---|
137 | // For each strip or triangle list.
|
---|
138 | for (size_t strip = 0; strip < PrimitivesVector.size(); ++strip)
|
---|
139 | {
|
---|
140 | if (PrimitivesVector[strip].Type == TRIANGLE_STRIP)
|
---|
141 | {
|
---|
142 | strip_count++;
|
---|
143 |
|
---|
144 | // Gets the index count.
|
---|
145 | strip_index_count = PrimitivesVector[strip].Indices.size();
|
---|
146 |
|
---|
147 | // Adds indices of the current strip to total index count.
|
---|
148 | geosubmesh->mIndexCount += strip_index_count;
|
---|
149 | }
|
---|
150 | else
|
---|
151 | {
|
---|
152 | strip_count += PrimitivesVector[strip].Indices.size() / 3;
|
---|
153 |
|
---|
154 | geosubmesh->mIndexCount += PrimitivesVector[strip].Indices.size();
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Reserve memory for strips list.
|
---|
160 | geosubmesh->mStrip = new Index*[strip_count];
|
---|
161 | geosubmesh->mStripCount = strip_count;
|
---|
162 |
|
---|
163 | // Reserve memory for indices.
|
---|
164 | geosubmesh->mIndex = new Index[geosubmesh->mIndexCount];
|
---|
165 |
|
---|
166 | // Initialize index count.
|
---|
167 | index_count = 0;
|
---|
168 |
|
---|
169 | // For each strip.
|
---|
170 | for (size_t strip = 0; strip < PrimitivesVector.size(); strip++)
|
---|
171 | {
|
---|
172 | if (PrimitivesVector[strip].Type == TRIANGLE_STRIP)
|
---|
173 | {
|
---|
174 | // Asigns the beginning of the strip.
|
---|
175 | geosubmesh->mStrip[strip] = &geosubmesh->mIndex[index_count];
|
---|
176 |
|
---|
177 | // Gets the index count of the current strip.
|
---|
178 | strip_index_count = PrimitivesVector[strip].Indices.size();
|
---|
179 |
|
---|
180 |
|
---|
181 | // Fill up the index array.
|
---|
182 | for ( size_t index = 0;
|
---|
183 | index < PrimitivesVector[strip].Indices.size();
|
---|
184 | index++)
|
---|
185 | {
|
---|
186 | geosubmesh->mIndex[index + index_count] = PrimitivesVector[strip].Indices[index];
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Adds the current strip size to index count.
|
---|
190 | index_count += PrimitivesVector[strip].Indices.size();
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | // For each index.
|
---|
195 | for ( size_t itri = 0;
|
---|
196 | itri < PrimitivesVector[strip].Indices.size() / 3;
|
---|
197 | itri ++)
|
---|
198 | {
|
---|
199 | // Asigns the beginning of the strip.
|
---|
200 | geosubmesh->mStrip[strip + itri] = &geosubmesh->mIndex[index_count];
|
---|
201 |
|
---|
202 | // Triangle indeces.
|
---|
203 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+0];
|
---|
204 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+1];
|
---|
205 | geosubmesh->mIndex[index_count++] = PrimitivesVector[strip].Indices[itri*3+2];
|
---|
206 |
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | // Sets the actual submesh type to triangle strips.
|
---|
212 | geosubmesh->mType = GEO_TRIANGLE_STRIPS;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // Update progress bar function.
|
---|
216 | if (mUPB)
|
---|
217 | {
|
---|
218 | mUPB(100);
|
---|
219 | }
|
---|
220 |
|
---|
221 | // All wright.
|
---|
222 | return 1;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /// GetMesh: Return de current Mesh.
|
---|
226 | Mesh* CustomStripifier::GetMesh()
|
---|
227 | {
|
---|
228 | Mesh *mesh_stripified;
|
---|
229 |
|
---|
230 | mesh_stripified = new Mesh();
|
---|
231 | *mesh_stripified = *mGeoMesh;
|
---|
232 |
|
---|
233 | return mesh_stripified;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // Sets what is the submesh that stores the leaves.
|
---|
237 | void CustomStripifier::SetSubMeshLeaves(size_t submesh)
|
---|
238 | {
|
---|
239 | mSubMeshLeaves = submesh;
|
---|
240 | }
|
---|
241 |
|
---|