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

Revision 985, 13.7 KB checked in by gumbau, 18 years ago (diff)
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(     std::string     name,
20                                                                                                                                                Mesh                            *geomesh)
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        //      Loads the Level Of Detail file.
34        LodStripsLibrary::LoadStripMesh((char*)name_of_file,mGeoMesh);
35}
36
37//-----------------------------------------------------------------------------
38//      Destroyer.
39//-----------------------------------------------------------------------------
40LodStripsLibrary::~LodStripsLibrary()
41{
42                delete[] mStrips;
43}
44
45//-----------------------------------------------------------------------------
46//      Public.
47//-----------------------------------------------------------------------------
48
49/*
50        Go to the level of detail.
51*/
52uint32 LodStripsLibrary::GoToLod(uint32 newLOD)
53{
54        register        int                                                     j;
55        register        int                                                     offset;
56        long                    int                                                     i;
57        long                    int                                                     k;
58        long                    int                                                     b_change_ok     =       1;
59        unsigned long   int                                                     lod;
60        long                    int                                                     t;
61        long                    int                                                     r;
62        long                    int                                                     sig;
63        long                    int                                                     el1;
64        long                    int                                                     el2;
65        long                    int                                                     total_changes;
66        long                    int                                                     totalR;
67        SmallIntVector                                          *strip;
68        SmallIntVector::iterator        start;
69        uint32                                                                          return_value;
70
71        //      Initialize the return value to the current LOD.
72        return_value    =       mCurrentLod;
73
74        if (newLOD != mCurrentLod)
75        {
76                if (newLOD > mCurrentLod)
77                {
78                        //HACIA ADELANTE
79                        //CurrentRegLOD
80                        //Incrementamos el LOD -> Calcular siguientes
81                        for (lod = mCurrentLod; lod < newLOD; lod++)
82                        {
83                                sig                             =       mVertex[lod];
84                                total_changes   =       mPChanges[lod];
85
86                                for (i = 0; i < total_changes; i++)
87                                {
88                                        //      Strip q Cambia.
89                                        t                       =       mCurrentRegLOD->strip;
90                                        strip   =       &(mStrips[t]);
91                                        start   =       strip->begin();                         
92                               
93                                        //      Position.
94                                        totalR  =       mCurrentRegLOD->position;
95
96                                        for (j = totalR - 1; j >= 0; j--)
97                                        {
98                                                start[*mCurrentData]    =       sig;
99                                               
100                                                ++mCurrentData;
101                                        }
102                                       
103                                        //      L1.
104                                        totalR  =       mCurrentRegLOD->vertexRepetition;
105                                       
106                                        for (j = totalR - 1; j >= 0; j--)
107                                        {
108                                                offset  =       *mCurrentData + 1;
109
110                                                strip->erase(   start + offset,
111                                                                                                        start
112                                                                                                        +
113                                                                                                        offset
114                                                                                                        +
115                                                                                                        (*(mCurrentData + 1)));
116
117                                                ++mCurrentData;
118                                                ++mCurrentData;
119                                        }
120                                       
121                                        //      L2
122                                        totalR  =       mCurrentRegLOD->edgeRepetition;
123                                       
124                                        for (j = totalR - 1; j >= 0; j--)
125                                        {
126                                                offset  =       *mCurrentData + 2;
127                                                strip->erase(   start   +       offset,
128                                                                                                        start
129                                                                                                        +
130                                                                                                        offset
131                                                                                                        +
132                                                                                                        2*(*(mCurrentData + 1)));
133
134                                                ++mCurrentData;
135                                                ++mCurrentData;
136                                        }
137
138                                        ++mCurrentRegLOD;
139                                       
140                                        mStripsChanges[t]       =       1;
141                               
142                                }
143                               
144                                mCurrentLod     =       lod + 1;
145                        }
146
147                }
148                //      Si el nuevo LOD es menos que el actual.
149                else
150                {
151                        --mCurrentRegLOD;
152                        --mCurrentData;
153                       
154                        //      Decrementamos el LOD -> Calcular anteriores.
155                        for (lod = mCurrentLod; lod > newLOD; lod--)
156                        {
157                                total_changes   =       mPChanges[lod - 1];
158                               
159                                for (i = 0; i < total_changes; i++)
160                                {
161                                        t                       =       mCurrentRegLOD->strip;
162                                        strip   =       &(mStrips[t]);
163                                        start   =       mStrips[t].begin();
164
165                                        //      L2.
166                                        totalR  =       mCurrentRegLOD->edgeRepetition;
167                                       
168                                        for (j = 0; j < totalR; j++)
169                                        {
170                                                el1     =       (*strip)[*(mCurrentData - 1)];
171                                                el2     =       (*strip)[*(mCurrentData - 1) + 1];
172                                                r               =       *(mCurrentData);
173                                               
174                                                for (k = 0; k < r; k++)
175                                                {
176                                                        (*strip).insert(start+*(mCurrentData - 1),el2);
177                                                        (*strip).insert(start+*(mCurrentData - 1),el1);
178                                                }
179                                               
180                                                --mCurrentData;
181                                                --mCurrentData;
182                                        }
183
184                                        //      Vertex Repetition.
185                                        totalR  =       mCurrentRegLOD->vertexRepetition;
186                                       
187                                        for (j = 0; j < totalR; j++)
188                                        {
189                                                el1     =       (*strip)[*(mCurrentData-1)];
190                                                r               =       *(mCurrentData);
191                                               
192                                                for (k = 0; k < r; k++)
193                                                {
194                                                        (*strip).insert(start+*(mCurrentData-1),el1);
195                                                }
196
197                                                --mCurrentData;
198                                                --mCurrentData;
199                                        }
200                                       
201                                        //      POS.
202                                        totalR  =       mCurrentRegLOD->position;
203                                       
204                                        for (j = 0; j < totalR; j++)
205                                        {
206                                                (*strip)[*mCurrentData] =       lod - 1;
207                                                --mCurrentData;
208                                        }
209                                       
210                                        --mCurrentRegLOD;
211                                        mStripsChanges[t]       =       1;
212                                }                       
213                               
214                                //      Actualizar LOD.
215                                mCurrentLod     =       lod - 1;
216                        }
217                       
218                        ++mCurrentData;
219                        ++mCurrentRegLOD;
220                }
221
222        }       
223
224        /*
225        // Visualizar.
226        k               =       0;
227
228        for (uint32 i   =       0; i < mTotalStrips; i++)
229        {
230                l                       =       mStrips[i].size();
231               
232                //      Los dos degenerados.
233                k                       =       k + l + 2;
234               
235                //      If is not the first strip.
236                if (i != 0)
237                {
238                        start   =       mStrips[i].begin();
239
240                        //      Degenerado.
241                        mStrips[i].insert(start,mStrips[i][0]);
242                }
243
244                //      If is not the last strip.
245                if (i != (mTotalStrips - 1))
246                {
247                        //      Degenerado.
248                        mStrips[i].push_back(mStrips[i][mStrips[i].size() - 1]);
249                }
250
251        }
252        */
253       
254        /*
255         * Write in the hardware buffer.
256         *
257        int contTira    = 0;
258       
259        //      Vuelca los datos en el buffer de pantalla.
260        //Ogre::HardwareIndexBufferSharedPtr    ibuf;
261        for(unsigned int j = 0; j < mGeoMesh->mSubMeshCount; j++)
262        {
263                k       =       0;
264                for(unsigned int s = 0; s < mGeoMesh->mSubMesh[j].mStripCount; s++)
265                {
266                        l       =       mStrips[contTira].size();
267
268                        mIndexBuffer[j]->writeData(k*sizeof(unsigned long),
269                                        l*sizeof(unsigned long),
270                                        &mStrips[contTira][0]);
271
272                        k       =       k + l;
273
274                        contTira++;
275                }
276
277                indexes[j]->indexCount  =       k;
278        }
279        */
280       
281        /*
282        //      Para el bunny.
283        start   =       mStrips[0].begin();
284        mStrips[0].erase(start);
285        */
286        /*
287        for (uint32 i   =       0; i < mTotalStrips; i++)
288        {
289                start   =       mStrips[i].begin();
290               
291                //      If is not the first strip.
292                if (i != 0)
293                {
294                        mStrips[i].erase(start);
295                }
296
297                //      If is not the last strip.
298                if (i != (mTotalStrips - 1))
299                {
300                        mStrips[i].pop_back();
301                }
302
303        }
304        */
305       
306        return  return_value;
307}
308
309
310/*
311        Gets the maximum level of detail.
312*/
313uint32 LodStripsLibrary::MaxLod()
314{
315        return  mMaxLod;
316}
317
318/*
319        Gets the minimum level of detail.
320*/
321uint32 LodStripsLibrary::MinLod()
322{
323        return  mMinLod;
324}
325
326/// Returns the number of vertices of the highest LOD.
327uint32  LodStripsLibrary::MaxVertices()
328{
329        uint32  number_of_vertices;
330
331        number_of_vertices      =       mTotalVertices;
332
333        return  number_of_vertices;
334}
335
336/// Returns the number of vertices of the lowest LOD.
337uint32  LodStripsLibrary::MinVertices()
338{
339        uint32  number_of_vertices;
340
341        //      Total vertices less total lod.
342        number_of_vertices      =       mTotalVertices - (mMaxLod + 1);
343
344        return  number_of_vertices;
345}
346
347/// Returns the number of triangles of the highest LOD.
348uint32  LodStripsLibrary::MaxFaces()
349{
350        uint32  number_of_faces;
351
352        uint32  current_lod;
353
354        current_lod     =       GoToLod(MaxLod());
355
356        //      Initialize number of faces count.
357        number_of_faces =       0;
358
359        //      For each strip.
360        for (unsigned int strip =       0; strip < mTotalStrips; strip++)
361        {
362                number_of_faces +=      int(mStrips[strip].size() - 2);
363        }
364
365        GoToLod(current_lod);
366
367        return  number_of_faces;
368}
369
370/// Returns the number of triangles of the lowest LOD.
371uint32  LodStripsLibrary::MinFaces()
372{
373        uint32  current_lod;
374        uint32  number_of_faces;
375
376        current_lod     =       GoToLod(MinLod());
377
378        //      Initialize number of faces count.
379        number_of_faces =       0;
380
381        //      For each strip.
382        for (unsigned int strip =       0; strip < mTotalStrips; strip++)
383        {
384                number_of_faces +=      int(mStrips[strip].size() - 2);
385        }
386
387        GoToLod(current_lod);
388
389        return  number_of_faces;
390}
391
392/// Establishes the new LOD range.
393/// Only the LODs in that range are stored and used.
394void            LodStripsLibrary::TrimByLod(uint32 minLod, uint32 maxLod)
395{
396        mMinLod =       minLod;
397        mMaxLod =       maxLod;
398}
399
400uint32  LodStripsLibrary::GetStripCount() const
401{
402        return  (uint32)        mTotalStrips;
403}
404
405uint32  LodStripsLibrary::GetIndexCountByStrip(uint32 istrip) const
406{
407        return (uint32) mStrips[istrip].size();
408}
409
410//-----------------------------------------------------------------------------
411//      Private.
412//-----------------------------------------------------------------------------
413
414/*
415        Copy a STL vector to a C array.
416*/
417void LodStripsLibrary::CopyVectors2Arrays()
418{
419        unsigned int                                    i;
420        unsigned int                                    j;
421        SmallInt                                        max;
422        SmallInt                                        t;
423        SmallInt                                        *data_array;
424        SmallIntVector          tira;
425        LODRegisterType         *changes_array;
426        LODRegisterVector       list_changes;
427
428        mTotalStrips            =       int(mFileStrips.size());
429        mVertex                                 =       new SmallInt[mTotalVertices];
430        mStripsChanges  =       new SmallInt[mTotalChanges];
431        mStrips                                 =       new SmallIntVector[mTotalStrips];
432        data_array                      =       new SmallInt[mData.size()];
433       
434        //Calcular el tamaño maximo de tira y el numero de caras total
435        max                                     =       0;
436        mTotalFaces     =       0;
437       
438        for (i = 0; i < mFileStrips.size(); i++)
439        {
440                t       =       int(mFileStrips[i].size());
441               
442                if (t>max)
443                {
444                        max     =       t;
445                }
446
447                mTotalFaces     +=      t - 2;
448        }
449
450        //Nueva version de ALLOCATE.
451        changes_array   =       new LODRegisterType[mFileChangesLOD.size()];
452       
453        //Rellenar vChanges
454        for (i = 0; i < mFileChangesLOD.size(); ++i)
455        {
456                changes_array[i]        =       mFileChangesLOD[i];
457        }
458
459        mCurrentRegLOD  =       changes_array;
460       
461        //COPY----------------------------------
462        //Copiar las tiras
463        for (i = 0; i < mFileStrips.size(); i++)
464        {
465                for (j = 0; j < mFileStrips[i].size(); j++)
466                {
467                        //Rellenar el vVector
468                        mStrips[i].push_back(mFileStrips[i][j]);
469                }
470               
471                //Poner a 0 los flags de modificados.
472                mStripsChanges[i]       =       0;
473        }
474
475        for (i = 0; i < mData.size(); i++)
476        {
477                data_array[i]   =       mData[i];
478        }
479       
480        mCurrentData    =       data_array;
481
482        //Meter los vertices en mVertex
483        for(i = 0; i < mFileVertices.size(); i++)
484        {
485                mVertex[i]      =       mFileVertices[i];
486        }
487}
488
489//-----------------------------------------------------------------------------
490// LoadStripMesh
491//-----------------------------------------------------------------------------
492void LodStripsLibrary::LoadStripMesh(char *name, Mesh *geomesh)
493{
494        FILE                                                                                                                    *fp;
495        SmallIntVector                                                                          strip_aux;
496        LODRegisterType                                                                         lod_register;
497        std::vector                     <LODRegisterType>       list_pos;
498        std::vector                     <unsigned int>          p_changes;
499        int                                                                                                                             value;
500        int                                                                                                                             v               =       0;
501        int                                                                                                                             t               =       -1;
502        int                                                                                                                             s               =       0;
503        int                                                                                                                             sig     =       0;
504        int                                                                                                                             c               =       -1;
505        char                                                                                                                    buff[80];
506        Index                                                                                                                   *index;
507        Index                                                                                                                   *indexBegin;
508        Index                                                                                                                   *indexEnd;
509        SubMesh                                                                                                         *geoSubMesh;
510
511        //      For each one of the submeshes.
512        for (unsigned int submesh       =       0; submesh < geomesh->mSubMeshCount; submesh++)
513        {
514                geoSubMesh      =       &geomesh->mSubMesh[submesh];
515
516                //      For each one of the strips.
517                for (unsigned int strip = 0; strip < geoSubMesh->mStripCount; strip++)
518                {
519                        // Insert an empty strip.
520                        t++;
521                        strip_aux.clear();
522                        mFileStrips.push_back(strip_aux);
523
524                        //      First index of the strip.
525                        indexBegin      =       geoSubMesh->mStrip[strip];
526
527                        //      If the strips is not the first.
528                        /*
529                        if (strip != 0)
530                        {
531                                indexBegin++;
532                        }
533                        */
534
535                        //      If is the final strip
536                        if (strip       == (geoSubMesh->mStripCount - 1))
537                        {
538                                //      The end of the index array.
539                                indexEnd        = &geoSubMesh->mIndex[geoSubMesh->mIndexCount];
540                        }
541                        else
542                        {
543                                //      The beginning of the next strip.
544                                indexEnd        = geoSubMesh->mStrip[strip + 1];
545
546                                //      Remove degenerated
547                                //indexEnd--;
548                        }
549                       
550                        int i;
551                        i       = 0;
552                        //      For each index of the strip.
553                        for (index = indexBegin; index < indexEnd; index++)
554                        {
555                                mFileStrips[t].push_back(indexBegin[i]);
556                                i++;
557                        }
558                }
559        }
560
561        //      Open the LOD file.
562        if ((fp = fopen (name, "r")) == NULL)
563        {
564                //      Error in open.
565        }
566        else
567        {
568                mTotalChanges   =       0;
569               
570
571                while (!feof (fp))
572                {
573                        fgets (buff, 80, fp);
574                       
575                        if (*buff == 'v')
576                        {
577                                //VERTICES
578                                sscanf (buff+2,"%d",&value);
579                               
580                                mFileVertices.push_back(value);
581                                v++;
582                        }
583                        else if ( (*buff == 'c') || (*buff == 'd'))
584                        {
585                                if (*buff == 'c')
586                                {
587                                        // c indica que empieza un nuevo cambio
588                                        c++;
589                                }
590                                else
591                                {
592                                        unsigned        int     a;
593                                        unsigned        int     b;
594                                        unsigned        int     c;
595                                        unsigned        int     d;
596                                       
597                                        sscanf (buff+2,"%u %u %u %u\n",&a,&b,&c,&d);
598                                        lod_register.strip                                              =       a;
599                                        lod_register.position                                   =       b;
600                                        lod_register.vertexRepetition   =       c;
601                                        lod_register.edgeRepetition             =       d;
602                                       
603                                        mFileChangesLOD.push_back(lod_register);
604                                }
605                        }
606                        else if (*buff=='b')
607                        {
608                                sscanf(buff+2,"%u",&value);
609                                mData.push_back(value);
610                        }
611                        else if (*buff=='p')
612                        {
613                                sscanf (buff+2,"%u",&value);
614                                p_changes.push_back(value);
615                        }//Fin ElseIf
616
617                } // End WhileEOF
618               
619                fclose(fp);
620               
621                //      Max / Min values for LOD.
622                mLods                                                                                   =       int(p_changes.size());
623                mMinLod                                                                         =       0;
624                mMaxLod                                                                         =       mLods;
625
626                //// CHAPUZA PROVISIONAL: EL ULTIMO ELEMENTO SE REPITE 2 VECES V.v
627                mPChanges                       =       new SmallInt[mLods];
628               
629                for (unsigned int i = 0; i < mLods; i++)
630                {
631                        mPChanges[i] = p_changes[i];
632                }
633
634                mTotalVertices                  =       int(mFileVertices.size());
635                mTotalStrips                            =       int(mFileStrips.size());
636                mTotalChanges                           =       int(mFileChangesLOD.size());
637
638                //Copy the data to the structure we will use
639                CopyVectors2Arrays();
640        }
641}
642
Note: See TracBrowser for help on using the repository browser.