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

Revision 985, 28.6 KB checked in by gumbau, 18 years ago (diff)
Line 
1#include <iostream>
2#include <fstream>
3
4#include "GeoLodStripsConstructor.h"
5
6using namespace Geometry;
7
8//--------------------------------------------------------------------------
9//Call GenerarModeloV and CopiarVectorsArraysNUEVAED to make changes
10//---------------------------------------------------------------------------
11//      Se utiliza en el constructor sobrecargado.
12void LodStripsConstructor::GenerarModeloCompleto(TIPOFUNC       upb)
13{       
14        float   percent;
15
16        percent =       10;
17
18        this->GenerarModeloV(upb);
19        this->CopiarVectors2ArraysNUEVAED();
20
21        //      Last updtate of the progress bar.
22        upb(percent);
23}
24
25//-----------------------------------------------------------------------------------
26//It makes Lods changes and count them
27//-----------------------------------------------------------------------------------
28void LodStripsConstructor::CalcularCambiosLODsVNuevaED(TIPOFUNC upb)
29{
30//      LODPosition                                             pos;
31//      vector  <LODPosition>   listaPos;
32        long            int                                                     k;
33        long            int                                                     i;
34        unsigned long           int                                     t;
35        long            int                                                     r;
36        unsigned long           int                                     v;
37        long            int                                                     p0;
38        long            int                                                     lod     =       0;
39        long            int                                                     sigue;
40        long            int                                                     ac[3]   =       {0,0,0};
41        long            int                                                     totalRegs       =       0;
42        long            int                                                     RegsCambios;
43        vector  <VECTORINT>                     TiraOriginal;
44        LODData                                                 regLOD;
45        LODChanges                                              listaCambios;
46        float   percent;
47        long    int     update;
48
49        //      Guardamos copia original.
50        TiraOriginal    =       this->cStrips;
51
52        this->MARCAVACIO        =       int(10 * this->cVerts.size());
53       
54        listaCambios.clear();
55
56        update  =       0;
57       
58        for     (lod = 0; ((this->cVerts[lod].Next) != -1); lod++)
59        {
60                update++;
61        }
62
63        update  =       update  / 20;
64        percent =       0.5;
65       
66        this->LodsDisp  =       this->TOTALCAMBIOS      =       lod;
67        pCambios                                =       new uint32[this->TOTALCAMBIOS];
68        pCambios[0]                     =       0;
69       
70        //      5 min.
71        for     (lod = 0; ((this->cVerts[lod].Next) != -1); lod++)
72        {
73                if ((lod % update) == 0)
74                {
75                        upb(percent);
76                }
77               
78                RegsCambios                             =       0;
79                regLOD.obligatory       =       this->Ordenacion[lod].obligatorio;
80               
81                for(t = 0; t < this->cStrips.size(); t++)
82                {
83                        //Inicializar
84                        regLOD.strip    =       this->MARCAVACIO;
85
86                        //PRIMERO: los cambios de los vertices
87                        totalRegs               =       0;
88                       
89                        for (v = 0; v < this->cStrips[t].size(); v++)
90                        {
91                                //      RESOLUCION VARIABLE MAL.
92                                if (this->cStrips[t][v] == lod)
93                                {
94                                        //Gestion de Tiras.
95                                        if (regLOD.strip == this->MARCAVACIO)
96                                        {
97                                                regLOD.strip    =       t;
98                                        }
99                                       
100                                        ++totalRegs;
101
102                                        cDatos.push_back(v);
103
104                                        this->cStrips[t][v]     =       this->cVerts[lod].Next;
105                                }
106                        }
107                       
108                        regLOD.nP       =       char(totalRegs);
109
110                        //SEGUNDO: Simplificaciones de Longitud 1
111                        // Cambio para simplifaciones de nivel 1 mayores de 2
112                        totalRegs       =       0;
113                        i                                       =       0;
114                        r                                       =       0;
115                        sigue                   =       0;
116                        k                                       =       long(this->cStrips[t].size()-3);
117                       
118                        while (i < k)
119                        {
120                                if (    (this->cStrips[t][i] == this->cStrips[t][i+1])
121                                                        &&
122                                                        (this->cStrips[t][i+1] == this->cStrips[t][i+2])
123                                                )
124                                {
125                                        //      Salvar el inicio de la repeticion.
126                                        if (!r)
127                                        {
128                                                p0      =       i;
129                                        }
130                                       
131                                        this->cStrips[t].erase(this->cStrips[t].begin()+i);
132                                        i--;
133                                        r++;
134                                       
135                                        if ((i+1) != this->cStrips[t].size()-1)
136                                        {
137                                                sigue   =       1;
138                                        }
139                                        else
140                                        {
141                                                sigue   =       0;
142                                        }
143                                       
144                                }
145                                else
146                                {
147                                        sigue   =       0;
148                                }
149
150                                if (!sigue && r)
151                                {
152                                        if (regLOD.strip == this->MARCAVACIO)
153                                        {
154                                                regLOD.strip    =       t;
155                                        }
156                                       
157                                        ++totalRegs;
158
159                                        cDatos.push_back(p0);
160                                        cDatos.push_back(r);
161
162                                        r       =       0;
163                                        k       =       long(this->cStrips[t].size()-r-3);
164                                }
165
166                                i++;
167                        }
168                       
169                        regLOD.nL1      =       char(totalRegs);
170
171                        //TERCERO: Simplificaciones de Longitud 2 tipo V1 V2 V1 V2 -> V1 V2
172                        totalRegs       =       0;
173                        i                                       =       0;
174                        r                                       =       0;
175                        sigue                   =       0;
176                        k                                       =       long(this->cStrips[t].size() - 4);
177                       
178                        while   (i < k)
179                        {
180                                if (    (this->cStrips[t][i] == this->cStrips[t][i+2])
181                                                        &&
182                                                        (this->cStrips[t][i+1] == this->cStrips[t][i+3])
183                                                )
184                                {
185                                        if (!r)
186                                        {
187                                                p0      =       i;
188                                        }
189                                       
190                                        this->cStrips[t].erase(this->cStrips[t].begin()+i);
191                                        this->cStrips[t].erase(this->cStrips[t].begin()+i);
192                                        r++;
193                                       
194                                        i       -=      2;
195                                       
196                                        if ((i+1) != this->cStrips[t].size()-3)
197                                        {
198                                                sigue   =       1;
199                                        }
200                                        else
201                                        {
202                                                sigue   =       0;
203                                        }
204                                       
205                                }
206                                else
207                                {
208                                        sigue   =       0;
209                                }
210
211                                if (!sigue && r)
212                                {
213                                        if (regLOD.strip == this->MARCAVACIO)
214                                        {
215                                                regLOD.strip    =       t;
216                                        }
217                                       
218                                        ++totalRegs;
219
220                                        cDatos.push_back(p0);
221                                        cDatos.push_back(r);
222                                       
223                                        r       =       0;
224                                        k       =       long((this->cStrips[t].size()-4-2*r));
225                                }
226
227                                i++;
228                        }
229                       
230                        regLOD.nL2      =       char(totalRegs);
231                       
232                        if (regLOD.strip != this->MARCAVACIO)
233                        {
234                                RegsCambios++;
235                                listaCambios.push_back(regLOD);
236                        }
237                }//     End for t.
238               
239                pCambios[lod]   =       RegsCambios;
240        }//     End for lod.
241
242        this->cCambios  =       listaCambios;
243
244        //      Guardar el numero de LODS disponibles.
245        this->TOTALCAMBIOS      =       this->LodsDisp=lod;
246
247        //Retornar a la lista original
248        this->cStrips   =       TiraOriginal;
249}
250
251//-----------------------------------------------------------------------
252//Put in order vertex of Mesh from simplification sequence
253//-----------------------------------------------------------------------
254void LodStripsConstructor::OrdenarModeloVQSLIM(TIPOFUNC upb)
255{
256        unsigned long int i;
257        unsigned long int j;
258        unsigned long int t;
259        unsigned long int v;
260        long int vSig;
261        long int k;
262        VECTORVERTEX                            Vertices;
263        VECTORVERTEX                            NuevosVerts;
264        vector  <VECTORINT>     NuevasTiras;
265        VECTORINT                                               tira;
266        vector  <Index>                 mV1Aux;
267        tipoOrden                                               Orden;
268        tipoOrden                                               *Ord;
269        bool                                                            bEncontrado;
270        long    int                                             c1;
271        long    int                                             c2;
272        //      Progress bar
273        float   percent;
274        long    int     update;
275
276        bEncontrado = false;
277
278        c1      =       0;
279        c2      =       0;
280       
281        update  =       long(geoMeshSQ->mSteps.size() / 20);
282        percent =       0.5;
283       
284        for     (i = 0; i < this->geoMeshSQ->mSteps.size(); i++)
285        {
286                if ((i % update) == 0)
287                {
288                        upb(percent);
289                }
290               
291                mV1Aux.push_back(this->geoMeshSQ->mSteps[i].mV1);
292
293                Orden.vQslimNext        =       this->geoMeshSQ->mSteps[i].mV0;
294                Orden.vQslim                    =       this->geoMeshSQ->mSteps[i].mV1;
295                Orden.obligatorio       =       this->geoMeshSQ->mSteps[i].obligatorio;
296                Orden.vLS                                       =       i;
297
298                Orden.x                                         =       this->geoMeshSQ->mSteps[i].x;
299                Orden.vLSNext                   =       -1;
300                Orden.cambio                    =       "n";
301
302                Ordenacion.push_back(Orden);
303        }
304
305        this->NumVertsRepetidos = c2;
306
307        update  =       long(Ordenacion.size() / 20);
308        percent =       0.5;
309
310        //      1 min.
311        //      Change all vertices that have x != 0.
312        for(i=0;i<Ordenacion.size();i++)
313        {
314                if ((i % update) == 0)
315                {
316                        upb(percent);
317                }
318
319                Ord     =       &(Ordenacion[i]);
320
321                if (Ord->x!=0)
322                {
323                        //      Change the simplification order.
324                        v                                                               =       Ord->vQslim;
325                        Ord->vQslim                     =       Ord->vQslimNext;
326                        Ord->vQslimNext =       v;
327
328                        for (j = i+1; j < Ordenacion.size(); j++)
329                        {
330                                if (Ordenacion[j].vQslim == Ord->vQslim)
331                                {
332                                        Ordenacion[j].vQslim    =       Ord->vQslimNext;
333                                        Ordenacion[j].cambio    =       "y";
334                                }
335                                else if (Ordenacion[j].vQslimNext == Ord->vQslim)
336                                {
337                                        Ordenacion[j].vQslimNext        =       Ord->vQslimNext;       
338                                        Ordenacion[j].cambio                    =       "y";
339                                }
340                        }
341                }
342        }
343
344        update  =       long(Ordenacion.size() / 20);
345        percent =       0.5;
346
347        //      3 seg.
348        //Ordenacion de los vertices
349        for(i = 0; i < Ordenacion.size(); i++)
350        {
351                if ((i % update) == 0)
352                {
353                        upb(percent);
354                }
355
356                c1++;
357                NuevosVerts.push_back(this->cVerts[Ordenacion[i].vQslim]);
358
359                cVerts[Ordenacion[i].vQslim].Next       =       -1;
360
361        }
362
363        //      Añadir aquellos vertices que no estaban en NuevosVerts
364        //      y no se simplificaban.
365        int cont                                =       0;
366        int contestaban =       0;
367
368        for(i = 0; i < this->cVerts.size(); i++)
369        {
370                if (this->cVerts[i].Next != -1)
371                {
372                        cont++;
373                        NuevosVerts.push_back(this->cVerts[i]);
374                        Orden.vLS                                       =       int(Ordenacion.size());
375                        Orden.obligatorio       = 0;
376                        Orden.vLSNext                   =       -1;
377                        Orden.vQslim                    =       i;
378                        Orden.vQslimNext        =       -1;
379
380                        Ordenacion.push_back(Orden);
381                }
382                else
383                {
384                        contestaban++;
385                }
386        }
387
388        update  =       long(Ordenacion.size() / 20);
389        percent =       0.5;
390       
391        //      2 min.
392        //      Calcular los siguiente teniendo en cuenta el QSLIM.
393        for (i = 0; i < Ordenacion.size(); i++)
394        {
395                if ((i % update) == 0)
396                {
397                        upb(percent);
398                }
399
400                v       =       Ordenacion[i].vQslimNext;
401
402                //Buscar ese vertice v cual es ahora
403                vSig    =       0;
404
405                for (t = 0; t < Ordenacion.size() && !vSig;t++)
406                {
407                        if (Ordenacion[t].vQslim==v)
408                        {
409                                vSig=Ordenacion[t].vLS;
410                        }
411                }
412
413                if (vSig!=0)
414                {
415                        Ordenacion[i].vLSNext   =       vSig;
416                }
417        }
418
419        //Creacion de nuevas tiras con las mismas dimensiones
420        for(t = 0; t < this->cStrips.size(); t++)
421        {
422                tira.clear();
423
424                for(i = 0; i < this->cStrips[t].size(); i++)
425                {
426                        tira.push_back(0);
427                }
428
429                NuevasTiras.push_back(tira);
430        }
431
432        update  =       long(NuevosVerts.size() / 40);
433        percent =       0.5;
434
435        k       =       0;
436
437        //      2 min.
438        //Cambiar los vertices de las tiras
439        for(v = 0; v < NuevosVerts.size(); v++)
440        {
441                if ((v % update) == 0)
442                {
443                        upb(percent);
444                }
445
446                for(t = 0; t < this->cStrips.size(); t++)
447                {
448                        for(i = 0; i < this->cStrips[t].size(); i++)
449                        {
450                                if (this->cStrips[t][i] == Ordenacion[v].vQslim)
451                                {
452                                        k++;
453                                        NuevasTiras[t][i]       =       v;
454                                }
455                        }
456                }
457        }
458
459        //Cambiar a Tiras Nuevas
460        this->cStrips   =       NuevasTiras;
461
462        //NuevosVerts y Ordenacion = tamaño.
463        for(i = 0; i < NuevosVerts.size(); i++)
464        {
465                NuevosVerts[i].Next     =       Ordenacion[i].vLSNext;
466        }
467
468        //Cambiar cVerts por nuevosVerts.
469        this->cVerts    =       NuevosVerts;
470
471        percent =       10;
472        upb(percent);
473}               
474
475//-----------------------------------------------------------------------------
476//      Constructor of LodStripsConstructor object from
477//      a Mesh and MeshSimplificationSequence
478//      Constructor del objeto LodStripsConstructor.
479//      Tomamos el Mesh y la secuencia de simplificacion que se pasan
480//      como parametros y llamamos a la funcion GenerarModeloCompleto para
481//      que se realicen las modificaciones a efectuar
482//-----------------------------------------------------------------------------
483LodStripsConstructor::LodStripsConstructor(const Mesh                                           *m,
484                                                                                                                const MeshSimplificationSequence        *ms,
485                                                                                                                        size_t                                                                                                          submesh,
486                                                                                                                        TIPOFUNC                                                                                                        upb)
487{
488        MARCA   =       TOTALTIRAS      =       TOTALVERTS      =       TOTALCAMBIOS    =       0;
489       
490        meshoriginal            =       m;
491        this->geoMesh           =       new Geometry::Mesh();
492        *this->geoMesh  =       *m;
493        this->geoMeshSQ = ms;
494
495        //      Set the leaves submesh.
496        mSubMeshLeaves  =       submesh;
497
498       
499        this->GenerarModeloCompleto(upb);
500}
501
502//---------------------------------------------
503//      Destructor LodStripsConstructor object
504//      Destructor de la clase LodStripsConstructor
505//---------------------------------------------
506LodStripsConstructor::~LodStripsConstructor()
507{
508        delete [] vDatos;
509        delete [] vCambios;
510        vector <LODData> cCambiosTemp;
511        cCambios.swap(cCambiosTemp);
512
513        VECTORUNINT cDatosTemp;
514        cDatos.swap(cDatosTemp);
515
516        delete [] vStrips;
517
518        delete [] vVerts;
519
520        vector <VECTORINT> cStripsTemp;
521        cStrips.swap(cStripsTemp);;
522
523        VECTORVERTEX cVertsTemp;
524        cVerts.swap(cVertsTemp);
525
526        delete [] pCambios;
527
528        delete this->geoMesh;
529}
530
531//------------------------------------------------------------------------------
532//      build lod file with necessary information and save all changes at
533//      lodStripConstructor object
534//      Construimos el archivo Lod donde se guarda la informacion sobre los
535//      cambios realizados en el mesh
536//      y copiamos todos los cambios realizados sobre las estructuras que
537//      contienen los vertices y los indices
538//      modificados sobre el objeto Mesh de la clase
539//------------------------------------------------------------------------------
540void LodStripsConstructor::Save(Serializer &oSerializer)
541{
542        unsigned int    i, j;
543        int k=0;
544        char    simp[256];
545
546        //******************  1º CONSTRUCCION DEL FICHERO.LOD *********************
547
548        //      VERTICES.
549        for(i = 0; i < this->cVerts.size(); i++)
550        {
551                sprintf(simp,"v %d\n",cVerts[i].Next);
552                oSerializer.WriteData(simp,sizeof(char),strlen(simp));
553        }
554
555        //      CSTRIPS.
556        this->TOTALINDICES      =       0;
557       
558        for(i = 0; i < this->cStrips.size(); i++)
559        {
560                this->TOTALINDICES += int(this->cStrips[i].size());
561        }
562
563        //      lines starting with a d.
564        //      Changes
565        for(i = 0; i < this->cCambios.size(); i++)
566        {
567                sprintf(simp,"c\n");
568                oSerializer.WriteData(simp,sizeof(char),strlen(simp));
569               
570                sprintf(simp,
571                                                "d %u %u %u %u %u\n",
572                                                cCambios[i].strip,
573                                                cCambios[i].nP,
574                                                cCambios[i].nL1,
575                                                cCambios[i].nL2,
576                                                cCambios[i].obligatory);
577               
578                oSerializer.WriteData(simp,sizeof(char),strlen(simp));
579        }
580
581        //      Lines starting with a b.
582        //      Data.
583        sprintf(simp,"a\n");
584        oSerializer.WriteData(simp,sizeof(char),strlen(simp));
585       
586        for(i = 0; i < this->cDatos.size(); i++)
587        {
588                sprintf(simp,"b %d\n",cDatos[i]);
589
590                oSerializer.WriteData(simp,sizeof(char),strlen(simp));
591        }
592
593        //      lines starting with a p.
594        //      Chages made in a LOD.
595        for(i = 0; i < this->TOTALCAMBIOS; i++)
596        {
597                sprintf(simp,"p %d\n",this->pCambios[i]);
598
599                oSerializer.WriteData(simp,sizeof(char),strlen(simp));
600        }
601
602        oSerializer.WriteData("\n",sizeof(char),1);
603
604        //*******************  2º DEVOLVER EL MESH CON LAS MODIFICACIONES EFECTUADAS  *********************
605        int num_no      =       0;
606
607        if(this->geoMesh->mSubMesh[0].mSharedVertexBuffer == true)
608        {
609                //mPosition
610                delete[] this->geoMesh->mVertexBuffer->mPosition;
611                this->geoMesh->mVertexBuffer->mPosition = new Vector3[this->cVerts.size()];
612
613                //mNormal
614                if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
615                {
616                        delete[] this->geoMesh->mVertexBuffer->mNormal;
617                        this->geoMesh->mVertexBuffer->mNormal = new Vector3[this->cVerts.size()];
618                }
619
620                //mTexCoords
621                if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
622                {
623                        delete[] this->geoMesh->mVertexBuffer->mTexCoords;
624                        this->geoMesh->mVertexBuffer->mTexCoords = new Vector2[this->cVerts.size()];
625                }
626
627                this->geoMesh->mVertexBuffer->mVertexCount      =       this->cVerts.size();
628                this->geoMesh->mVertexBuffer->mVertexInfo               =       this->meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo;
629
630                for(i = 0; i < this->geoMesh->mSubMeshCount; i++)
631                {
632                        this->geoMesh->mSubMesh[i].mVertexBuffer                                =       this->geoMesh->mVertexBuffer;
633                        this->geoMesh->mSubMesh[i].mSharedVertexBuffer  =       true;
634                }
635
636                int     indice  =       0;
637
638                for (j = 0; j < this->cVerts.size(); j++)
639                {
640                        //Copiamos las coordeandas de posicion
641                        this->geoMesh->mVertexBuffer->mPosition[indice].x       =       this->cVerts[j].Vertex[0];
642                        this->geoMesh->mVertexBuffer->mPosition[indice].y       =       this->cVerts[j].Vertex[1];
643                        this->geoMesh->mVertexBuffer->mPosition[indice].z       =       this->cVerts[j].Vertex[2];
644
645                        //Copiamos las normales
646                        if (meshoriginal->mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
647                        {
648                                this->geoMesh->mVertexBuffer->mNormal[indice].x =       this->cVerts[j].Normal[0];
649                                this->geoMesh->mVertexBuffer->mNormal[indice].y =       this->cVerts[j].Normal[1];
650                                this->geoMesh->mVertexBuffer->mNormal[indice].z =       this->cVerts[j].Normal[2];
651                        }
652
653                        //Copiamos las coordenadas de textura
654                        if (meshoriginal->mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
655                        {
656                                this->geoMesh->mVertexBuffer->mTexCoords[indice].x      =       this->cVerts[j].TexCoords[0];
657                                this->geoMesh->mVertexBuffer->mTexCoords[indice].y      =       this->cVerts[j].TexCoords[1];
658                        }
659
660                        indice++;
661
662                }
663
664                this->geoMesh->mVertexBuffer->mVertexCount      =       indice;
665        }
666        else    //OSCAR
667        {
668                //Supondre que solo tengo un submesh con vertices no compartidos
669                //mPosition
670                delete[]        this->geoMesh->mSubMesh[0].mVertexBuffer->mPosition;
671               
672                this->geoMesh->mSubMesh[0].mVertexBuffer->mPosition     =       new Vector3[this->cVerts.size()];
673
674                //mNormal
675                if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
676                {
677                        delete[] this->geoMesh->mSubMesh[0].mVertexBuffer->mNormal;
678                        this->geoMesh->mSubMesh[0].mVertexBuffer->mNormal = new Vector3[this->cVerts.size()];
679                }
680
681                //mTexCoords
682                if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
683                {
684                        delete[] this->geoMesh->mSubMesh[0].mVertexBuffer->mTexCoords;
685                        this->geoMesh->mSubMesh[0].mVertexBuffer->mTexCoords = new Vector2[this->cVerts.size()];
686                }
687
688                this->geoMesh->mSubMesh[0].mVertexBuffer->mVertexCount =this->cVerts.size();
689                this->geoMesh->mSubMesh[0].mVertexBuffer->mVertexInfo = this->meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo;
690
691                this->geoMesh->mSubMesh[0].mSharedVertexBuffer=false;
692
693                int indice=0;
694                for (j=0; j<this->cVerts.size(); j++)
695                {
696                        //Copiamos las coordeandas de posicion
697                        this->geoMesh->mSubMesh[0].mVertexBuffer->mPosition[indice].x=this->cVerts[j].Vertex[0];
698                        this->geoMesh->mSubMesh[0].mVertexBuffer->mPosition[indice].y=this->cVerts[j].Vertex[1];
699                        this->geoMesh->mSubMesh[0].mVertexBuffer->mPosition[indice].z=this->cVerts[j].Vertex[2];
700
701                        //Copiamos las normales
702                        if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
703                        {
704                                this->geoMesh->mSubMesh[0].mVertexBuffer->mNormal[indice].x=this->cVerts[j].Normal[0];
705                                this->geoMesh->mSubMesh[0].mVertexBuffer->mNormal[indice].y=this->cVerts[j].Normal[1];
706                                this->geoMesh->mSubMesh[0].mVertexBuffer->mNormal[indice].z=this->cVerts[j].Normal[2];
707                        }
708
709                        //Copiamos las coordenadas de textura
710                        if (meshoriginal->mSubMesh[0].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
711                        {
712                                this->geoMesh->mSubMesh[0].mVertexBuffer->mTexCoords[indice].x=this->cVerts[j].TexCoords[0];
713                                this->geoMesh->mSubMesh[0].mVertexBuffer->mTexCoords[indice].y=this->cVerts[j].TexCoords[1];
714                        }
715                        indice++;
716                }
717                this->geoMesh->mSubMesh[0].mVertexBuffer->mVertexCount =indice;
718        }
719       
720        int indice_acum = 0;
721        for(i=0;i<this->geoMesh->mSubMeshCount;i++)
722        {
723                for (size_t ind=0; ind < meshoriginal->mSubMesh[i].mIndexCount; ind++)
724                {
725                        if (meshoriginal->mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
726                        {
727                                //Hay coordenadas de textura y normales
728                                if(meshoriginal->mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
729                                {
730                                        for (size_t indvo=0; indvo<this->geoMesh->mSubMesh[i].mVertexBuffer->mVertexCount; indvo++)
731                                        {
732                                                if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mPosition[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mPosition[indvo]))
733                                                {
734                                                        if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mNormal[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mNormal[indvo]))
735                                                        {
736                                                                if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mTexCoords[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[indvo]))
737                                                                {
738                                                                        this->geoMesh->mSubMesh[i].mIndex[ind]= int(indvo);//+indice_acum;
739                                                                }
740                                                        }
741                                                }
742                                        }
743                                }
744                                else            //Hay coordenadas de textura pero no hay normales
745                                {
746                                        for (size_t indvo=0; indvo<this->geoMesh->mSubMesh[i].mVertexBuffer->mVertexCount; indvo++)
747                                        {
748                                                if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mPosition[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mPosition[indvo]))
749                                                {
750                                                        if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mTexCoords[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[indvo]))
751                                                        {
752                                                                this->geoMesh->mSubMesh[i].mIndex[ind]=int(indvo);//+indice_acum;
753                                                        }
754                                                }
755                                        }
756                                }
757                        }
758                        else
759                        {
760                                // No hay coordenadas de textura pero si normales
761                                if(meshoriginal->mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
762                                {
763                                        for (size_t indvo=0; indvo<this->geoMesh->mSubMesh[i].mVertexBuffer->mVertexCount; indvo++)
764                                        {
765                                                if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mPosition[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mPosition[indvo]))
766                                                {
767                                                        if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mNormal[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mNormal[indvo]))
768                                                        {
769                                                                this->geoMesh->mSubMesh[i].mIndex[ind]=int(indvo);//+indice_acum;
770                                                        }
771                                                }
772                                        }
773                                }
774                                else            //No hay coordenadas de texturas ni normales
775                                {
776                                        for (size_t indvo=0; indvo<this->geoMesh->mSubMesh[i].mVertexBuffer->mVertexCount; indvo++)
777                                        {
778                                                if (igual(meshoriginal->mSubMesh[i].mVertexBuffer->mPosition[meshoriginal->mSubMesh[i].mIndex[ind]],this->geoMesh->mSubMesh[i].mVertexBuffer->mPosition[indvo]))
779                                                {
780                                                        this->geoMesh->mSubMesh[i].mIndex[ind]=int(indvo);//+indice_acum;
781                                                }
782                                        }
783                                }
784                        }
785                }
786        }
787
788}
789
790int LodStripsConstructor::igual(Geometry::Vector3 vo,Geometry::Vector3 vc)
791{
792        if (vo.x==vc.x && vo.y==vc.y && vo.z==vc.z)
793                return 1;
794        else
795                return 0;
796}
797
798int LodStripsConstructor::igual(Geometry::Vector2 vo,Geometry::Vector2 vc)
799{
800        if (vo.x==vc.x && vo.y==vc.y)
801                return 1;
802        else
803                return 0;
804}
805
806void LodStripsConstructor::Load(Serializer &oSerializer)
807{
808}
809
810//-----------------------------------------------------------------------------------------------
811//Call leeVerticesyTirasDeMesh, OrdenarModeloVQSLIM and CalcularCambiosLODsVNuevaED to make changes
812//at vertex and index strutures
813//Llama a los metodos que realizan los cambios sobre las estructuras que contienen la informacion
814//de los vertices y los indices
815//-----------------------------------------------------------------------------------------------
816void LodStripsConstructor::GenerarModeloV(TIPOFUNC      upb)
817{
818        long int        max;
819        long int        t;
820        unsigned long int       i;
821        float                   percent;
822
823        percent =       10;
824
825        this->leeVerticesyTirasDeMesh();
826
827        upb(percent);
828
829        //      6 min.
830        this->OrdenarModeloVQSLIM(upb);
831
832        //Calcular el tamaño maximo de tira
833        max     =       0;
834
835        for(i = 0; i < this->TOTALTIRAS; i++)
836        {
837                t       =       long(this->cStrips[i].size());
838               
839                if (t > max)
840                {
841                        max     =       t;
842                }
843        }
844
845        this->MARCA     =       max + 1;
846
847        //      5 min.
848        this->CalcularCambiosLODsVNuevaED(upb);
849
850}
851
852//-----------------------------------------------------------------------------------------
853//It fills structs with vertex and index information
854//Rellena estructuras de datos que contienen la informacion de vertices y tiras modificadas
855//-----------------------------------------------------------------------------------------
856void LodStripsConstructor::CopiarVectors2ArraysNUEVAED()
857{
858        unsigned int            i;
859        unsigned int            j;
860        uint32                  max;
861        uint32                  t;
862        VECTORINT               tira;
863        LODChanges      listaCambios;
864
865        //      ALLOCATE----------------
866        this->TOTALVERTS        =       int(this->cVerts.size());
867        this->TOTALTIRAS        =       int(this->cStrips.size());
868        this->vVerts                    =       new tipoVertice[this->TOTALVERTS];
869        this->lStripsV          =       new VECTORINT[this->TOTALTIRAS];
870        this->vDatos                    =       new uint32[cDatos.size()];
871       
872        //      Calcular el tamaño maximo de tira y el numero de caras total.
873        max                                                             =       0;
874        this->TOTALCARAS        =       0;
875
876        for     (i = 0; i < this->cStrips.size(); i++)
877        {
878                t       =       int(this->cStrips[i].size());
879               
880                if (t > max)
881                {
882                        max     =       t;
883                }
884
885                this->TOTALCARAS        +=      (t-2);
886        }
887       
888        this->vStrips   =       Allocate2DArrayINT(this->TOTALTIRAS,max+1);
889        this->MARCA             =       int(this->cVerts.size());
890        vCambios                        =       new LODData[this->cCambios.size()];
891       
892        //      Rellenar vChanges.
893        for     (i = 0; i < this->cCambios.size();++i)
894        {
895                vCambios[i]     =       cCambios[i];
896        }
897       
898        for (i = 0; i < this->cStrips.size(); i++)
899        {
900                for (j = 0; j < this->cStrips[i].size(); j++)
901                {
902                        //      Rellenar el array vStrips.
903                        this->vStrips[i][j]     = this->cStrips[i][j];
904                       
905                        //      Rellenar el vVector.
906                        this->lStripsV[i].push_back(this->cStrips[i][j]);
907                }
908               
909                this->vStrips[i][j]     =       this->MARCA;
910        }
911
912        for (i = 0; i < cDatos.size(); i++)
913        {
914                vDatos[i]       =       cDatos[i];
915        }
916       
917        pCurrentData    =       vDatos;
918
919        //      Meter los vertices en vVerts.
920        for     (i = 0; i < this->cVerts.size(); i++)
921        {
922                this->vVerts[i] =       this->cVerts[i];
923        }
924
925}
926
927//---------------------------------------------------------------------------------------------------
928//It fills cVerts and cStrips with original Mesh vertex and strips information
929//Rellena las estructuras cVerts Y cStrips a partir de de los vertices y las tiras del mesh original
930//---------------------------------------------------------------------------------------------------
931void LodStripsConstructor::leeVerticesyTirasDeMesh()
932{
933        tipoVertice     vAux;
934        VECTORINT               tira;
935        size_t                  i;
936        size_t                  j;
937        bool                            error;
938
939        error   =       false;
940       
941        if(this->geoMesh->mSubMeshCount == 1)
942        {
943                error   =       false;
944        }
945        else
946        {
947                for     (i = 0; i < this->geoMesh->mSubMeshCount;i++)
948                {
949                        if      (this->geoMesh->mSubMesh[i].mSharedVertexBuffer == true)
950                        {
951                                error   =       false;
952                        }
953                        else
954                        {
955                                error   =       true;
956                                break;
957                        }
958                }
959        }
960       
961        if(!error)
962        {
963                if      (       (this->geoMesh->mSubMesh[0].mSharedVertexBuffer == true)
964                                        &&
965                                        (this->geoMesh->mSubMeshCount > 1)
966                                )
967                {
968                        for     (j = 0; j < this->geoMesh->mVertexBuffer->mVertexCount; j++)
969                        {
970                                //      Si hay mas de un submesh ListaIndices no se rellena correctamente
971                                vAux.Vertex[0] = this->geoMesh->mVertexBuffer->mPosition[j].x;
972                                vAux.Vertex[1] = this->geoMesh->mVertexBuffer->mPosition[j].y;
973                                vAux.Vertex[2] = this->geoMesh->mVertexBuffer->mPosition[j].z;
974
975                                //      Añadimos las normales en cVerts.
976                                if      (       this->geoMesh->mVertexBuffer->mVertexInfo
977                                                        &
978                                                        Geometry::VERTEX_NORMAL
979                                                )
980                                {
981                                        vAux.Normal[0] = this->geoMesh->mVertexBuffer->mNormal[j].x;
982                                        vAux.Normal[1] = this->geoMesh->mVertexBuffer->mNormal[j].y;
983                                        vAux.Normal[2] = this->geoMesh->mVertexBuffer->mNormal[j].z;
984                                }
985
986                                //      Añadimos las coordenadas de textura a cVerts.
987                                if      (       this->geoMesh->mVertexBuffer->mVertexInfo
988                                                        &
989                                                        Geometry::VERTEX_TEXCOORDS
990                                                )
991                                {
992                                        vAux.TexCoords[0] = this->geoMesh->mVertexBuffer->mTexCoords[j].x;
993                                        vAux.TexCoords[1] = this->geoMesh->mVertexBuffer->mTexCoords[j].y;
994                                }
995
996                                vAux.Next                       =       -10;
997                                vAux.numMesh    =       0;
998                       
999                                vAux.obligatorio        =       0;
1000                               
1001                                this->cVerts.push_back(vAux);
1002                        }
1003                }
1004                else
1005                {
1006                        for     (i = 0; i < this->geoMesh->mSubMeshCount; i++)
1007                        {
1008                                for     (       j = 0;
1009                                                j < this->geoMesh->mSubMesh[i].mVertexBuffer->mVertexCount;
1010                                                j++)
1011                                {
1012                                        //      Si hay mas de un submesh ListaIndices
1013                                        //      no se rellena correctamente.
1014                                        vAux.Vertex[0] = this->geoMesh->mSubMesh[i].
1015                                                mVertexBuffer->mPosition[j].x;
1016                                        vAux.Vertex[1] = this->geoMesh->mSubMesh[i].
1017                                                mVertexBuffer->mPosition[j].y;
1018                                        vAux.Vertex[2] = this->geoMesh->mSubMesh[i].
1019                                                mVertexBuffer->mPosition[j].z;
1020                                        //      Añadimos las normales en cVerts.
1021                                        vAux.Normal[0] = this->geoMesh->mSubMesh[i].
1022                                                mVertexBuffer->mNormal[j].x;
1023                                        vAux.Normal[1] = this->geoMesh->mSubMesh[i].
1024                                                mVertexBuffer->mNormal[j].y;
1025                                        vAux.Normal[2] = this->geoMesh->mSubMesh[i].
1026                                                mVertexBuffer->mNormal[j].z;
1027
1028                                        //      Añadimos las coordenadas de textura a cVerts.
1029                                        vAux.TexCoords[0]       =       this->geoMesh->mSubMesh[i].
1030                                                mVertexBuffer->mTexCoords[j].x;
1031                                        vAux.TexCoords[1] = this->geoMesh->mSubMesh[i].
1032                                                mVertexBuffer->mTexCoords[j].y;
1033
1034                                        vAux.Next                       =       -10;
1035                                        vAux.numMesh    =       int(i);
1036
1037                                        this->cVerts.push_back(vAux);
1038                                }
1039                        }
1040                }       
1041
1042                Index   indice_acum;
1043                Index   num_vert;
1044
1045                num_vert        =       0;
1046               
1047                for     (i = 0; i < this->geoMesh->mSubMeshCount; i++)
1048                {
1049                        //      If is not the leaves submesh.
1050                        if (mSubMeshLeaves != i)
1051                        {
1052                                indice_acum     =       0;
1053
1054                                for     (j = 0; j < this->geoMesh->mSubMesh[i].mStripCount; j++)
1055                                {
1056                                        Index   *aux    =       this->geoMesh->mSubMesh[i].mStrip[j];
1057
1058                                        /*
1059                                        if (j != 0)
1060                                        {
1061                                                // La primera tira no empieza por índices repetidos.
1062                                                aux     =       aux+1;
1063
1064                                                indice_acum++;
1065                                        }
1066                                        */
1067
1068                                        if (j+1 < this->geoMesh->mSubMesh[i].mStripCount)
1069                                        {
1070                                                Index   *siguiente      =       this->geoMesh->mSubMesh[i].mStrip[j+1];
1071
1072                                                tira.clear();
1073
1074                                                while (aux != siguiente)
1075                                                {
1076                                                        tira.push_back(*aux);
1077                                                        indice_acum++;
1078
1079                                                        aux     =       aux + 1;
1080                                                }
1081
1082                                                //tira.pop_back();
1083
1084                                                this->cStrips.push_back(tira);
1085                                        }
1086                                        else
1087                                        {
1088                                                // Insertar los indices de la última tira en el cStrips
1089                                                tira.clear();
1090
1091                                                for     (       unsigned int k = indice_acum;
1092                                                                k < this->geoMesh->mSubMesh[i].mIndexCount;
1093                                                                k++)
1094                                                {
1095                                                        tira.push_back(this->geoMesh->mSubMesh[i].mIndex[k]);
1096                                                }
1097
1098                                                this->cStrips.push_back(tira);
1099                                        }
1100                                }
1101
1102                                num_vert        +=      int(this->geoMesh->mSubMesh[i].
1103                                        mVertexBuffer->mVertexCount);
1104                        }
1105                }
1106
1107                this->TOTALVERTS        =       int(cVerts.size());
1108                this->TOTALTIRAS        =       int(cStrips.size());
1109        }
1110        else
1111        {
1112                //      Error.
1113        }
1114}
1115
1116///     GetMesh: Return de current Mesh.
1117Mesh    *       LodStripsConstructor::GetMesh()
1118{
1119        Mesh            *mesh_built;
1120
1121        mesh_built              =       new Mesh();
1122        *mesh_built             =       *geoMesh;
1123
1124        return  mesh_built;
1125}
1126
1127// Sets what is the submesh that stores the leaves.
1128void LodStripsConstructor::SetSubMeshLeaves(size_t      submesh)
1129{
1130        mSubMeshLeaves  =       submesh;
1131}
1132
Note: See TracBrowser for help on using the repository browser.