source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodStripsLibrary.cpp @ 1322

Revision 1322, 17.0 KB checked in by gumbau, 18 years ago (diff)

Removed online degenerated vertices

Line 
1/*==========================================================================
2 *      (C) 2005 Universitat Jaume I
3 *==========================================================================
4 *      PROYECT:        GAME TOOLS
5 *==========================================================================
6 *      CONTENT:
7 *
8 *
9 *      @file   GeoLodStripsLibrary.cpp
10 *===========================================================================*/
11
12#include "GeoLodStripsLibrary.h"
13
14using namespace Geometry;
15
16//-----------------------------------------------------------------------------
17//      Constructors.
18//-----------------------------------------------------------------------------
19LodStripsLibrary::LodStripsLibrary(const LodStripsLibraryData *lodstripsdata, Mesh *geomesh, CREATEINDEXDATAFUNC idfun)
20:create_index_data_func(idfun?idfun:DefaultIndexDataCreator)
21{
22//      const   char    *name_of_file;
23
24        //      Sets the current lod.
25        mCurrentLod     =       0;
26
27        //      Converts a String to const char *.
28//      name_of_file    =       name.data();
29
30        //      Sets the global mesh.
31        mGeoMesh = geomesh;
32
33        dataRetrievalInterface=NULL;
34        indices_x_submesh = NULL;
35        offsets_x_submesh = NULL;
36        mStripsSubmesh = NULL;
37       
38        //      Loads the Level Of Detail file.
39        LodStripsLibrary::LoadStripMesh(lodstripsdata,mGeoMesh);
40
41        GoToLod(0.0f); 
42        minFaces = 0;
43        for (unsigned int strip = 0; strip < mTotalStrips; strip++)
44                minFaces += int(mStrips[strip].size() - 2);
45
46        GoToLod(1.0f);
47        maxFaces = 0;
48        for (unsigned int strip = 0; strip < mTotalStrips; strip++)
49                maxFaces += int(mStrips[strip].size() - 2);
50}
51
52//-----------------------------------------------------------------------------
53//      Destroyer.
54//-----------------------------------------------------------------------------
55LodStripsLibrary::~LodStripsLibrary()
56{
57        delete  [] mStrips;
58        if (dataRetrievalInterface)
59                delete dataRetrievalInterface;
60        if (indices_x_submesh)
61                delete[] indices_x_submesh;
62        if (offsets_x_submesh)
63                delete[] offsets_x_submesh;
64        if (mStripsSubmesh)
65                delete[] mStripsSubmesh;
66}
67
68//-----------------------------------------------------------------------------
69//      Public.
70//-----------------------------------------------------------------------------
71
72//-----------------------------------------------------------------------------
73//      Go to the level of detail.
74//-----------------------------------------------------------------------------
75void LodStripsLibrary::GoToLod(float lodfactor)
76{
77        register        int                                                     j;
78        register        int                                                     offset;
79        long                    int                                                     i;
80        long                    int                                                     k;
81        long                    int                                                     b_change_ok     =       1;
82        unsigned long   int                                     lod;
83        long                    int                                                     t;
84        long                    int                                                     r;
85        long                    int                                                     next;
86        long                    int                                                     el1;
87        long                    int                                                     el2;
88        long                    int                                                     total_changes;
89        long                    int                                                     totalR;
90        SmallIntVector                                          *strip;
91        SmallIntVector::iterator        start;
92        uint32                                                                          return_value;
93
94        uint32 newLOD = (uint32)((1.0f - lodfactor) * mLods);
95   
96        //      Initialize the return value to the current LOD.
97        return_value    =       mCurrentLod;
98
99        if (newLOD != mCurrentLod)
100        {
101                if (newLOD > mCurrentLod)
102                {
103                        //      Forwards.
104                        //      Increments LOD.
105                        for (lod = mCurrentLod; lod < newLOD; lod++)
106                        {
107                                next                                            =       mVertex[lod];
108                                total_changes   =       mPChanges[lod];
109
110                                for (i = 0; i < total_changes; i++)
111                                {
112                                        //      Strip change.
113                                        t                       =       mCurrentRegLOD->strip;
114                                        strip   =       &(mStrips[t]);
115                                        start   =       strip->begin();                         
116                               
117                                        //      Position.
118                                        totalR  =       mCurrentRegLOD->position;
119
120                                        for (j = totalR - 1; j >= 0; j--)
121                                        {
122                                                start[*mCurrentData]    =       next;
123                                               
124                                                ++mCurrentData;
125                                        }
126                                       
127                                        //      L1.
128                                        totalR  =       mCurrentRegLOD->vertexRepetition;
129                                       
130                                        for (j = totalR - 1; j >= 0; j--)
131                                        {
132                                                offset  =       *mCurrentData + 1;
133
134                                                strip->erase(   start + offset,
135                                                                                                        start
136                                                                                                        +
137                                                                                                        offset
138                                                                                                        +
139                                                                                                        (*(mCurrentData + 1)));
140
141                                                ++mCurrentData;
142                                                ++mCurrentData;
143                                        }
144                                       
145                                        //      L2
146                                        totalR  =       mCurrentRegLOD->edgeRepetition;
147                                       
148                                        for (j = totalR - 1; j >= 0; j--)
149                                        {
150                                                offset  =       *mCurrentData + 2;
151                                                strip->erase(   start   +       offset,
152                                                                                                        start
153                                                                                                        +
154                                                                                                        offset
155                                                                                                        +
156                                                                                                        2*(*(mCurrentData + 1)));
157
158                                                mCurrentData++;
159                                                mCurrentData++;
160                                        }
161
162                                        mCurrentRegLOD++;
163                                       
164                                        mStripsChanges[t]       =       1;
165                               
166                                }
167                               
168                                mCurrentLod     =       lod + 1;
169                        }
170
171                }
172                //      If LOD is less than actual.
173                else
174                {
175                        mCurrentRegLOD--;
176                        mCurrentData--;
177                       
178                        //      Decrements LOD.
179                        for (lod = mCurrentLod; lod > newLOD; lod--)
180                        {
181                                total_changes   =       mPChanges[lod - 1];
182                               
183                                for (i = 0; i < total_changes; i++)
184                                {
185                                        t                       =       mCurrentRegLOD->strip;
186                                        strip   =       &(mStrips[t]);
187                                        start   =       mStrips[t].begin();
188
189                                        //      L2.
190                                        totalR  =       mCurrentRegLOD->edgeRepetition;
191                                       
192                                        for (j = 0; j < totalR; j++)
193                                        {
194                                                el1     =       (*strip)[*(mCurrentData - 1)];
195                                                el2     =       (*strip)[*(mCurrentData - 1) + 1];
196                                                r               =       *(mCurrentData);
197                                               
198                                                for (k = 0; k < r; k++)
199                                                {
200                                                        (*strip).insert(start+*(mCurrentData - 1),el2);
201                                                        (*strip).insert(start+*(mCurrentData - 1),el1);
202                                                }
203                                               
204                                                mCurrentData--;
205                                                mCurrentData--;
206                                        }
207
208                                        //      Vertex Repetition.
209                                        totalR  =       mCurrentRegLOD->vertexRepetition;
210                                       
211                                        for (j = 0; j < totalR; j++)
212                                        {
213                                                el1     =       (*strip)[*(mCurrentData-1)];
214                                                r               =       *(mCurrentData);
215                                               
216                                                for (k = 0; k < r; k++)
217                                                {
218                                                        (*strip).insert(start+*(mCurrentData-1),el1);
219                                                }
220
221                                                mCurrentData--;
222                                                mCurrentData--;
223                                        }
224                                       
225                                        //      POS.
226                                        totalR  =       mCurrentRegLOD->position;
227                                       
228                                        for (j = 0; j < totalR; j++)
229                                        {
230                                                (*strip)[*mCurrentData] =       lod - 1;
231                                                mCurrentData--;
232                                        }
233                                       
234                                        mCurrentRegLOD--;
235                                        mStripsChanges[t]       =       1;
236                                }                       
237                               
238                                //      Update LOD.
239                                mCurrentLod     =       lod - 1;
240                        }
241                       
242                        mCurrentData++;
243                        mCurrentRegLOD++;
244                }
245        }
246
247        UpdateDataRetrievalInterface();
248//      return  return_value;
249}
250
251//-----------------------------------------------------------------------------
252//      Returns the number of vertices of the highest LOD.
253//-----------------------------------------------------------------------------
254uint32  LodStripsLibrary::MaxVertices()
255{
256        return  mMaxVerticesLOD;
257}
258
259//-----------------------------------------------------------------------------
260//      Returns the number of vertices of the lowest LOD.
261//-----------------------------------------------------------------------------
262uint32  LodStripsLibrary::MinVertices()
263{
264        uint32  number_of_vertices;
265
266        //      Total vertices of minimum lod.
267        number_of_vertices      =       mMaxVerticesLOD - (mMinLod + 1);
268
269        return  number_of_vertices;
270}
271
272//-----------------------------------------------------------------------------
273//      Returns the number of triangles of the highest LOD.
274//-----------------------------------------------------------------------------
275uint32  LodStripsLibrary::MaxFaces()
276{
277/*      uint32  number_of_faces;
278
279        uint32  current_lod;
280
281        current_lod     =       GoToLod(MaxLod());
282
283        //      Initialize number of faces count.
284        number_of_faces =       0;
285
286        //      For each strip.
287        for (unsigned int strip =       0; strip < mTotalStrips; strip++)
288        {
289                number_of_faces +=      int(mStrips[strip].size() - 2);
290        }
291
292        GoToLod(current_lod);
293
294        return  number_of_faces;*/
295
296        return maxFaces;
297}
298
299//-----------------------------------------------------------------------------
300//      Returns the number of triangles of the lowest LOD.
301//-----------------------------------------------------------------------------
302uint32  LodStripsLibrary::MinFaces()
303{
304/*      uint32  current_lod;
305        uint32  number_of_faces;
306
307        current_lod     =       GoToLod(MinLod());
308
309        //      Initialize number of faces count.
310        number_of_faces =       0;
311
312        //      For each strip.
313        for (unsigned int strip =       0; strip < mTotalStrips; strip++)
314        {
315                number_of_faces +=      int(mStrips[strip].size() - 2);
316        }
317
318        GoToLod(current_lod);
319
320        return  number_of_faces;*/
321
322        return minFaces;
323}
324
325//-----------------------------------------------------------------------------
326//      Establishes the new LOD range.
327//      Only the LODs in that range are stored and used.
328//-----------------------------------------------------------------------------
329void            LodStripsLibrary::TrimByLod(uint32 minLod, uint32 maxLod)
330{
331        //      Refresh number of vercies of the max lod.
332        mMaxVerticesLOD +=      mMaxLod - maxLod;
333       
334        mMinLod =       minLod;
335        mMaxLod =       maxLod;
336}
337/*
338//-----------------------------------------------------------------------------
339//      Get strip count.
340//-----------------------------------------------------------------------------
341uint32  LodStripsLibrary::GetStripCount() const
342{
343        return  (uint32)        mTotalStrips;
344}
345
346//-----------------------------------------------------------------------------
347//      Get Index by strip.
348//-----------------------------------------------------------------------------
349uint32  LodStripsLibrary::GetIndexCountByStrip(uint32 istrip) const
350{
351        return (uint32) mStrips[istrip].size();
352}
353*/
354//-----------------------------------------------------------------------------
355//      Private.
356//-----------------------------------------------------------------------------
357
358//-----------------------------------------------------------------------------
359//      Copy a STL vector to a C array.
360//-----------------------------------------------------------------------------
361void LodStripsLibrary::CopyVectors2Arrays()
362{
363        SmallInt                                        max;
364        SmallInt                                        t;
365        SmallInt                                        *data_array;
366        SmallIntVector          tira;
367        LODRegisterType         *changes_array;
368        LODRegisterVector       list_changes;
369
370        mTotalStrips            =       int(mFileStrips.size());
371        mVertex                                 =       new SmallInt[mTotalVertices];
372        mStripsChanges  =       new SmallInt[mTotalChanges];
373        mStrips                                 =       new SmallIntVector[mTotalStrips];
374        data_array                      =       new SmallInt[mData.size()];
375       
376        max                                     =       0;
377        mTotalFaces     =       0;
378
379//      unsigned int * strip_sizes = new unsigned int[mFileStrips.size()];
380        unsigned int total_strip_size = 0;
381       
382        for (unsigned   int     i = 0; i < mFileStrips.size(); i++)
383        {
384                total_strip_size += t = int(mFileStrips[i].size());
385                if (t>max)
386                        max     =       t;
387                mTotalFaces     +=      t - 2;
388        }
389
390        changes_array   =       new LODRegisterType[mFileChangesLOD.size()];
391       
392        //      Fill up changes array.
393        for (unsigned   int     i = 0; i < mFileChangesLOD.size(); ++i)
394        {
395                changes_array[i]        =       mFileChangesLOD[i];
396        }
397
398        mCurrentRegLOD  =       changes_array;
399       
400        //      Copy strips.
401        unsigned int numDegenerated = 2*mTotalStrips - 2;
402        dataRetrievalInterface=create_index_data_func(total_strip_size+numDegenerated); // temporal!
403        dataRetrievalInterface->Begin();
404
405        unsigned        int     ii = 0;
406
407        for (unsigned   int     i = 0; i < mFileStrips.size(); i++)
408        {
409                for (unsigned   int     j = 0; j < mFileStrips[i].size(); j++,ii++)
410                {
411                        mStrips[i].push_back(mFileStrips[i][j]);
412                        dataRetrievalInterface->SetIndex(ii,mFileStrips[i][j]);
413                }
414               
415                //      Reset flags of strips changed.
416                mStripsChanges[i]       =       0;
417        }
418        dataRetrievalInterface->End();
419
420        for (unsigned   int     i = 0; i < mData.size(); i++)
421        {
422                data_array[i]   =       mData[i];
423        }
424       
425        mCurrentData    =       data_array;
426
427        //      Fill up mVertex.
428        for(unsigned    int     i = 0; i < mFileVertices.size(); i++)
429        {
430                mVertex[i]      =       mFileVertices[i];
431        }
432
433//      delete[] strip_sizes;
434}
435
436//-----------------------------------------------------------------------------
437//      LoadStripMesh
438//-----------------------------------------------------------------------------
439void LodStripsLibrary::LoadStripMesh(const LodStripsLibraryData *lodstripsdata, Mesh *geomesh)
440{
441        //FILE                                                                                                                  *fp;
442        SmallIntVector                                                                          strip_aux;
443        //LODRegisterType                                                                               lod_register;
444        std::vector                     <LODRegisterType>       list_pos;
445        std::vector                     <unsigned int>          p_changes;
446        //int                                                                                                                           value;
447        int                                                                                                                             v                       =       0;
448        int                                                                                                                             t                       =       -1;
449        int                                                                                                                             s                       =       0;
450        int                                                                                                                             next    =       0;
451        int                                                                                                                             c                       =       -1;
452        //char                                                                                                                  buff[80];
453        Index                                                                                                                   *index;
454        Index                                                                                                                   *indexBegin;
455        Index                                                                                                                   *indexEnd;
456        SubMesh                                                                                                         *geoSubMesh;
457
458        //      For each one of the submeshes.
459        indices_x_submesh = new int[geomesh->mSubMeshCount];
460        offsets_x_submesh = new int[geomesh->mSubMeshCount];
461        for (unsigned int submesh       =       0; submesh < geomesh->mSubMeshCount; submesh++)
462        {
463                geoSubMesh      =       &geomesh->mSubMesh[submesh];
464
465                //      For each one of the strips.
466                for (unsigned int strip = 0; strip < geoSubMesh->mStripCount; strip++)
467                {
468                        // Insert an empty strip.
469                        t++;
470                        strip_aux.clear();
471                        mFileStrips.push_back(strip_aux);
472
473                        //      First index of the strip.
474                        indexBegin      =       geoSubMesh->mStrip[strip];
475
476                        //      If the strips is not the first.
477                       
478/*                      if (strip != 0)
479                        {
480                                indexBegin++;
481                        }
482*/                     
483
484                        //      If is the final strip
485                        if (strip       == (geoSubMesh->mStripCount - 1))
486                        {
487                                //      The end of the index array.
488                                indexEnd        = &geoSubMesh->mIndex[geoSubMesh->mIndexCount];
489                        }
490                        else
491                        {
492                                //      The beginning of the next strip.
493                                indexEnd        = geoSubMesh->mStrip[strip + 1];
494
495                                //      Remove degenerated
496//                              indexEnd--;
497                        }
498                       
499                        int i;
500                        i       = 0;
501                        indices_x_submesh[submesh] = 0;
502                        if (submesh==0)
503                                offsets_x_submesh[submesh] = 0;
504                        else
505                                offsets_x_submesh[submesh] = indices_x_submesh[submesh-1];
506
507                        //      For each index of the strip.
508                        for (index = indexBegin; index < indexEnd; index++)
509                        {
510                                mFileStrips[t].push_back(indexBegin[i]);
511                                indices_x_submesh[submesh]++;
512                                i++;
513                        }                       
514                }
515        }
516
517        //      TODO:   LOD file loaded in load file mesh process.
518        //      Open the LOD file.
519/*      if ((fp = fopen (name, "r")) == NULL)
520        {
521                //      Error in open.
522        }
523        else
524        {
525                mTotalChanges   =       0;
526               
527                while (!feof (fp))
528                {
529                        fgets (buff, 80, fp);
530                       
531                        if (*buff == 'v')
532                        {
533                                sscanf (buff+2,"%d",&value);
534                               
535                                mFileVertices.push_back(value);
536                                v++;
537                        }
538                        else if ( (*buff == 'c') || (*buff == 'd'))
539                        {
540                                if (*buff == 'c')
541                                {
542                                        c++;
543                                }
544                                else
545                                {
546                                        unsigned        int     a;
547                                        unsigned        int     b;
548                                        unsigned        int     c;
549                                        unsigned        int     d;
550                                       
551                                        sscanf (buff+2,"%u %u %u %u\n",&a,&b,&c,&d);
552                                        lod_register.strip                                              =       a;
553                                        lod_register.position                                   =       b;
554                                        lod_register.vertexRepetition   =       c;
555                                        lod_register.edgeRepetition             =       d;
556                                       
557                                        mFileChangesLOD.push_back(lod_register);
558                                }
559                        }
560                        else if (*buff=='b')
561                        {
562                                sscanf(buff+2,"%u",&value);
563                                mData.push_back(value);
564                        }
565                        else if (*buff=='p')
566                        {
567                                sscanf (buff+2,"%u",&value);
568                                p_changes.push_back(value);
569                        }
570                }
571               
572                fclose(fp);*/
573
574        mFileVertices = lodstripsdata->mFileVertices;
575        mFileChangesLOD = lodstripsdata->mFileChangesLOD;
576        mData = lodstripsdata->mData;
577        p_changes = lodstripsdata->p_changes;
578
579        mStripsSubmesh = new int [geomesh->mSubMeshCount];
580        for (size_t     submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
581        {       
582                mStripsSubmesh[submesh]=0;
583        }
584
585                       
586        //      Max / Min values for LOD.
587        mLods                                                                                   =       int(p_changes.size());
588        mMaxLod                                                                         =       0;
589        mMinLod                                                                         =       mLods;
590
591        mPChanges                       =       new SmallInt[mLods];
592       
593        for (unsigned int i = 0; i < mLods; i++)
594        {
595                mPChanges[i] = p_changes[i];
596        }
597
598        mTotalVertices          =       int(mFileVertices.size());
599        mMaxVerticesLOD         =       mTotalVertices;
600        mTotalStrips                    =       int(mFileStrips.size());
601        mTotalChanges                   =       int(mFileChangesLOD.size());
602
603        //Copy the data to the structure we will use
604        CopyVectors2Arrays();
605//      }
606}
607
608void LodStripsLibrary::UpdateDataRetrievalInterface(void)
609{
610        unsigned        int     ii      =       0;
611       
612        dataRetrievalInterface->Begin();
613
614        int                     counter                                 =       0;
615        int                     target_submesh  =       0;
616        size_t  strip_count                     =       0;
617
618        for (unsigned   int     i       =       0;      i < mTotalStrips;       i++,    strip_count++)
619        {
620                if (strip_count >= mGeoMesh->mSubMesh[target_submesh].mStripCount)
621                {
622                        indices_x_submesh[target_submesh] = counter;
623
624                        if (target_submesh==0)
625                        {
626                                offsets_x_submesh[target_submesh] = 0;
627                        }
628                        else
629                        {
630                                offsets_x_submesh[target_submesh] = indices_x_submesh[target_submesh-1] + offsets_x_submesh[target_submesh-1];
631                        }
632
633                        counter                                                                                                 =       0;
634                        mStripsSubmesh[target_submesh]  =       (int)strip_count;
635                        strip_count                                                                                     =       0;
636
637                        target_submesh++;
638                }
639               
640                int lastindex = -1;
641                for (SmallIntVector::iterator it=mStrips[i].begin(); it!=mStrips[i].end(); it++, ii++)
642                {
643/*                      // repeat the first index of the strip: degenerate
644                        if (lastindex==-1)
645                        {
646                                lastindex=*it;
647                                dataRetrievalInterface->SetIndex(ii,*it);                               
648                                counter++;
649                                ii++;
650                        }*/
651                        lastindex=*it;
652                        dataRetrievalInterface->SetIndex(ii,*it);
653                        counter++;
654                }
655
656/*              // last repeat last vertex of the strip: degenerate
657                dataRetrievalInterface->SetIndex(ii,lastindex);
658                counter++;
659                ii++;*/
660
661                dataRetrievalInterface->SetNumValidIndices(ii);
662        }
663
664        if (strip_count >= mGeoMesh->mSubMesh[target_submesh].mStripCount)
665        {
666                indices_x_submesh[target_submesh] = counter;
667                if (target_submesh==0)
668                        offsets_x_submesh[target_submesh] = 0;
669                else
670                        offsets_x_submesh[target_submesh] = indices_x_submesh[target_submesh-1] + offsets_x_submesh[target_submesh-1];
671
672                counter = 0;
673                mStripsSubmesh[target_submesh] = (int)strip_count;
674                strip_count = 0;
675                target_submesh++;
676        }
677
678        dataRetrievalInterface->End();
679}
Note: See TracBrowser for help on using the repository browser.