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

Revision 1009, 21.3 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        //      For each simplification step.
313        for (unsigned   int i = 0;      i < VMI::mVMISteps.size();      i++)
314        {
315                current_step.mV0        =       VMI::mVMISteps[i].mV0;
316                current_step.mV1        =       VMI::mVMISteps[i].mV1;
317                current_step.mT0        =       VMI::mVMISteps[i].mT0;
318                current_step.mT1        =       VMI::mVMISteps[i].mT1;
319               
320                current_step.x  =       VMI::mVMISteps[i].x;
321                current_step.y  =       VMI::mVMISteps[i].y;
322                current_step.z  =       VMI::mVMISteps[i].z;
323
324                current_step.obligatorio        =       VMI::mVMISteps[i].obligatory;
325
326                //      For each face modificated.
327                while (j < VMI::mVMISteps[i].mModfaces.size())
328                {
329                        current_step.mModfaces.push_back(VMI::mVMISteps[i].mModfaces[j]);
330                       
331                        j++;
332                }
333               
334                msimpsequence->mSteps.push_back(current_step);
335        }
336}
337
338//---------------------------------------------------------------------------
339//      Init vmi mesh structure for a geometry mesh given.
340//---------------------------------------------------------------------------
341VMI::Mesh *     ViewPointDrivenSimplifier::initMeshStructure(Mesh       *geoMesh)
342{
343        GLuint                          i, j, v1, v2, v3, n, e, t;
344        VMI::Mesh               *vmi_mesh;
345        SubMesh                         *geosubmesh;
346        VertexBuffer    *vertex_buffer;
347        int                                             mesh_index_count;
348        int                                             index;
349
350        //      Reallocate memory for vmi mesh object.
351        vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh));
352
353        if (vmi_mesh == NULL)
354        {
355                fprintf(stderr, "Error allocating memory\n");
356                exit(1);
357        }
358
359        //      Shared vertex buffer.
360        vertex_buffer   =       geoMesh->mVertexBuffer;
361       
362        //      Reallocate memory for vertices of mesh.
363        vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex)
364                                                                                                                                                                *
365                                                                                                                                                                vertex_buffer->mVertexCount);
366
367        if (vmi_mesh->vertices == NULL)
368        {
369                fprintf(stderr, "Error allocating memory\n");
370                exit(1);
371        }
372
373        //      Initialize mesh index count.
374        mesh_index_count        =       0;
375       
376        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
377        {
378                mesh_index_count        +=      int(mGeoMesh->mSubMesh[submesh].mIndexCount);
379        }
380       
381        //      Reallocate memory for indices.
382        vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle)
383                                                                                                                                                                                *
384                                                                                                                                                                                (mesh_index_count / 3));
385
386        if (vmi_mesh->triangles == NULL)
387        {
388                fprintf(stderr, "Error allocating memory\n");
389                exit(1);
390        }
391
392        printf("Adding vertices...");
393       
394        //      Fill up vertices.
395        for (i = 0; i < vertex_buffer->mVertexCount; i++)
396        {
397                // Vertices start at 0.
398                vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x;
399                vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y;
400                vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z;
401               
402                vmi_mesh->vertices[i].numTriangles = 0;
403                vmi_mesh->vertices[i].triangles = NULL;
404                vmi_mesh->vertices[i].enable = GL_TRUE;
405        }
406       
407        printf("Ok\n");
408        vmi_mesh->numVertices = int(vertex_buffer->mVertexCount);
409        vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount);
410
411        //      Fill up triangles.
412        printf("Adding triangles...");
413       
414        //      Initialize index of triangle.
415        index   =       0;
416       
417        //      For each submesh.
418        for (unsigned int submesh       =       0; submesh      <       mGeoMesh->mSubMeshCount; submesh++)
419        {
420                //      Gets actual submesh.
421                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
422               
423                for (i = 0; i < (geosubmesh->mIndexCount / 3); i++)
424                {
425                        vmi_mesh->triangles[index].id                           = index;
426                        vmi_mesh->triangles[index].submesh              = submesh;
427                        vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)];
428                        vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1];
429                        vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2];
430
431                        vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices,
432                                        &vmi_mesh->triangles[index]);
433                        //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area);
434                        computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]);
435
436                        vmi_mesh->triangles[index].saliency = 0.0;
437
438                        vmi_mesh->triangles[index].enable = GL_TRUE;
439
440                        for (j = 0; j < 3; j++)
441                        {
442                                // Adding triangle index adjacent to 3 vertices
443                                v1 = vmi_mesh->triangles[index].indices[j];
444
445                                // Reallocate memory for the new adjacent triangle
446                                vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint));
447                                VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index);
448                        }
449
450                        //      Increments triangle count.
451                        index++;
452                }
453        }
454       
455        //printf("\n");
456        printf("Ok\n");
457
458        vmi_mesh->numTriangles                          = mesh_index_count / 3;
459        vmi_mesh->currentNumTriangles = mesh_index_count / 3;
460
461        printf("Num Triangles: %d\n",vmi_mesh->numTriangles);
462       
463        // E = 3 T / 2
464        vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge)
465                                                                                                                                                *
466                                                                                                                                                vmi_mesh->numTriangles * 3);
467
468        if (vmi_mesh->edges == NULL)
469        {
470                fprintf(stderr, "Error allocating memory\n");
471                exit(1);
472        }
473
474        // Init edges
475        for (i=0; i<vmi_mesh->numTriangles * 3; i++)
476        {
477                vmi_mesh->edges[i].triangles    = NULL;
478                vmi_mesh->edges[i].numTriangles = 0;
479        }
480
481        printf("Adding edges...");
482        n = 0;
483
484        for (i=0; i<vmi_mesh->numTriangles; i++)
485        {
486                t               =       0;
487                v1      =       vmi_mesh->triangles[i].indices[0];
488                v2      =       vmi_mesh->triangles[i].indices[1];
489                v3      =       vmi_mesh->triangles[i].indices[2];
490
491                if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1)
492                {
493                        vmi_mesh->edges[n].u                    =       v1;
494                        vmi_mesh->edges[n].v                    =       v2;
495                        vmi_mesh->edges[n].enable =     GL_TRUE;
496
497                        // Reallocate memory for the new adjacent triangle
498                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
499                        // Adding triangle i adjacent to edge n
500                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
501                        //printf("n:%d i:%d\n", n, i);
502
503                        // Adding edge n adjacent to triangle i
504                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
505                        n++;
506                }
507                else
508                {
509                        // Reallocate memory for the new adjacent triangle.
510                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
511                        // Adding triangle i adjacent to edge e
512                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
513                        //printf("n:%d i:%d\n", e, i);
514
515                        // Adding edge e adjacent to triangle i
516                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
517                }
518                if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1)
519                {
520                        vmi_mesh->edges[n].u = v2;
521                        vmi_mesh->edges[n].v = v3;
522                        vmi_mesh->edges[n].enable = GL_TRUE;
523
524                        // Reallocate memory for the new adjacent triangle
525                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
526                        // Adding triangle i adjacent to edge n
527                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
528                        //printf("n:%d i:%d\n", n, i);
529
530                        // Adding edge n adjacent to triangle i
531                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
532                        n++;
533                }
534                else
535                {
536                        // Reallocate memory for the new adjacent triangle
537                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
538                        // Adding triangle i adjacent to edge e
539                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
540                        //printf("n:%d i:%d\n", e, i);
541
542                        // Adding edge e adjacent to triangle i
543                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
544                }
545               
546                if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1)
547                {
548                        vmi_mesh->edges[n].u = v3;
549                        vmi_mesh->edges[n].v = v1;
550                        vmi_mesh->edges[n].enable = GL_TRUE;
551
552                        // Reallocate memory for the new adjacent triangle
553                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
554                        // Adding triangle i adjacent to edge n
555                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
556                        //printf("n:%d i:%d\n", n, i);
557
558                        // Adding edge n adjacent to triangle i
559                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
560                        n++;
561                }
562                else
563                {
564                        // Reallocate memory for the new adjacent triangle
565                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
566                        // Adding triangle i adjacent to edge e
567                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
568                        //printf("n:%d i:%d\n", e, i);
569
570                        // Adding edge e adjacent to triangle i
571                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
572                }
573        }
574       
575        printf("Ok\n");
576        vmi_mesh->numEdges = n;
577
578        return vmi_mesh;
579}
580
581//---------------------------------------------------------------------------
582//      Gets the geometry mesh of a  the vmi mesh given.
583//---------------------------------------------------------------------------
584void    ViewPointDrivenSimplifier::loadMesh()
585{
586        int                                             num_indices;
587        SubMesh                         *geosubmesh;
588        VertexBuffer    *vertex_buffer;
589       
590        //      Gets old vertex buffer.
591        vertex_buffer   =       mGeoMesh->mVertexBuffer;
592       
593        //      Initialize auxiliar vertex buffer.
594        mVB     =       new     VertexBuffer();
595       
596        mVB->mPosition          =       new     Vector3[VMI::mesh->currentNumVertices];
597        mVB->mNormal                    =       new     Vector3[VMI::mesh->currentNumVertices];
598        mVB->mTexCoords         =       new     Vector2[VMI::mesh->currentNumVertices];
599
600        mVB->mVertexInfo        =       vertex_buffer->mVertexInfo;
601
602        //      For each submesh.
603        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
604        {
605                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
606
607                delete  []geosubmesh->mIndex;
608
609                //      Initialize submesh index count;
610                num_indices     =       0;
611
612                //      For each triangle.
613                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
614                {
615                        //      If is enable and of the current submesh.
616                        if ((VMI::mesh->triangles[i].enable)
617                                        &&
618                                        (VMI::mesh->triangles[i].submesh == submesh))
619                        {
620                                //      Increments submesh index count.
621                                num_indices     +=      3;
622                        }
623                }
624
625                geosubmesh->mIndexCount =       num_indices;
626
627                geosubmesh->mIndex      =       new Index[geosubmesh->mIndexCount];
628
629                //      Initialize number of indices.
630                num_indices     =       0;
631
632                //      Fill up indices.
633                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
634                {
635                        if ((VMI::mesh->triangles[i].enable)
636                                        &&
637                                        (VMI::mesh->triangles[i].submesh == submesh))
638                        {
639                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[0];
640                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[1];
641                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[2];
642                        }
643                }
644
645                //      For each index.
646                for (unsigned int i = 0; i < geosubmesh->mIndexCount; i++)
647                {
648                        findVertex(submesh,i);
649                }
650
651                geosubmesh->mVertexBuffer       =       mVB;
652        }
653
654        delete  vertex_buffer;
655       
656        mGeoMesh->mVertexBuffer =       mVB;
657
658}
659
660//---------------------------------------------------------------------------
661//      Find vertex in auxiliar vertex buffer.
662//---------------------------------------------------------------------------
663void    ViewPointDrivenSimplifier::findVertex(size_t    submesh, size_t elem)
664{
665        bool                                    found;
666        int                                             index;
667        unsigned int                    i;
668        int                                             new_elem;
669        VertexBuffer    *vertex_buffer;
670
671        found   =       false;
672
673        //      Shared vertex buffer.
674        vertex_buffer   =       mGeoMesh->mVertexBuffer;
675       
676        index   =       mGeoMesh->mSubMesh[submesh].mIndex[elem];
677       
678        i       =       0;
679       
680        while (!found && (i < mVB->mVertexCount))
681        {
682                if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x)
683                                &&
684                                (VMI::mesh->vertices[index].y == mVB->mPosition[i].y)
685                                &&
686                                (VMI::mesh->vertices[index].z == mVB->mPosition[i].z))
687                {
688                        found   =       true;
689
690                        //      Update index.
691                        mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        i;
692                }
693
694                //      Increments index.
695                i++;
696        }
697
698        if (!found)
699        {
700                //      Last element.
701                new_elem        =       int(mVB->mVertexCount);
702               
703                //      Add to last.
704                mVB->mPosition[new_elem].x      =       VMI::mesh->vertices[index].x;
705                mVB->mPosition[new_elem].y      =       VMI::mesh->vertices[index].y;
706                mVB->mPosition[new_elem].z      =       VMI::mesh->vertices[index].z;
707
708                mVB->mNormal[new_elem].x        =       vertex_buffer->mNormal[index].x;
709                mVB->mNormal[new_elem].y        =       vertex_buffer->mNormal[index].y;
710                mVB->mNormal[new_elem].z        =       vertex_buffer->mNormal[index].z;
711
712                mVB->mTexCoords[new_elem].x     =       vertex_buffer->mTexCoords[index].x;
713                mVB->mTexCoords[new_elem].y     =       vertex_buffer->mTexCoords[index].y;
714
715                //      Update index.
716                mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        new_elem;
717
718                //      Increments vertex count.
719                mVB->mVertexCount++;
720        }
721}
722
Note: See TracBrowser for help on using the repository browser.