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

Revision 1025, 29.3 KB checked in by gumbau, 18 years ago (diff)

namespace simplif

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