source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp @ 1007

Revision 1007, 21.4 KB checked in by gumbau, 18 years ago (diff)
Line 
1#include "GeoMeshSimplifier.h"
2#include "GeoMeshSimpSequence.h"
3#include "SimplificationMethod.h"
4#include <iostream>
5#include <fstream>
6
7using namespace Geometry;
8using namespace std;
9
10////////////////////////////////////////////////////////////////////////////
11//                                                                                                                                                                                                                                                                                              //
12//                                                                                      MeshSimplifier class                                                                                                                    //
13//                                                                                                                                                                                                                                                                                              //
14////////////////////////////////////////////////////////////////////////////
15
16//---------------------------------------------------------------------------
17//      MeshSimplifier constructor.
18//---------------------------------------------------------------------------
19MeshSimplifier::MeshSimplifier( const Mesh      *m,
20                                                                                                                                TIPOFUNC                upb)
21{
22        objmesh                                 =       m;
23        mGeoMesh                                =       NULL;
24        msimpsequence           =       NULL;
25        indexMeshLeaves =       -1;
26
27        //      Sets the actual progress bar function.
28        mUPB    =       upb;
29}
30
31//---------------------------------------------------------------------------
32//      MeshSimplifier destroyer.
33//---------------------------------------------------------------------------
34MeshSimplifier::~MeshSimplifier()
35{
36        delete msimpsequence;
37}
38
39//---------------------------------------------------------------------------
40// Returns the simplified mesh.
41//---------------------------------------------------------------------------
42Mesh *  MeshSimplifier::GetMesh()
43{
44        return mGeoMesh;
45}
46
47//---------------------------------------------------------------------------
48//      Set submesh leaves
49//---------------------------------------------------------------------------
50void MeshSimplifier::setMeshLeaves(Index index)
51{
52        indexMeshLeaves =       index;
53}
54
55//---------------------------------------------------------------------------
56// Returns the simplification sequence for general meshes.
57//---------------------------------------------------------------------------
58MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence()
59{
60        return msimpsequence;
61}
62
63////////////////////////////////////////////////////////////////////////////
64//                                                                                                                                                                                                                                                                                              //
65//                                                                      GeometryBasedSimplifier class                                                                                                   //
66//                                                                                                                                                                                                                                                                                              //
67////////////////////////////////////////////////////////////////////////////
68
69//---------------------------------------------------------------------------
70//---------------------------------------------------------------------------
71GeometryBasedSimplifier::GeometryBasedSimplifier(       const Mesh      *m,
72                                                                                                                                                                                                        TIPOFUNC                upb)
73                                                                                                                                                                                                :MeshSimplifier(m,upb)
74{
75}
76
77//---------------------------------------------------------------------------
78//---------------------------------------------------------------------------
79GeometryBasedSimplifier::~GeometryBasedSimplifier()
80{
81}
82
83//---------------------------------------------------------------------------
84// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification.
85//---------------------------------------------------------------------------
86void GeometryBasedSimplifier::Simplify(Real paramlod)
87{
88        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh);
89       
90        m_qslim->setMeshLeaves(indexMeshLeaves);
91       
92        msimpsequence   =       m_qslim->Decimate(paramlod,0,mUPB);
93       
94        mGeoMesh        =       m_qslim->GetMesh();
95       
96        delete  m_qslim;
97}
98
99//---------------------------------------------------------------------------
100// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification.
101//---------------------------------------------------------------------------
102void GeometryBasedSimplifier::Simplify(uint32 numvertices)
103{
104        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh);
105       
106        m_qslim->setMeshLeaves(indexMeshLeaves);
107       
108        msimpsequence   =       m_qslim->Decimate((float)numvertices,1,mUPB);
109
110        mGeoMesh        =       m_qslim->GetMesh();
111       
112        delete m_qslim;
113}
114
115////////////////////////////////////////////////////////////////////////////
116//                                                                                                                                                                                                                                                                                              //
117//                                                              ViewPointDrivenSimplifier class                                                                                                 //
118//                                                                                                                                                                                                                                                                                              //
119////////////////////////////////////////////////////////////////////////////
120
121//---------------------------------------------------------------------------
122/// Class constructor. Will call Simplifier class constructor.
123//---------------------------------------------------------------------------
124ViewPointDrivenSimplifier::ViewPointDrivenSimplifier(   const Mesh      *m,
125                                                                                                                                                                                                                        TIPOFUNC                upb)
126                                                                                                                                                                                                                :MeshSimplifier(m,upb)
127{
128        //char s[MAX_CHAR];
129
130        //      Set progress update function
131        VMI::mUPB       =       upb;
132       
133        VMI::width  = 256;
134        VMI::height = 256;
135
136        VMI::bEnableOffScreen     = GL_TRUE;
137        VMI::bBeQuiet             = GL_FALSE;
138        VMI::bSaveLog             = GL_FALSE;
139        VMI::bLoadCamerasFromFile = GL_FALSE;
140
141        VMI::cameraType = 2;
142        VMI::radius = 1.3;
143        VMI::fov = 60.0;
144
145        printf("w: %d h: %d\n", VMI::width, VMI::height);
146
147        printf( "t: %d c: %d o: %d r: %f\n",
148                        VMI::numDemandedTriangles,
149                        VMI::cameraType,
150                        VMI::bEnableOffScreen,
151                        VMI::radius);
152
153
154        mGeoMesh        =       new Mesh();
155       
156        *mGeoMesh       =       *m;
157       
158        //      Transform NoSV Mesh to a SV Mesh.
159        mGeoMesh        =       mGeoMesh->toSharedVertex();
160
161        //      Loads the vmi mesh structure for a geometry mesh given.
162        VMI::mesh = initMeshStructure(mGeoMesh);
163       
164        //VMI::printMesh(mesh);
165        //getchar();
166
167        // Get a filename without extension
168        //strncpy(VMI::filename, argv[argc - 1], strlen(argv[argc - 1]) - 4);
169
170        //glutInit(&argc, argv);
171        // RGB and Alpha.
172        glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);
173        glutInitWindowSize(VMI::width, VMI::height);
174        glutInitWindowPosition(100, 100);
175
176        //#ifdef KL // Kullback-Leibler
177        //    sprintf(s, "VKL - [%s]", argv[argc-1]);
178        //#endif
179        //#ifdef MI // Mutual Information
180        //    sprintf(s, "VMI - [%s]", argv[argc-1]);
181        //#endif
182        //#ifdef HE // Hellinger
183        //    sprintf(s, "VHE - [%s]", argv[argc-1]);
184        //#endif
185        //#ifdef CS // Chi-Square
186        //    sprintf(s, "VCS - [%s]", argv[argc-1]);
187        //#endif
188
189        VMI::vmiWin     =       glutCreateWindow("VMI"/*s*/);
190
191        glewInit();
192
193        VMI::init();
194
195        if (VMI::bLoadCamerasFromFile == GL_FALSE)
196        {
197                switch (VMI::cameraType)
198                {
199                        case 0:
200                                VMI::cameras = VMI::setCameras(VMI::radius, OCTAHEDRON, &VMI::numCameras);
201                                printf("Number of cameras: %d\n", OCTAHEDRON);
202                                break;
203                        case 1:
204                                VMI:: cameras = VMI::setCameras(VMI::radius, ICOSAHEDRON, &VMI::numCameras);
205                                printf("Number of cameras: %d\n", ICOSAHEDRON);
206                                break;
207                        case 2:
208                                VMI::cameras = VMI::setCameras(VMI::radius, DODECAHEDRON, &VMI::numCameras);
209                                printf("Number of cameras: %d\n", DODECAHEDRON);
210                                break;
211                        default:
212                                break;
213                }
214        }
215        else
216        {
217                // Loads a cameras file
218                //sprintf(s,"%s.cam", VMI::filename);
219
220                //VMI::cameras = VMI::loadCameras(VMI::radius, s, &VMI::numCameras);
221                //getchar();
222        }
223
224        VMI::histogram  =       VMI::initHistogram(     VMI::mesh->currentNumTriangles,
225                                                                                                                                                                VMI::numCameras);
226
227        VMI::initialIs  =       VMI::initIs(VMI::numCameras);
228
229}
230
231//---------------------------------------------------------------------------
232/// Class destructor.
233//---------------------------------------------------------------------------
234ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void)
235{
236        // Free memory
237         VMI::freeMemory();
238}
239
240/// Copy constructor
241//ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&);
242
243/// Assignment operator
244//ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&);
245
246/// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification.
247void ViewPointDrivenSimplifier::Simplify(Real percent)
248{
249        // TODO: parametros libreria globales
250
251
252        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent);
253
254        if ((VMI::numDemandedTriangles == 0)
255                        ||
256                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles))
257        {
258                printf("Illegal number of triangles.\n");
259        }
260
261        VMI::display();
262
263        //      Load a geometry mesh for vmi mesh.
264        loadMesh();
265
266        GetMeshSimpSequence();
267}
268
269//---------------------------------------------------------------------------
270/// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification.
271//---------------------------------------------------------------------------
272void ViewPointDrivenSimplifier::Simplify(uint32 numVertices)
273{
274        float   percent;
275
276        percent =       (numVertices    *       100.0) / VMI::mesh->numVertices;
277
278        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100));
279
280        if ((VMI::numDemandedTriangles == 0)
281                        ||
282                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles))
283        {
284                printf("Illegal number of triangles.\n");
285        }
286
287        VMI::display();
288
289        //      Load a geometry mesh for vmi mesh.
290        loadMesh();
291
292        GetMeshSimpSequence();
293}
294
295// Returns the simplified mesh.
296//Mesh  *       ViewPointDrivenSimplifier::GetMesh()
297//{
298//      return mGeoMesh;
299//}
300
301//---------------------------------------------------------------------------
302//      Gets the VMI mesh simplification sequence.
303//---------------------------------------------------------------------------
304void    ViewPointDrivenSimplifier::GetMeshSimpSequence()
305{
306        unsigned        int j = 0;
307       
308        MeshSimplificationSequence::Step        current_step;
309       
310        msimpsequence   =       new MeshSimplificationSequence();
311       
312        //      Debug.
313        cout    <<      "GetMeshsimpSequence VMI"
314                                <<      endl;
315
316        //      For each simplification step.
317        for (unsigned   int i = 0;      i < VMI::mVMISteps.size();      i++)
318        {
319                current_step.mV0        =       VMI::mVMISteps[i].mV0;
320                current_step.mV1        =       VMI::mVMISteps[i].mV1;
321                current_step.mT0        =       VMI::mVMISteps[i].mT0;
322                current_step.mT1        =       VMI::mVMISteps[i].mT1;
323               
324                current_step.x  =       VMI::mVMISteps[i].x;
325                current_step.y  =       VMI::mVMISteps[i].y;
326                current_step.z  =       VMI::mVMISteps[i].z;
327
328                current_step.obligatorio        =       VMI::mVMISteps[i].obligatory;
329
330                //      For each face modificated.
331                while (j < VMI::mVMISteps[i].mModfaces.size())
332                {
333                        current_step.mModfaces.push_back(VMI::mVMISteps[i].mModfaces[j]);
334                       
335                        j++;
336                }
337               
338                //      Debug.
339                cout    <<      "Step "
340                                        <<      i
341                                        <<      " added."
342                                        <<      endl;
343               
344                msimpsequence->mSteps.push_back(current_step);
345        }
346}
347
348//---------------------------------------------------------------------------
349//      Init vmi mesh structure for a geometry mesh given.
350//---------------------------------------------------------------------------
351VMI::Mesh *     ViewPointDrivenSimplifier::initMeshStructure(Mesh       *geoMesh)
352{
353        GLuint                          i, j, v1, v2, v3, n, e, t;
354        VMI::Mesh               *vmi_mesh;
355        SubMesh                         *geosubmesh;
356        VertexBuffer    *vertex_buffer;
357        int                                             mesh_index_count;
358        int                                             index;
359
360        //      Reallocate memory for vmi mesh object.
361        vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh));
362
363        if (vmi_mesh == NULL)
364        {
365                fprintf(stderr, "Error allocating memory\n");
366                exit(1);
367        }
368
369        //      Shared vertex buffer.
370        vertex_buffer   =       geoMesh->mVertexBuffer;
371       
372        //      Reallocate memory for vertices of mesh.
373        vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex)
374                                                                                                                                                                *
375                                                                                                                                                                vertex_buffer->mVertexCount);
376
377        if (vmi_mesh->vertices == NULL)
378        {
379                fprintf(stderr, "Error allocating memory\n");
380                exit(1);
381        }
382
383        //      Initialize mesh index count.
384        mesh_index_count        =       0;
385       
386        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
387        {
388                mesh_index_count        +=      int(mGeoMesh->mSubMesh[submesh].mIndexCount);
389        }
390       
391        //      Reallocate memory for indices.
392        vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle)
393                                                                                                                                                                                *
394                                                                                                                                                                                (mesh_index_count / 3));
395
396        if (vmi_mesh->triangles == NULL)
397        {
398                fprintf(stderr, "Error allocating memory\n");
399                exit(1);
400        }
401
402        printf("Adding vertices...");
403       
404        //      Fill up vertices.
405        for (i = 0; i < vertex_buffer->mVertexCount; i++)
406        {
407                // Vertices start at 0.
408                vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x;
409                vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y;
410                vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z;
411               
412                vmi_mesh->vertices[i].numTriangles = 0;
413                vmi_mesh->vertices[i].triangles = NULL;
414                vmi_mesh->vertices[i].enable = GL_TRUE;
415        }
416       
417        printf("Ok\n");
418        vmi_mesh->numVertices = int(vertex_buffer->mVertexCount);
419        vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount);
420
421        //      Fill up triangles.
422        printf("Adding triangles...");
423       
424        //      Initialize index of triangle.
425        index   =       0;
426       
427        //      For each submesh.
428        for (unsigned int submesh       =       0; submesh      <       mGeoMesh->mSubMeshCount; submesh++)
429        {
430                //      Gets actual submesh.
431                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
432               
433                for (i = 0; i < (geosubmesh->mIndexCount / 3); i++)
434                {
435                        vmi_mesh->triangles[index].id                           = index;
436                        vmi_mesh->triangles[index].submesh              = submesh;
437                        vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)];
438                        vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1];
439                        vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2];
440
441                        vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices,
442                                        &vmi_mesh->triangles[index]);
443                        //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area);
444                        computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]);
445
446                        vmi_mesh->triangles[index].saliency = 0.0;
447
448                        vmi_mesh->triangles[index].enable = GL_TRUE;
449
450                        for (j = 0; j < 3; j++)
451                        {
452                                // Adding triangle index adjacent to 3 vertices
453                                v1 = vmi_mesh->triangles[index].indices[j];
454
455                                // Reallocate memory for the new adjacent triangle
456                                vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint));
457                                VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index);
458                        }
459
460                        //      Increments triangle count.
461                        index++;
462                }
463        }
464       
465        //printf("\n");
466        printf("Ok\n");
467
468        vmi_mesh->numTriangles                          = mesh_index_count / 3;
469        vmi_mesh->currentNumTriangles = mesh_index_count / 3;
470
471        printf("Num Triangles: %d\n",vmi_mesh->numTriangles);
472       
473        // E = 3 T / 2
474        vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge)
475                                                                                                                                                *
476                                                                                                                                                vmi_mesh->numTriangles * 3);
477
478        if (vmi_mesh->edges == NULL)
479        {
480                fprintf(stderr, "Error allocating memory\n");
481                exit(1);
482        }
483
484        // Init edges
485        for (i=0; i<vmi_mesh->numTriangles * 3; i++)
486        {
487                vmi_mesh->edges[i].triangles    = NULL;
488                vmi_mesh->edges[i].numTriangles = 0;
489        }
490
491        printf("Adding edges...");
492        n = 0;
493
494        for (i=0; i<vmi_mesh->numTriangles; i++)
495        {
496                t               =       0;
497                v1      =       vmi_mesh->triangles[i].indices[0];
498                v2      =       vmi_mesh->triangles[i].indices[1];
499                v3      =       vmi_mesh->triangles[i].indices[2];
500
501                if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1)
502                {
503                        vmi_mesh->edges[n].u                    =       v1;
504                        vmi_mesh->edges[n].v                    =       v2;
505                        vmi_mesh->edges[n].enable =     GL_TRUE;
506
507                        // Reallocate memory for the new adjacent triangle
508                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
509                        // Adding triangle i adjacent to edge n
510                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
511                        //printf("n:%d i:%d\n", n, i);
512
513                        // Adding edge n adjacent to triangle i
514                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
515                        n++;
516                }
517                else
518                {
519                        // Reallocate memory for the new adjacent triangle.
520                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
521                        // Adding triangle i adjacent to edge e
522                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
523                        //printf("n:%d i:%d\n", e, i);
524
525                        // Adding edge e adjacent to triangle i
526                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
527                }
528                if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1)
529                {
530                        vmi_mesh->edges[n].u = v2;
531                        vmi_mesh->edges[n].v = v3;
532                        vmi_mesh->edges[n].enable = GL_TRUE;
533
534                        // Reallocate memory for the new adjacent triangle
535                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
536                        // Adding triangle i adjacent to edge n
537                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
538                        //printf("n:%d i:%d\n", n, i);
539
540                        // Adding edge n adjacent to triangle i
541                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
542                        n++;
543                }
544                else
545                {
546                        // Reallocate memory for the new adjacent triangle
547                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
548                        // Adding triangle i adjacent to edge e
549                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
550                        //printf("n:%d i:%d\n", e, i);
551
552                        // Adding edge e adjacent to triangle i
553                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
554                }
555               
556                if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1)
557                {
558                        vmi_mesh->edges[n].u = v3;
559                        vmi_mesh->edges[n].v = v1;
560                        vmi_mesh->edges[n].enable = GL_TRUE;
561
562                        // Reallocate memory for the new adjacent triangle
563                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
564                        // Adding triangle i adjacent to edge n
565                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
566                        //printf("n:%d i:%d\n", n, i);
567
568                        // Adding edge n adjacent to triangle i
569                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
570                        n++;
571                }
572                else
573                {
574                        // Reallocate memory for the new adjacent triangle
575                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
576                        // Adding triangle i adjacent to edge e
577                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
578                        //printf("n:%d i:%d\n", e, i);
579
580                        // Adding edge e adjacent to triangle i
581                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
582                }
583        }
584       
585        printf("Ok\n");
586        vmi_mesh->numEdges = n;
587
588        return vmi_mesh;
589}
590
591//---------------------------------------------------------------------------
592//      Gets the geometry mesh of a  the vmi mesh given.
593//---------------------------------------------------------------------------
594void    ViewPointDrivenSimplifier::loadMesh()
595{
596        int                                             num_indices;
597        SubMesh                         *geosubmesh;
598        VertexBuffer    *vertex_buffer;
599       
600        //      Gets old vertex buffer.
601        vertex_buffer   =       mGeoMesh->mVertexBuffer;
602       
603        //      Initialize auxiliar vertex buffer.
604        mVB     =       new     VertexBuffer();
605       
606        mVB->mPosition          =       new     Vector3[VMI::mesh->currentNumVertices];
607        mVB->mNormal                    =       new     Vector3[VMI::mesh->currentNumVertices];
608        mVB->mTexCoords         =       new     Vector2[VMI::mesh->currentNumVertices];
609
610        mVB->mVertexInfo        =       vertex_buffer->mVertexInfo;
611
612        //      For each submesh.
613        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
614        {
615                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
616
617                delete  []geosubmesh->mIndex;
618
619                //      Initialize submesh index count;
620                num_indices     =       0;
621
622                //      For each triangle.
623                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
624                {
625                        //      If is enable and of the current submesh.
626                        if ((VMI::mesh->triangles[i].enable)
627                                        &&
628                                        (VMI::mesh->triangles[i].submesh == submesh))
629                        {
630                                //      Increments submesh index count.
631                                num_indices     +=      3;
632                        }
633                }
634
635                geosubmesh->mIndexCount =       num_indices;
636
637                geosubmesh->mIndex      =       new Index[geosubmesh->mIndexCount];
638
639                //      Initialize number of indices.
640                num_indices     =       0;
641
642                //      Fill up indices.
643                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
644                {
645                        if ((VMI::mesh->triangles[i].enable)
646                                        &&
647                                        (VMI::mesh->triangles[i].submesh == submesh))
648                        {
649                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[0];
650                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[1];
651                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[2];
652                        }
653                }
654
655                //      For each index.
656                for (unsigned int i = 0; i < geosubmesh->mIndexCount; i++)
657                {
658                        findVertex(submesh,i);
659                }
660
661                geosubmesh->mVertexBuffer       =       mVB;
662        }
663
664        delete  vertex_buffer;
665       
666        mGeoMesh->mVertexBuffer =       mVB;
667
668}
669
670//---------------------------------------------------------------------------
671//      Find vertex in auxiliar vertex buffer.
672//---------------------------------------------------------------------------
673void    ViewPointDrivenSimplifier::findVertex(size_t    submesh, size_t elem)
674{
675        bool                                    found;
676        int                                             index;
677        unsigned int                    i;
678        int                                             new_elem;
679        VertexBuffer    *vertex_buffer;
680
681        found   =       false;
682
683        //      Shared vertex buffer.
684        vertex_buffer   =       mGeoMesh->mVertexBuffer;
685       
686        index   =       mGeoMesh->mSubMesh[submesh].mIndex[elem];
687       
688        i       =       0;
689       
690        while (!found && (i < mVB->mVertexCount))
691        {
692                if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x)
693                                &&
694                                (VMI::mesh->vertices[index].y == mVB->mPosition[i].y)
695                                &&
696                                (VMI::mesh->vertices[index].z == mVB->mPosition[i].z))
697                {
698                        found   =       true;
699
700                        //      Update index.
701                        mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        i;
702                }
703
704                //      Increments index.
705                i++;
706        }
707
708        if (!found)
709        {
710                //      Last element.
711                new_elem        =       int(mVB->mVertexCount);
712               
713                //      Add to last.
714                mVB->mPosition[new_elem].x      =       VMI::mesh->vertices[index].x;
715                mVB->mPosition[new_elem].y      =       VMI::mesh->vertices[index].y;
716                mVB->mPosition[new_elem].z      =       VMI::mesh->vertices[index].z;
717
718                mVB->mNormal[new_elem].x        =       vertex_buffer->mNormal[index].x;
719                mVB->mNormal[new_elem].y        =       vertex_buffer->mNormal[index].y;
720                mVB->mNormal[new_elem].z        =       vertex_buffer->mNormal[index].z;
721
722                mVB->mTexCoords[new_elem].x     =       vertex_buffer->mTexCoords[index].x;
723                mVB->mTexCoords[new_elem].y     =       vertex_buffer->mTexCoords[index].y;
724
725                //      Update index.
726                mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        new_elem;
727
728                //      Increments vertex count.
729                mVB->mVertexCount++;
730        }
731}
732
Note: See TracBrowser for help on using the repository browser.