source: GTP/trunk/Lib/Geom/GTGeometry/src/GeoMeshStripifier.cpp @ 774

Revision 774, 6.3 KB checked in by gumbau, 18 years ago (diff)

GTGeometry and GeoTool? initial imports

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