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

Revision 1014, 20.6 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//      Constructor.
71//---------------------------------------------------------------------------
72GeometryBasedSimplifier::GeometryBasedSimplifier(       const Mesh      *m,
73                                                                                                                                                                                                        TIPOFUNC                upb)
74                                                                                                                                                                                                :MeshSimplifier(m,upb)
75{
76}
77
78//---------------------------------------------------------------------------
79//      Destroyer.
80//---------------------------------------------------------------------------
81GeometryBasedSimplifier::~GeometryBasedSimplifier()
82{
83}
84
85//---------------------------------------------------------------------------
86// 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.
87//---------------------------------------------------------------------------
88void GeometryBasedSimplifier::Simplify(Real paramlod)
89{
90        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh);
91       
92        m_qslim->setMeshLeaves(indexMeshLeaves);
93       
94        msimpsequence   =       m_qslim->Decimate(paramlod,0,mUPB);
95       
96        mGeoMesh        =       m_qslim->GetMesh();
97       
98        delete  m_qslim;
99}
100
101//---------------------------------------------------------------------------
102// 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.
103//---------------------------------------------------------------------------
104void GeometryBasedSimplifier::Simplify(uint32 numvertices)
105{
106        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh);
107       
108        m_qslim->setMeshLeaves(indexMeshLeaves);
109       
110        msimpsequence   =       m_qslim->Decimate((float)numvertices,1,mUPB);
111
112        mGeoMesh        =       m_qslim->GetMesh();
113       
114        delete m_qslim;
115}
116
117////////////////////////////////////////////////////////////////////////////
118//                                                                                                                                                                                                                                                                                              //
119//                                                              ViewPointDrivenSimplifier class                                                                                                 //
120//                                                                                                                                                                                                                                                                                              //
121////////////////////////////////////////////////////////////////////////////
122
123//---------------------------------------------------------------------------
124/// Class constructor. Will call Simplifier class constructor.
125//---------------------------------------------------------------------------
126ViewPointDrivenSimplifier::ViewPointDrivenSimplifier(   const Mesh      *m,
127                                                                                                                                                                                                                        TIPOFUNC                upb)
128                                                                                                                                                                                                                :MeshSimplifier(m,upb)
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        // RGB and Alpha.
165        glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);
166        glutInitWindowSize(VMI::width, VMI::height);
167        glutInitWindowPosition(100, 100);
168
169        VMI::vmiWin     =       glutCreateWindow("VMI");
170
171        glewInit();
172
173        VMI::init();
174
175        if (VMI::bLoadCamerasFromFile == GL_FALSE)
176        {
177                switch (VMI::cameraType)
178                {
179                        case 0:
180                                VMI::cameras = VMI::setCameras(VMI::radius, OCTAHEDRON, &VMI::numCameras);
181                                printf("Number of cameras: %d\n", OCTAHEDRON);
182                                break;
183                        case 1:
184                                VMI:: cameras = VMI::setCameras(VMI::radius, ICOSAHEDRON, &VMI::numCameras);
185                                printf("Number of cameras: %d\n", ICOSAHEDRON);
186                                break;
187                        case 2:
188                                VMI::cameras = VMI::setCameras(VMI::radius, DODECAHEDRON, &VMI::numCameras);
189                                printf("Number of cameras: %d\n", DODECAHEDRON);
190                                break;
191                        default:
192                                break;
193                }
194        }
195
196        VMI::histogram  =       VMI::initHistogram(     VMI::mesh->currentNumTriangles,
197                                                                                                                                                                VMI::numCameras);
198
199        VMI::initialIs  =       VMI::initIs(VMI::numCameras);
200
201}
202
203//---------------------------------------------------------------------------
204/// Class destructor.
205//---------------------------------------------------------------------------
206ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void)
207{
208        // Free memory
209         VMI::freeMemory();
210}
211
212/// Copy constructor
213//ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&);
214
215/// Assignment operator
216//ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&);
217
218/// 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.
219void ViewPointDrivenSimplifier::Simplify(Real percent)
220{
221        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent);
222
223        if ((VMI::numDemandedTriangles == 0)
224                        ||
225                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles))
226        {
227                printf("Illegal number of triangles.\n");
228        }
229
230        VMI::display();
231
232        //      Load a geometry mesh for vmi mesh.
233        loadMesh();
234
235        GetMeshSimpSequence();
236}
237
238//---------------------------------------------------------------------------
239/// 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.
240//---------------------------------------------------------------------------
241void ViewPointDrivenSimplifier::Simplify(uint32 numVertices)
242{
243        float   percent;
244
245        percent =       (numVertices    *       100.0) / VMI::mesh->numVertices;
246
247        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100));
248
249        if ((VMI::numDemandedTriangles == 0)
250                        ||
251                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles))
252        {
253                printf("Illegal number of triangles.\n");
254        }
255
256        VMI::display();
257
258        //      Load a geometry mesh for vmi mesh.
259        loadMesh();
260
261        GetMeshSimpSequence();
262}
263
264// Returns the simplified mesh.
265//Mesh  *       ViewPointDrivenSimplifier::GetMesh()
266//{
267//      return mGeoMesh;
268//}
269
270//---------------------------------------------------------------------------
271//      Gets the VMI mesh simplification sequence.
272//---------------------------------------------------------------------------
273void    ViewPointDrivenSimplifier::GetMeshSimpSequence()
274{
275        unsigned        int j = 0;
276       
277        MeshSimplificationSequence::Step        current_step;
278       
279        msimpsequence   =       new MeshSimplificationSequence();
280       
281        //      For each simplification step.
282        for (unsigned   int i = 0;      i < VMI::mVMISteps.size();      i++)
283        {
284                current_step.mV0        =       VMI::mVMISteps[i].mV0;
285                current_step.mV1        =       VMI::mVMISteps[i].mV1;
286                current_step.mT0        =       VMI::mVMISteps[i].mT0;
287                current_step.mT1        =       VMI::mVMISteps[i].mT1;
288               
289                current_step.x  =       VMI::mVMISteps[i].x;
290                current_step.y  =       VMI::mVMISteps[i].y;
291                current_step.z  =       VMI::mVMISteps[i].z;
292
293                current_step.obligatorio        =       VMI::mVMISteps[i].obligatory;
294
295                //      For each face modificated.
296                while (j < VMI::mVMISteps[i].mModfaces.size())
297                {
298                        current_step.mModfaces.push_back(VMI::mVMISteps[i].mModfaces[j]);
299                       
300                        j++;
301                }
302               
303                msimpsequence->mSteps.push_back(current_step);
304        }
305}
306
307//---------------------------------------------------------------------------
308//      Init vmi mesh structure for a geometry mesh given.
309//---------------------------------------------------------------------------
310VMI::Mesh *     ViewPointDrivenSimplifier::initMeshStructure(Mesh       *geoMesh)
311{
312        GLuint                          i, j, v1, v2, v3, n, e, t;
313        VMI::Mesh               *vmi_mesh;
314        SubMesh                         *geosubmesh;
315        VertexBuffer    *vertex_buffer;
316        int                                             mesh_index_count;
317        int                                             index;
318
319        //      Reallocate memory for vmi mesh object.
320        vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh));
321
322        if (vmi_mesh == NULL)
323        {
324                fprintf(stderr, "Error allocating memory\n");
325                exit(1);
326        }
327
328        //      Shared vertex buffer.
329        vertex_buffer   =       geoMesh->mVertexBuffer;
330       
331        //      Reallocate memory for vertices of mesh.
332        vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex)
333                                                                                                                                                                *
334                                                                                                                                                                vertex_buffer->mVertexCount);
335
336        if (vmi_mesh->vertices == NULL)
337        {
338                fprintf(stderr, "Error allocating memory\n");
339                exit(1);
340        }
341
342        //      Initialize mesh index count.
343        mesh_index_count        =       0;
344       
345        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
346        {
347                mesh_index_count        +=      int(mGeoMesh->mSubMesh[submesh].mIndexCount);
348        }
349       
350        //      Reallocate memory for indices.
351        vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle)
352                                                                                                                                                                                *
353                                                                                                                                                                                (mesh_index_count / 3));
354
355        if (vmi_mesh->triangles == NULL)
356        {
357                fprintf(stderr, "Error allocating memory\n");
358                exit(1);
359        }
360
361        printf("Adding vertices...");
362       
363        //      Fill up vertices.
364        for (i = 0; i < vertex_buffer->mVertexCount; i++)
365        {
366                // Vertices start at 0.
367                vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x;
368                vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y;
369                vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z;
370               
371                vmi_mesh->vertices[i].numTriangles = 0;
372                vmi_mesh->vertices[i].triangles = NULL;
373                vmi_mesh->vertices[i].enable = GL_TRUE;
374        }
375       
376        printf("Ok\n");
377        vmi_mesh->numVertices = int(vertex_buffer->mVertexCount);
378        vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount);
379
380        //      Fill up triangles.
381        printf("Adding triangles...");
382       
383        //      Initialize index of triangle.
384        index   =       0;
385       
386        //      For each submesh.
387        for (unsigned int submesh       =       0; submesh      <       mGeoMesh->mSubMeshCount; submesh++)
388        {
389                //      Gets actual submesh.
390                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
391               
392                for (i = 0; i < (geosubmesh->mIndexCount / 3); i++)
393                {
394                        vmi_mesh->triangles[index].id                           = index;
395                        vmi_mesh->triangles[index].submesh              = submesh;
396                        vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)];
397                        vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1];
398                        vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2];
399
400                        vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices,
401                                        &vmi_mesh->triangles[index]);
402                        //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area);
403                        computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]);
404
405                        vmi_mesh->triangles[index].saliency = 0.0;
406
407                        vmi_mesh->triangles[index].enable = GL_TRUE;
408
409                        for (j = 0; j < 3; j++)
410                        {
411                                // Adding triangle index adjacent to 3 vertices.
412                                v1 = vmi_mesh->triangles[index].indices[j];
413
414                                // Reallocate memory for the new adjacent triangle.
415                                vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint));
416                                VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index);
417                        }
418
419                        //      Increments triangle count.
420                        index++;
421                }
422        }
423       
424        printf("Ok\n");
425
426        vmi_mesh->numTriangles                          = mesh_index_count / 3;
427        vmi_mesh->currentNumTriangles = mesh_index_count / 3;
428
429        printf("Num Triangles: %d\n",vmi_mesh->numTriangles);
430       
431        // E = 3 T / 2
432        vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge)
433                                                                                                                                                *
434                                                                                                                                                vmi_mesh->numTriangles * 3);
435
436        if (vmi_mesh->edges == NULL)
437        {
438                fprintf(stderr, "Error allocating memory\n");
439                exit(1);
440        }
441
442        // Init edges
443        for (i=0; i<vmi_mesh->numTriangles * 3; i++)
444        {
445                vmi_mesh->edges[i].triangles    = NULL;
446                vmi_mesh->edges[i].numTriangles = 0;
447        }
448
449        printf("Adding edges...");
450        n = 0;
451
452        for (i=0; i<vmi_mesh->numTriangles; i++)
453        {
454                t               =       0;
455                v1      =       vmi_mesh->triangles[i].indices[0];
456                v2      =       vmi_mesh->triangles[i].indices[1];
457                v3      =       vmi_mesh->triangles[i].indices[2];
458
459                if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1)
460                {
461                        vmi_mesh->edges[n].u                    =       v1;
462                        vmi_mesh->edges[n].v                    =       v2;
463                        vmi_mesh->edges[n].enable =     GL_TRUE;
464
465                        // Reallocate memory for the new adjacent triangle
466                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
467                        // Adding triangle i adjacent to edge n
468                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
469                        //printf("n:%d i:%d\n", n, i);
470
471                        // Adding edge n adjacent to triangle i
472                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
473                        n++;
474                }
475                else
476                {
477                        // Reallocate memory for the new adjacent triangle.
478                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
479                        // Adding triangle i adjacent to edge e
480                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
481                        //printf("n:%d i:%d\n", e, i);
482
483                        // Adding edge e adjacent to triangle i
484                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
485                }
486                if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1)
487                {
488                        vmi_mesh->edges[n].u = v2;
489                        vmi_mesh->edges[n].v = v3;
490                        vmi_mesh->edges[n].enable = GL_TRUE;
491
492                        // Reallocate memory for the new adjacent triangle
493                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
494                        // Adding triangle i adjacent to edge n
495                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
496                        //printf("n:%d i:%d\n", n, i);
497
498                        // Adding edge n adjacent to triangle i
499                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
500                        n++;
501                }
502                else
503                {
504                        // Reallocate memory for the new adjacent triangle
505                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
506                        // Adding triangle i adjacent to edge e
507                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
508                        //printf("n:%d i:%d\n", e, i);
509
510                        // Adding edge e adjacent to triangle i
511                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
512                }
513               
514                if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1)
515                {
516                        vmi_mesh->edges[n].u = v3;
517                        vmi_mesh->edges[n].v = v1;
518                        vmi_mesh->edges[n].enable = GL_TRUE;
519
520                        // Reallocate memory for the new adjacent triangle
521                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint));
522                        // Adding triangle i adjacent to edge n
523                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i);
524                        //printf("n:%d i:%d\n", n, i);
525
526                        // Adding edge n adjacent to triangle i
527                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n);
528                        n++;
529                }
530                else
531                {
532                        // Reallocate memory for the new adjacent triangle
533                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint));
534                        // Adding triangle i adjacent to edge e
535                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i);
536                        //printf("n:%d i:%d\n", e, i);
537
538                        // Adding edge e adjacent to triangle i
539                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e);
540                }
541        }
542       
543        printf("Ok\n");
544        vmi_mesh->numEdges = n;
545
546        return vmi_mesh;
547}
548
549//---------------------------------------------------------------------------
550//      Gets the geometry mesh of a  the vmi mesh given.
551//---------------------------------------------------------------------------
552void    ViewPointDrivenSimplifier::loadMesh()
553{
554        int                                             num_indices;
555        SubMesh                         *geosubmesh;
556        VertexBuffer    *vertex_buffer;
557       
558        //      Gets old vertex buffer.
559        vertex_buffer   =       mGeoMesh->mVertexBuffer;
560       
561        //      Initialize auxiliar vertex buffer.
562        mVB     =       new     VertexBuffer();
563       
564        mVB->mPosition          =       new     Vector3[VMI::mesh->currentNumVertices];
565        mVB->mNormal                    =       new     Vector3[VMI::mesh->currentNumVertices];
566        mVB->mTexCoords         =       new     Vector2[VMI::mesh->currentNumVertices];
567
568        mVB->mVertexInfo        =       vertex_buffer->mVertexInfo;
569
570        //      For each submesh.
571        for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++)
572        {
573                geosubmesh      =       &mGeoMesh->mSubMesh[submesh];
574
575                delete  []geosubmesh->mIndex;
576
577                //      Initialize submesh index count;
578                num_indices     =       0;
579
580                //      For each triangle.
581                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
582                {
583                        //      If is enable and of the current submesh.
584                        if ((VMI::mesh->triangles[i].enable)
585                                        &&
586                                        (VMI::mesh->triangles[i].submesh == submesh))
587                        {
588                                //      Increments submesh index count.
589                                num_indices     +=      3;
590                        }
591                }
592
593                geosubmesh->mIndexCount =       num_indices;
594
595                geosubmesh->mIndex      =       new Index[geosubmesh->mIndexCount];
596
597                //      Initialize number of indices.
598                num_indices     =       0;
599
600                //      Fill up indices.
601                for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++)
602                {
603                        if ((VMI::mesh->triangles[i].enable)
604                                        &&
605                                        (VMI::mesh->triangles[i].submesh == submesh))
606                        {
607                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[0];
608                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[1];
609                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[2];
610                        }
611                }
612
613                //      For each index.
614                for (unsigned int i = 0; i < geosubmesh->mIndexCount; i++)
615                {
616                        findVertex(submesh,i);
617                }
618
619                geosubmesh->mVertexBuffer       =       mVB;
620        }
621
622        delete  vertex_buffer;
623       
624        mGeoMesh->mVertexBuffer =       mVB;
625
626}
627
628//---------------------------------------------------------------------------
629//      Find vertex in auxiliar vertex buffer.
630//---------------------------------------------------------------------------
631void    ViewPointDrivenSimplifier::findVertex(size_t    submesh, size_t elem)
632{
633        bool                                    found;
634        int                                             index;
635        unsigned int                    i;
636        int                                             new_elem;
637        VertexBuffer    *vertex_buffer;
638
639        found   =       false;
640
641        //      Shared vertex buffer.
642        vertex_buffer   =       mGeoMesh->mVertexBuffer;
643       
644        index   =       mGeoMesh->mSubMesh[submesh].mIndex[elem];
645       
646        i       =       0;
647       
648        while (!found && (i < mVB->mVertexCount))
649        {
650                if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x)
651                                &&
652                                (VMI::mesh->vertices[index].y == mVB->mPosition[i].y)
653                                &&
654                                (VMI::mesh->vertices[index].z == mVB->mPosition[i].z))
655                {
656                        found   =       true;
657
658                        //      Update index.
659                        mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        i;
660                }
661
662                //      Increments index.
663                i++;
664        }
665
666        if (!found)
667        {
668                //      Last element.
669                new_elem        =       int(mVB->mVertexCount);
670               
671                //      Add to last.
672                mVB->mPosition[new_elem].x      =       VMI::mesh->vertices[index].x;
673                mVB->mPosition[new_elem].y      =       VMI::mesh->vertices[index].y;
674                mVB->mPosition[new_elem].z      =       VMI::mesh->vertices[index].z;
675
676                mVB->mNormal[new_elem].x        =       vertex_buffer->mNormal[index].x;
677                mVB->mNormal[new_elem].y        =       vertex_buffer->mNormal[index].y;
678                mVB->mNormal[new_elem].z        =       vertex_buffer->mNormal[index].z;
679
680                mVB->mTexCoords[new_elem].x     =       vertex_buffer->mTexCoords[index].x;
681                mVB->mTexCoords[new_elem].y     =       vertex_buffer->mTexCoords[index].y;
682
683                //      Update index.
684                mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        new_elem;
685
686                //      Increments vertex count.
687                mVB->mVertexCount++;
688        }
689}
690
Note: See TracBrowser for help on using the repository browser.