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

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