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

Revision 1048, 29.9 KB checked in by gumbau, 18 years ago (diff)

Fixed culling bug

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