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

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