1 | #include "GeoMeshSimplifier.h" |
---|
2 | #include "GeoMeshSimpSequence.h" |
---|
3 | #include "SimplificationMethod.h" |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | |
---|
7 | using namespace Geometry; |
---|
8 | using namespace std; |
---|
9 | |
---|
10 | ////////////////////////////////////////////////////////////////////////// |
---|
11 | // // |
---|
12 | // MeshSimplifier class // |
---|
13 | // // |
---|
14 | ////////////////////////////////////////////////////////////////////////// |
---|
15 | |
---|
16 | //------------------------------------------------------------------------- |
---|
17 | // MeshSimplifier constructor. |
---|
18 | //------------------------------------------------------------------------- |
---|
19 | MeshSimplifier::MeshSimplifier( const Mesh *m, |
---|
20 | TIPOFUNC upb) |
---|
21 | { |
---|
22 | // Saves initial state of mesh. |
---|
23 | mInitialMesh = new Mesh(); |
---|
24 | *mInitialMesh = *m; |
---|
25 | |
---|
26 | objmesh = m; |
---|
27 | mGeoMesh = NULL; |
---|
28 | msimpsequence = NULL; |
---|
29 | indexMeshLeaves = -1; |
---|
30 | |
---|
31 | // Sets the actual progress bar function. |
---|
32 | mUPB = upb; |
---|
33 | } |
---|
34 | |
---|
35 | //------------------------------------------------------------------------- |
---|
36 | // MeshSimplifier destroyer. |
---|
37 | //------------------------------------------------------------------------- |
---|
38 | MeshSimplifier::~MeshSimplifier() |
---|
39 | { |
---|
40 | delete mInitialMesh; |
---|
41 | delete msimpsequence; |
---|
42 | } |
---|
43 | |
---|
44 | //------------------------------------------------------------------------- |
---|
45 | // Returns the simplified mesh. |
---|
46 | //------------------------------------------------------------------------- |
---|
47 | Mesh * MeshSimplifier::GetMesh() |
---|
48 | { |
---|
49 | return mGeoMesh; |
---|
50 | } |
---|
51 | |
---|
52 | //------------------------------------------------------------------------- |
---|
53 | // Set submesh leaves |
---|
54 | //------------------------------------------------------------------------- |
---|
55 | void MeshSimplifier::setMeshLeaves(Geometry::Index index) |
---|
56 | { |
---|
57 | indexMeshLeaves = index; |
---|
58 | } |
---|
59 | |
---|
60 | //------------------------------------------------------------------------- |
---|
61 | // Returns the simplification sequence for general meshes. |
---|
62 | //------------------------------------------------------------------------- |
---|
63 | MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence() |
---|
64 | { |
---|
65 | return msimpsequence; |
---|
66 | } |
---|
67 | |
---|
68 | //------------------------------------------------------------------------- |
---|
69 | // Sort mesh bones. |
---|
70 | //------------------------------------------------------------------------- |
---|
71 | void MeshSimplifier::sortBones() |
---|
72 | { |
---|
73 | <<<<<<< .mine
|
---|
74 | VertexBuffer *simplified_vb; |
---|
75 | VertexBuffer *initial_vb; |
---|
76 | =======
|
---|
77 | VertexBuffer *mesh_vb; |
---|
78 | VertexBuffer *copy_vb; |
---|
79 | >>>>>>> .r2080
|
---|
80 | |
---|
81 | <<<<<<< .mine
|
---|
82 | std::vector<VertexBoneAssignment> *simplified_bones; |
---|
83 | std::vector<VertexBoneAssignment> *initial_bones; |
---|
84 | =======
|
---|
85 | std::vector<VertexBoneAssignment> *mesh_bones; |
---|
86 | std::vector<VertexBoneAssignment> *copy_bones; |
---|
87 | >>>>>>> .r2080
|
---|
88 | |
---|
89 | <<<<<<< .mine
|
---|
90 | // After simplifying, the object always is shared vertex |
---|
91 | // so, the bones must be placed in the GeoMesh |
---|
92 | simplified_bones = &mGeoMesh->mBones; |
---|
93 | simplified_vb = mGeoMesh->mVertexBuffer; |
---|
94 | =======
|
---|
95 | // After simplifying, the object always is shared vertex |
---|
96 | // so, the bones must be placed in the GeoMesh |
---|
97 | mesh_bones = &mGeoMesh->mBones; |
---|
98 | mesh_vb = mGeoMesh->mVertexBuffer; |
---|
99 | >>>>>>> .r2080
|
---|
100 | |
---|
101 | <<<<<<< .mine
|
---|
102 | // we assume the original mesh is shared vertex |
---|
103 | // because before the simplification process |
---|
104 | // the mesh becomes converted to shared vertex |
---|
105 | // so, the original bones must be searched in the |
---|
106 | // Mesh, not in every submesh |
---|
107 | =======
|
---|
108 | // we assume the original mesh is shared vertex |
---|
109 | // because before the simplification process |
---|
110 | // the mesh becomes converted to shared vertex |
---|
111 | // so, the original bones must be searched in the |
---|
112 | // Mesh, not in every submesh |
---|
113 | >>>>>>> .r2080
|
---|
114 | |
---|
115 | <<<<<<< .mine
|
---|
116 | simplified_bones->clear(); |
---|
117 | =======
|
---|
118 | mesh_bones->clear(); |
---|
119 | >>>>>>> .r2080
|
---|
120 | |
---|
121 | <<<<<<< .mine
|
---|
122 | // Vertex buffers. |
---|
123 | initial_vb = mInitialMesh->mVertexBuffer; |
---|
124 | =======
|
---|
125 | // Vertex buffers. |
---|
126 | copy_vb = mInitialMesh->mVertexBuffer; |
---|
127 | >>>>>>> .r2080
|
---|
128 | |
---|
129 | <<<<<<< .mine
|
---|
130 | // Bones. |
---|
131 | initial_bones = &mInitialMesh->mBones; |
---|
132 | =======
|
---|
133 | // Bones. |
---|
134 | copy_bones = &mInitialMesh->mBones; |
---|
135 | |
---|
136 | // If there are submesh bones. |
---|
137 | for (int b = 0; b < copy_bones->size(); b++) |
---|
138 | { |
---|
139 | VertexBoneAssignment assign; |
---|
140 | >>>>>>> .r2080
|
---|
141 | |
---|
142 | <<<<<<< .mine
|
---|
143 | // For each bone assignment. |
---|
144 | for (int b = 0; b < initial_bones->size(); b++) |
---|
145 | { |
---|
146 | VertexBoneAssignment assign; |
---|
147 | =======
|
---|
148 | int n = (*copy_bones)[b].vertexIndex; |
---|
149 | >>>>>>> .r2080
|
---|
150 | |
---|
151 | <<<<<<< .mine
|
---|
152 | int n = (*initial_bones)[b].vertexIndex; |
---|
153 | |
---|
154 | // For each vertex. |
---|
155 | for (int i = 0; i < simplified_vb->mVertexCount; i++) |
---|
156 | { |
---|
157 | if (simplified_vb->mPosition[i].x == initial_vb->mPosition[n].x && |
---|
158 | simplified_vb->mPosition[i].y == initial_vb->mPosition[n].y && |
---|
159 | simplified_vb->mPosition[i].z == initial_vb->mPosition[n].z) |
---|
160 | { |
---|
161 | assign.vertexIndex = i; |
---|
162 | assign.boneIndex = (*initial_bones)[b].boneIndex; |
---|
163 | assign.weight = (*initial_bones)[b].weight; |
---|
164 | |
---|
165 | simplified_bones->push_back(assign); |
---|
166 | } |
---|
167 | =======
|
---|
168 | // Initialize o. |
---|
169 | |
---|
170 | int o=0; |
---|
171 | for (o=0; o<mesh_vb->mVertexCount; o++) |
---|
172 | { |
---|
173 | if (mesh_vb->mPosition[o].x == copy_vb->mPosition[n].x && |
---|
174 | mesh_vb->mPosition[o].y == copy_vb->mPosition[n].y && |
---|
175 | mesh_vb->mPosition[o].z == copy_vb->mPosition[n].z) |
---|
176 | { |
---|
177 | assign.vertexIndex = o; |
---|
178 | assign.boneIndex = (*copy_bones)[b].boneIndex; |
---|
179 | assign.weight = 1.0f; |
---|
180 | mesh_bones->push_back(assign); |
---|
181 | } |
---|
182 | >>>>>>> .r2080
|
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | ////////////////////////////////////////////////////////////////////////// |
---|
189 | // // |
---|
190 | // GeometryBasedSimplifier class // |
---|
191 | // // |
---|
192 | ////////////////////////////////////////////////////////////////////////// |
---|
193 | |
---|
194 | //------------------------------------------------------------------------- |
---|
195 | // Constructor. |
---|
196 | //------------------------------------------------------------------------- |
---|
197 | GeometryBasedSimplifier::GeometryBasedSimplifier( const Mesh *m, |
---|
198 | TIPOFUNC upb) |
---|
199 | :MeshSimplifier(m,upb) |
---|
200 | { |
---|
201 | } |
---|
202 | |
---|
203 | //------------------------------------------------------------------------- |
---|
204 | // Destroyer. |
---|
205 | //------------------------------------------------------------------------- |
---|
206 | GeometryBasedSimplifier::~GeometryBasedSimplifier() |
---|
207 | { |
---|
208 | } |
---|
209 | |
---|
210 | //------------------------------------------------------------------------- |
---|
211 | // Starts the simplification process. Receives as a parameter |
---|
212 | // the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify |
---|
213 | // method to perform an image based simplification. |
---|
214 | //------------------------------------------------------------------------- |
---|
215 | void GeometryBasedSimplifier::Simplify(Real paramlod) |
---|
216 | { |
---|
217 | SimplificationMethod *m_qslim = new SimplificationMethod(objmesh); |
---|
218 | |
---|
219 | m_qslim->setMeshLeaves(indexMeshLeaves); |
---|
220 | |
---|
221 | msimpsequence = m_qslim->Decimate(paramlod,0,mUPB); |
---|
222 | |
---|
223 | mGeoMesh = m_qslim->GetMesh(); |
---|
224 | |
---|
225 | // Debug. |
---|
226 | cout << "Number of bone assignaments: " |
---|
227 | << mGeoMesh->mBones.size() |
---|
228 | << endl; |
---|
229 | |
---|
230 | // if the initial mesh had shared vertex, |
---|
231 | // convert the mesh to shared vertex |
---|
232 | //Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
233 | //delete mGeoMesh; |
---|
234 | //mGeoMesh = sharedMesh; |
---|
235 | |
---|
236 | // if the initial mesh had shared vertex, |
---|
237 | // convert the mesh to shared vertex |
---|
238 | for (int i=0; i<mInitialMesh->mSubMeshCount; i++) |
---|
239 | { |
---|
240 | if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) |
---|
241 | { |
---|
242 | Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
243 | delete mGeoMesh; |
---|
244 | mGeoMesh = sharedMesh; |
---|
245 | break; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | // Sort bones. |
---|
250 | //sortBones(); |
---|
251 | |
---|
252 | delete m_qslim; |
---|
253 | } |
---|
254 | |
---|
255 | //------------------------------------------------------------------------- |
---|
256 | // Starts the simplification process. Receives as a parameter the |
---|
257 | // number of vertices of the resulting mesh. |
---|
258 | // Implements the Simplifier::Simplify method to |
---|
259 | // perform an image based simplification. |
---|
260 | //------------------------------------------------------------------------- |
---|
261 | void GeometryBasedSimplifier::Simplify(uint32 numvertices) |
---|
262 | { |
---|
263 | SimplificationMethod *m_qslim = new SimplificationMethod(objmesh); |
---|
264 | |
---|
265 | m_qslim->setMeshLeaves(indexMeshLeaves); |
---|
266 | |
---|
267 | msimpsequence = m_qslim->Decimate((float)numvertices,1,mUPB); |
---|
268 | |
---|
269 | mGeoMesh = m_qslim->GetMesh(); |
---|
270 | |
---|
271 | // if the initial mesh had shared vertex, |
---|
272 | // convert the mesh to shared vertex |
---|
273 | //Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
274 | //delete mGeoMesh; |
---|
275 | //mGeoMesh = sharedMesh; |
---|
276 | |
---|
277 | // if the initial mesh had shared vertex, |
---|
278 | // convert the mesh to shared vertex |
---|
279 | for (int i=0; i<mInitialMesh->mSubMeshCount; i++) |
---|
280 | { |
---|
281 | if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) |
---|
282 | { |
---|
283 | Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
284 | delete mGeoMesh; |
---|
285 | mGeoMesh = sharedMesh; |
---|
286 | break; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | // Sort bones. |
---|
291 | //sortBones(); |
---|
292 | |
---|
293 | delete m_qslim; |
---|
294 | } |
---|
295 | |
---|
296 | ////////////////////////////////////////////////////////////////////////// |
---|
297 | // // |
---|
298 | // ViewPointDrivenSimplifier class // |
---|
299 | // // |
---|
300 | ////////////////////////////////////////////////////////////////////////// |
---|
301 | |
---|
302 | //------------------------------------------------------------------------ |
---|
303 | // Class constructor. Will call Simplifier class constructor. |
---|
304 | //------------------------------------------------------------------------ |
---|
305 | ViewPointDrivenSimplifier::ViewPointDrivenSimplifier( const Mesh *m, |
---|
306 | TIPOFUNC upb) |
---|
307 | :MeshSimplifier(m,upb) |
---|
308 | { |
---|
309 | // Set progress update function |
---|
310 | VMI::mUPB = upb; |
---|
311 | |
---|
312 | VMI::width = 256; |
---|
313 | VMI::height = 256; |
---|
314 | |
---|
315 | VMI::bEnableOffScreen = GL_TRUE; |
---|
316 | VMI::bBeQuiet = GL_FALSE; |
---|
317 | VMI::bSaveLog = GL_FALSE; |
---|
318 | VMI::bLoadCamerasFromFile = GL_FALSE; |
---|
319 | VMI::bRemoveRedundantVertices = GL_TRUE; |
---|
320 | |
---|
321 | VMI::cameraType = 3; // 20 viewpoints |
---|
322 | VMI::radius = 1.3; |
---|
323 | VMI::fov = 60.0; |
---|
324 | |
---|
325 | printf("w: %d h: %d\n", VMI::width, VMI::height); |
---|
326 | |
---|
327 | printf( "t: %d c: %d o: %d r: %f\n", |
---|
328 | VMI::numDemandedTriangles, |
---|
329 | VMI::cameraType, |
---|
330 | VMI::bEnableOffScreen, |
---|
331 | VMI::radius); |
---|
332 | |
---|
333 | // Fill up attributes. |
---|
334 | fillUpPosNorTC(mInitialMesh); |
---|
335 | |
---|
336 | VMI::vPositions = vPositions; |
---|
337 | VMI::vNormals = vNormals; |
---|
338 | VMI::vTexCoords = vTexCoords; |
---|
339 | |
---|
340 | mGeoMesh = new Mesh(); |
---|
341 | |
---|
342 | *mGeoMesh = *m; |
---|
343 | |
---|
344 | // Transform NoSV Mesh to a SV Mesh. |
---|
345 | //mGeoMesh = mGeoMesh->toSharedVertex(); |
---|
346 | |
---|
347 | // Join Vertices. |
---|
348 | mTexConserver = new TextureConserver(); |
---|
349 | mTexConserver->JoinVertices(mGeoMesh); |
---|
350 | |
---|
351 | // Loads the vmi mesh structure for a geometry mesh given. |
---|
352 | VMI::mesh = initMeshStructure(mGeoMesh); |
---|
353 | |
---|
354 | // RGB and Alpha. |
---|
355 | glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); |
---|
356 | glutInitWindowSize(VMI::width, VMI::height); |
---|
357 | glutInitWindowPosition(100, 100); |
---|
358 | |
---|
359 | VMI::vmiWin = glutCreateWindow("VMI"); |
---|
360 | |
---|
361 | glewInit(); |
---|
362 | |
---|
363 | VMI::init(); |
---|
364 | |
---|
365 | if (VMI::bLoadCamerasFromFile == GL_FALSE) |
---|
366 | VMI::cameras = VMI::setCameras(VMI::radius, VMI::cameraType, &VMI::numCameras); |
---|
367 | |
---|
368 | VMI::histogram = VMI::initHistogram( VMI::mesh->currentNumTriangles, |
---|
369 | VMI::numCameras); |
---|
370 | |
---|
371 | VMI::initialIs = VMI::initIs(VMI::numCameras); |
---|
372 | } |
---|
373 | |
---|
374 | //------------------------------------------------------------------------- |
---|
375 | /// Class destructor. |
---|
376 | //------------------------------------------------------------------------- |
---|
377 | ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void) |
---|
378 | { |
---|
379 | // Free memory |
---|
380 | VMI::freeMemory(); |
---|
381 | } |
---|
382 | |
---|
383 | /// Starts the simplification process. Receives as a parameter the |
---|
384 | /// LOD factor in a range of [0,1]. Implements the |
---|
385 | /// Simplifier::Simplify method to perform an image based simplification. |
---|
386 | void ViewPointDrivenSimplifier::Simplify(Real percent) |
---|
387 | { |
---|
388 | VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent); |
---|
389 | |
---|
390 | if ((VMI::numDemandedTriangles == 0) |
---|
391 | || |
---|
392 | (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) |
---|
393 | { |
---|
394 | printf("Illegal number of triangles.\n"); |
---|
395 | } |
---|
396 | |
---|
397 | VMI::display(); |
---|
398 | |
---|
399 | // Cleans firts simplification. |
---|
400 | VMI::freeMemory(); |
---|
401 | |
---|
402 | VMI::mesh = initMeshStructure(mInitialMesh); |
---|
403 | |
---|
404 | VMI::contractInitialMesh(VMI::mesh); |
---|
405 | |
---|
406 | // Load a geometry mesh for vmi mesh. |
---|
407 | loadMesh(); |
---|
408 | |
---|
409 | GetMeshSimpSequence(); |
---|
410 | |
---|
411 | // Sort bones. |
---|
412 | bonesReassignament(); |
---|
413 | } |
---|
414 | |
---|
415 | //------------------------------------------------------------------------- |
---|
416 | /// Starts the simplification process. Receives as a parameter |
---|
417 | /// the number of vertices of the resulting mesh. |
---|
418 | /// Implements the Simplifier::Simplify method to perform |
---|
419 | /// an image based simplification. |
---|
420 | //------------------------------------------------------------------------- |
---|
421 | void ViewPointDrivenSimplifier::Simplify(uint32 numVertices) |
---|
422 | { |
---|
423 | float percent; |
---|
424 | |
---|
425 | percent = (numVertices * 100.0) / VMI::mesh->numVertices; |
---|
426 | |
---|
427 | VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100)); |
---|
428 | |
---|
429 | if ((VMI::numDemandedTriangles == 0) |
---|
430 | || |
---|
431 | (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) |
---|
432 | { |
---|
433 | printf("Illegal number of triangles.\n"); |
---|
434 | } |
---|
435 | |
---|
436 | VMI::display(); |
---|
437 | |
---|
438 | // Cleans firts simplification. |
---|
439 | VMI::freeMemory(); |
---|
440 | |
---|
441 | VMI::mesh = initMeshStructure(mInitialMesh); |
---|
442 | |
---|
443 | VMI::contractInitialMesh(VMI::mesh); |
---|
444 | |
---|
445 | // Load a geometry mesh for vmi mesh. |
---|
446 | loadMesh(); |
---|
447 | |
---|
448 | GetMeshSimpSequence(); |
---|
449 | |
---|
450 | // Sort bones. |
---|
451 | bonesReassignament(); |
---|
452 | } |
---|
453 | |
---|
454 | //------------------------------------------------------------------------- |
---|
455 | // Gets the VMI mesh simplification sequence. |
---|
456 | //------------------------------------------------------------------------- |
---|
457 | void ViewPointDrivenSimplifier::GetMeshSimpSequence() |
---|
458 | { |
---|
459 | size_t j = 0; |
---|
460 | |
---|
461 | MeshSimplificationSequence::Step current_step; |
---|
462 | |
---|
463 | msimpsequence = new MeshSimplificationSequence(); |
---|
464 | |
---|
465 | // Copy the mesh simplification sequence. |
---|
466 | msimpsequence->mSteps = VMI::mSequence->mSteps; |
---|
467 | msimpsequence->mNewVertices = VMI::mSequence->mNewVertices; |
---|
468 | } |
---|
469 | |
---|
470 | //------------------------------------------------------------------------- |
---|
471 | // Initialize the mesh of VMI module. |
---|
472 | //------------------------------------------------------------------------- |
---|
473 | VMI::Mesh * ViewPointDrivenSimplifier::initMeshStructure(Mesh *geoMesh) |
---|
474 | { |
---|
475 | GLuint i, j, v1, v2, v3, n, e, t; |
---|
476 | VMI::Mesh *vmi_mesh; |
---|
477 | SubMesh *geosubmesh; |
---|
478 | VertexBuffer *vertex_buffer; |
---|
479 | int mesh_index_count; |
---|
480 | int index; |
---|
481 | |
---|
482 | // Reallocate memory for vmi mesh object. |
---|
483 | vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh)); |
---|
484 | |
---|
485 | if (vmi_mesh == NULL) |
---|
486 | { |
---|
487 | fprintf(stderr, "Error allocating memory\n"); |
---|
488 | exit(1); |
---|
489 | } |
---|
490 | |
---|
491 | // Shared vertex buffer. |
---|
492 | vertex_buffer = geoMesh->mVertexBuffer; |
---|
493 | |
---|
494 | // Reallocate memory for vertices of mesh. |
---|
495 | vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex) |
---|
496 | * |
---|
497 | vertex_buffer->mVertexCount); |
---|
498 | |
---|
499 | if (vmi_mesh->vertices == NULL) |
---|
500 | { |
---|
501 | fprintf(stderr, "Error allocating memory\n"); |
---|
502 | exit(1); |
---|
503 | } |
---|
504 | |
---|
505 | // Initialize mesh index count. |
---|
506 | mesh_index_count = 0; |
---|
507 | |
---|
508 | for ( unsigned int submesh = 0; |
---|
509 | submesh < geoMesh->mSubMeshCount; |
---|
510 | submesh++) |
---|
511 | { |
---|
512 | mesh_index_count += int(geoMesh->mSubMesh[submesh].mIndexCount); |
---|
513 | } |
---|
514 | |
---|
515 | // Reallocate memory for indices. |
---|
516 | vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle) |
---|
517 | * |
---|
518 | (mesh_index_count / 3)); |
---|
519 | |
---|
520 | if (vmi_mesh->triangles == NULL) |
---|
521 | { |
---|
522 | fprintf(stderr, "Error allocating memory\n"); |
---|
523 | exit(1); |
---|
524 | } |
---|
525 | |
---|
526 | printf("Adding vertices..."); |
---|
527 | |
---|
528 | // Fill up vertices. |
---|
529 | for (i = 0; i < vertex_buffer->mVertexCount; i++) |
---|
530 | { |
---|
531 | // Vertices start at 0. |
---|
532 | vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x; |
---|
533 | vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y; |
---|
534 | vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z; |
---|
535 | |
---|
536 | vmi_mesh->vertices[i].numTriangles = 0; |
---|
537 | vmi_mesh->vertices[i].triangles = NULL; |
---|
538 | vmi_mesh->vertices[i].enable = GL_TRUE; |
---|
539 | vmi_mesh->vertices[i].movable = GL_TRUE; |
---|
540 | } |
---|
541 | |
---|
542 | printf("Ok\n"); |
---|
543 | vmi_mesh->numVertices = int(vertex_buffer->mVertexCount); |
---|
544 | vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount); |
---|
545 | |
---|
546 | printf("Vertices: %d\n",vmi_mesh->numVertices); |
---|
547 | |
---|
548 | // Fill up triangles. |
---|
549 | printf("Adding triangles..."); |
---|
550 | |
---|
551 | // Initialize index of triangle. |
---|
552 | index = 0; |
---|
553 | |
---|
554 | // For each submesh. |
---|
555 | for ( unsigned int submesh = 0; |
---|
556 | submesh < geoMesh->mSubMeshCount; |
---|
557 | submesh++) |
---|
558 | { |
---|
559 | // Gets actual submesh. |
---|
560 | geosubmesh = &geoMesh->mSubMesh[submesh]; |
---|
561 | |
---|
562 | // For each three vertices. |
---|
563 | for (i = 0; i < (geosubmesh->mIndexCount / 3); i++) |
---|
564 | { |
---|
565 | vmi_mesh->triangles[index].id = index; |
---|
566 | vmi_mesh->triangles[index].submesh = submesh; |
---|
567 | vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)]; |
---|
568 | vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1]; |
---|
569 | vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2]; |
---|
570 | |
---|
571 | vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices, |
---|
572 | &vmi_mesh->triangles[index]); |
---|
573 | computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]); |
---|
574 | |
---|
575 | vmi_mesh->triangles[index].saliency = 0.0; |
---|
576 | |
---|
577 | vmi_mesh->triangles[index].enable = GL_TRUE; |
---|
578 | |
---|
579 | for (j = 0; j < 3; j++) |
---|
580 | { |
---|
581 | // Adding triangle index adjacent to 3 vertices. |
---|
582 | v1 = vmi_mesh->triangles[index].indices[j]; |
---|
583 | |
---|
584 | // Reallocate memory for the new adjacent triangle. |
---|
585 | vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); |
---|
586 | |
---|
587 | VMI::addItem( (int *)vmi_mesh->vertices[v1].triangles, |
---|
588 | (int *)&vmi_mesh->vertices[v1].numTriangles, |
---|
589 | index); |
---|
590 | } |
---|
591 | |
---|
592 | // Increments triangle count. |
---|
593 | index++; |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | printf("Ok\n"); |
---|
598 | |
---|
599 | vmi_mesh->numTriangles = mesh_index_count / 3; |
---|
600 | vmi_mesh->currentNumTriangles = mesh_index_count / 3; |
---|
601 | |
---|
602 | printf("Num Triangles: %d\n",vmi_mesh->numTriangles); |
---|
603 | |
---|
604 | // E = 3 T / 2 |
---|
605 | vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge) |
---|
606 | * |
---|
607 | vmi_mesh->numTriangles * 3); |
---|
608 | |
---|
609 | if (vmi_mesh->edges == NULL) |
---|
610 | { |
---|
611 | fprintf(stderr, "Error allocating memory\n"); |
---|
612 | exit(1); |
---|
613 | } |
---|
614 | |
---|
615 | printf("Adding edges..."); |
---|
616 | n = 0; |
---|
617 | |
---|
618 | // For each triangle adds three edges. |
---|
619 | for (i = 0; i < vmi_mesh->numTriangles; i++) |
---|
620 | { |
---|
621 | t = 0; |
---|
622 | v1 = vmi_mesh->triangles[i].indices[0]; |
---|
623 | v2 = vmi_mesh->triangles[i].indices[1]; |
---|
624 | v3 = vmi_mesh->triangles[i].indices[2]; |
---|
625 | |
---|
626 | if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1) |
---|
627 | { |
---|
628 | vmi_mesh->edges[n].u = v1; |
---|
629 | vmi_mesh->edges[n].v = v2; |
---|
630 | vmi_mesh->edges[n].triangles = NULL; |
---|
631 | vmi_mesh->edges[n].numTriangles = 0; |
---|
632 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
633 | |
---|
634 | // Reallocate memory for the new adjacent triangle |
---|
635 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
636 | |
---|
637 | // Adding triangle i adjacent to edge n |
---|
638 | VMI::addItem( (int *)vmi_mesh->edges[n].triangles, |
---|
639 | (int *)&vmi_mesh->edges[n].numTriangles, |
---|
640 | i); |
---|
641 | |
---|
642 | // Adding edge n adjacent to triangle i |
---|
643 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
644 | n++; |
---|
645 | } |
---|
646 | else |
---|
647 | { |
---|
648 | // Reallocate memory for the new adjacent triangle. |
---|
649 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
650 | |
---|
651 | // Adding triangle i adjacent to edge e |
---|
652 | VMI::addItem( (int *)vmi_mesh->edges[e].triangles, |
---|
653 | (int *)&vmi_mesh->edges[e].numTriangles, |
---|
654 | i); |
---|
655 | |
---|
656 | // Adding edge e adjacent to triangle i |
---|
657 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
658 | } |
---|
659 | |
---|
660 | if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1) |
---|
661 | { |
---|
662 | vmi_mesh->edges[n].u = v2; |
---|
663 | vmi_mesh->edges[n].v = v3; |
---|
664 | vmi_mesh->edges[n].triangles = NULL; |
---|
665 | vmi_mesh->edges[n].numTriangles = 0; |
---|
666 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
667 | |
---|
668 | // Reallocate memory for the new adjacent triangle |
---|
669 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
670 | |
---|
671 | // Adding triangle i adjacent to edge n |
---|
672 | VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); |
---|
673 | vmi_mesh->edges[n].triangles = NULL; |
---|
674 | vmi_mesh->edges[n].numTriangles = 0; |
---|
675 | |
---|
676 | // Adding edge n adjacent to triangle i |
---|
677 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
678 | n++; |
---|
679 | } |
---|
680 | else |
---|
681 | { |
---|
682 | // Reallocate memory for the new adjacent triangle |
---|
683 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
684 | |
---|
685 | // Adding triangle i adjacent to edge e |
---|
686 | VMI::addItem( (int *)vmi_mesh->edges[e].triangles, |
---|
687 | (int *)&vmi_mesh->edges[e].numTriangles, |
---|
688 | i); |
---|
689 | |
---|
690 | // Adding edge e adjacent to triangle i |
---|
691 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
692 | } |
---|
693 | |
---|
694 | if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1) |
---|
695 | { |
---|
696 | vmi_mesh->edges[n].u = v3; |
---|
697 | vmi_mesh->edges[n].v = v1; |
---|
698 | vmi_mesh->edges[n].triangles = NULL; |
---|
699 | vmi_mesh->edges[n].numTriangles = 0; |
---|
700 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
701 | |
---|
702 | // Reallocate memory for the new adjacent triangle. |
---|
703 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
704 | |
---|
705 | // Adding triangle i adjacent to edge n |
---|
706 | VMI::addItem( (int *)vmi_mesh->edges[n].triangles, |
---|
707 | (int *)&vmi_mesh->edges[n].numTriangles, |
---|
708 | i); |
---|
709 | |
---|
710 | // Adding edge n adjacent to triangle i |
---|
711 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
712 | n++; |
---|
713 | } |
---|
714 | else |
---|
715 | { |
---|
716 | // Reallocate memory for the new adjacent triangle |
---|
717 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
718 | |
---|
719 | // Adding triangle i adjacent to edge e |
---|
720 | VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); |
---|
721 | |
---|
722 | // Adding edge e adjacent to triangle i |
---|
723 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
724 | } |
---|
725 | } |
---|
726 | |
---|
727 | printf("Ok\n"); |
---|
728 | vmi_mesh->numEdges = n; |
---|
729 | |
---|
730 | // Creates vertex multimap. |
---|
731 | initVertexMultimap(vmi_mesh,mTexConserver->mVertices); |
---|
732 | |
---|
733 | return vmi_mesh; |
---|
734 | } |
---|
735 | |
---|
736 | //------------------------------------------------------------------------- |
---|
737 | // Gets the geometry mesh of a the vmi mesh given. |
---|
738 | //------------------------------------------------------------------------- |
---|
739 | void ViewPointDrivenSimplifier::loadMesh() |
---|
740 | { |
---|
741 | int num_indices; |
---|
742 | SubMesh *geosubmesh; |
---|
743 | VertexBuffer *vertex_buffer; |
---|
744 | |
---|
745 | // Gets old vertex buffer. |
---|
746 | vertex_buffer = mGeoMesh->mVertexBuffer; |
---|
747 | |
---|
748 | // Initialize auxiliar vertex buffer. |
---|
749 | mVB = new VertexBuffer(); |
---|
750 | |
---|
751 | mVB->mPosition = new Vector3[VMI::mesh->currentNumVertices]; |
---|
752 | mVB->mNormal = new Vector3[VMI::mesh->currentNumVertices]; |
---|
753 | mVB->mTexCoords = new Vector2[VMI::mesh->currentNumVertices]; |
---|
754 | |
---|
755 | mVB->mVertexInfo = vertex_buffer->mVertexInfo; |
---|
756 | |
---|
757 | // For each submesh. |
---|
758 | for (size_t submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++) |
---|
759 | { |
---|
760 | geosubmesh = &mGeoMesh->mSubMesh[submesh]; |
---|
761 | |
---|
762 | delete []geosubmesh->mIndex; |
---|
763 | |
---|
764 | // Initialize submesh index count; |
---|
765 | num_indices = 0; |
---|
766 | |
---|
767 | // For each triangle. |
---|
768 | for (size_t i = 0; i < VMI::mesh->numTriangles; i++) |
---|
769 | { |
---|
770 | // If is enable and of the current submesh. |
---|
771 | if ((VMI::mesh->triangles[i].enable) |
---|
772 | && |
---|
773 | (VMI::mesh->triangles[i].submesh == submesh)) |
---|
774 | { |
---|
775 | // Increments submesh index count. |
---|
776 | num_indices += 3; |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | geosubmesh->mIndexCount = num_indices; |
---|
781 | |
---|
782 | geosubmesh->mIndex = new Geometry::Index[geosubmesh->mIndexCount]; |
---|
783 | |
---|
784 | // Initialize number of indices. |
---|
785 | num_indices = 0; |
---|
786 | |
---|
787 | // Fill up indices. |
---|
788 | for (size_t i = 0; i < VMI::mesh->numTriangles; i++) |
---|
789 | { |
---|
790 | if ((VMI::mesh->triangles[i].enable) |
---|
791 | && |
---|
792 | (VMI::mesh->triangles[i].submesh == submesh)) |
---|
793 | { |
---|
794 | geosubmesh->mIndex[num_indices++] = |
---|
795 | VMI::mesh->triangles[i].indices[0]; |
---|
796 | |
---|
797 | geosubmesh->mIndex[num_indices++] = |
---|
798 | VMI::mesh->triangles[i].indices[1]; |
---|
799 | |
---|
800 | geosubmesh->mIndex[num_indices++] = |
---|
801 | VMI::mesh->triangles[i].indices[2]; |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | // For each index. |
---|
806 | for (size_t i = 0; i < geosubmesh->mIndexCount; i++) |
---|
807 | { |
---|
808 | findVertex(submesh,i); |
---|
809 | } |
---|
810 | |
---|
811 | geosubmesh->mVertexBuffer = mVB; |
---|
812 | } |
---|
813 | |
---|
814 | delete vertex_buffer; |
---|
815 | |
---|
816 | mGeoMesh->mVertexBuffer = mVB; |
---|
817 | } |
---|
818 | |
---|
819 | //------------------------------------------------------------------------- |
---|
820 | // Find vertex in auxiliar vertex buffer. |
---|
821 | //------------------------------------------------------------------------- |
---|
822 | void ViewPointDrivenSimplifier::findVertex(size_t submesh, size_t elem) |
---|
823 | { |
---|
824 | bool found; |
---|
825 | int index; |
---|
826 | unsigned int i; |
---|
827 | int new_elem; |
---|
828 | VertexBuffer *vertex_buffer; |
---|
829 | map<int,int>::iterator it; |
---|
830 | |
---|
831 | found = false; |
---|
832 | |
---|
833 | // Shared vertex buffer. |
---|
834 | vertex_buffer = mInitialMesh->mVertexBuffer; |
---|
835 | |
---|
836 | index = mGeoMesh->mSubMesh[submesh].mIndex[elem]; |
---|
837 | |
---|
838 | i = 0; |
---|
839 | |
---|
840 | if ((it = mIndexMap.find(index)) != mIndexMap.end()) |
---|
841 | { |
---|
842 | mGeoMesh->mSubMesh[submesh].mIndex[elem] = (*it).second; |
---|
843 | } |
---|
844 | else |
---|
845 | { |
---|
846 | mIndexMap[index] = int(mVB->mVertexCount); |
---|
847 | |
---|
848 | // Last element. |
---|
849 | new_elem = int(mVB->mVertexCount); |
---|
850 | |
---|
851 | mVB->mPosition[new_elem] = VMI::vPositions[index]; |
---|
852 | mVB->mNormal[new_elem] = VMI::vNormals[index]; |
---|
853 | mVB->mTexCoords[new_elem] = VMI::vTexCoords[index]; |
---|
854 | |
---|
855 | // Update index. |
---|
856 | mGeoMesh->mSubMesh[submesh].mIndex[elem] = new_elem; |
---|
857 | |
---|
858 | // Increments vertex count. |
---|
859 | mVB->mVertexCount++; |
---|
860 | } |
---|
861 | } |
---|
862 | |
---|
863 | //------------------------------------------------------------------------- |
---|
864 | // Reassigns bones. |
---|
865 | //------------------------------------------------------------------------- |
---|
866 | void ViewPointDrivenSimplifier::bonesReassignament() |
---|
867 | { |
---|
868 | size_t vertex_id; |
---|
869 | size_t bones_count; |
---|
870 | bool vertex_found; |
---|
871 | map<int,int>::iterator im; |
---|
872 | VertexBoneAssignment bone; |
---|
873 | |
---|
874 | vector<VertexBoneAssignment>::iterator ib; |
---|
875 | |
---|
876 | // Copy new vertices. |
---|
877 | for (unsigned int i = 0; i < msimpsequence->mNewVertices.size(); i++) |
---|
878 | { |
---|
879 | vertex_id = msimpsequence->mNewVertices[i].id; |
---|
880 | |
---|
881 | // Initialize number of bones. |
---|
882 | bones_count = mInitialMesh->mBones.size(); |
---|
883 | |
---|
884 | // check if my twin-vertex-bone has a bone assignment |
---|
885 | // we check only the GeoMesh bones because the lodstrips |
---|
886 | // only works for sharedvertex bones. |
---|
887 | for (int j = 0; j < bones_count; j++) |
---|
888 | { |
---|
889 | ib = mInitialMesh->mBones.begin() + j; |
---|
890 | |
---|
891 | if (ib->vertexIndex == msimpsequence->mNewVertices[i].bonefrom) |
---|
892 | { |
---|
893 | bone.vertexIndex = vertex_id; |
---|
894 | bone.boneIndex = ib->boneIndex; |
---|
895 | bone.weight = ib->weight; |
---|
896 | |
---|
897 | mInitialMesh->mBones.push_back(bone); |
---|
898 | bones_count++; |
---|
899 | } |
---|
900 | } |
---|
901 | } |
---|
902 | |
---|
903 | // Clears bones. |
---|
904 | mGeoMesh->mBones.clear(); |
---|
905 | |
---|
906 | // For each bone assignment. |
---|
907 | for (unsigned int i = 0; i < mInitialMesh->mBones.size(); i++) |
---|
908 | { |
---|
909 | bone.vertexIndex = mInitialMesh->mBones[i].vertexIndex; |
---|
910 | bone.boneIndex = mInitialMesh->mBones[i].boneIndex; |
---|
911 | bone.weight = mInitialMesh->mBones[i].weight; |
---|
912 | |
---|
913 | vertex_found = false; |
---|
914 | |
---|
915 | // If the vertex is found in the simplification model. |
---|
916 | if ((im = mIndexMap.find(bone.vertexIndex)) |
---|
917 | != |
---|
918 | mIndexMap.end()) |
---|
919 | { |
---|
920 | bone.vertexIndex = (*im).second; |
---|
921 | |
---|
922 | mGeoMesh->mBones.push_back(bone); |
---|
923 | vertex_found = true; |
---|
924 | } |
---|
925 | } |
---|
926 | } |
---|
927 | |
---|
928 | //------------------------------------------------------------------------- |
---|
929 | // Fill up position, normal and texture coordinates. |
---|
930 | //------------------------------------------------------------------------- |
---|
931 | void ViewPointDrivenSimplifier::fillUpPosNorTC(Mesh *geoMesh) |
---|
932 | { |
---|
933 | VertexBuffer *vertex_buffer; |
---|
934 | Vector3 v3; |
---|
935 | Vector2 v2; |
---|
936 | |
---|
937 | vertex_buffer = geoMesh->mVertexBuffer; |
---|
938 | |
---|
939 | for (size_t i = 0; i < vertex_buffer->mVertexCount; i++) |
---|
940 | { |
---|
941 | v3 = vertex_buffer->mPosition[i]; |
---|
942 | |
---|
943 | vPositions.push_back(v3); |
---|
944 | |
---|
945 | v3 = vertex_buffer->mNormal[i]; |
---|
946 | |
---|
947 | vNormals.push_back(v3); |
---|
948 | |
---|
949 | v2 = vertex_buffer->mTexCoords[i]; |
---|
950 | |
---|
951 | vTexCoords.push_back(v2); |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | //------------------------------------------------------------------------- |
---|
956 | // EdgesMultimap class |
---|
957 | //------------------------------------------------------------------------- |
---|
958 | |
---|
959 | //------------------------------------------------------------------------- |
---|
960 | // Constructor. |
---|
961 | //------------------------------------------------------------------------- |
---|
962 | EdgesMultimap::EdgesMultimap() |
---|
963 | { |
---|
964 | edges.clear(); |
---|
965 | } |
---|
966 | |
---|
967 | //------------------------------------------------------------------------- |
---|
968 | // Insert and edge. |
---|
969 | //------------------------------------------------------------------------- |
---|
970 | void EdgesMultimap::insert(int v1,int v2) |
---|
971 | { |
---|
972 | edges.insert(pair<int,int>(v1,v2)); |
---|
973 | edges.insert(pair<int,int>(v2,v1)); |
---|
974 | } |
---|
975 | |
---|
976 | //------------------------------------------------------------------------- |
---|
977 | // Remove edges with v1 and v2. |
---|
978 | //------------------------------------------------------------------------- |
---|
979 | void EdgesMultimap::remove(int v1,int v2) |
---|
980 | { |
---|
981 | multimap<int,int>::iterator lb; |
---|
982 | multimap<int,int>::iterator ub; |
---|
983 | |
---|
984 | if (edges.find(v1) != edges.end()) |
---|
985 | { |
---|
986 | lb = edges.lower_bound(v1); |
---|
987 | ub = edges.upper_bound(v1); |
---|
988 | |
---|
989 | while (lb != ub) |
---|
990 | { |
---|
991 | if ((*lb).second == v2) |
---|
992 | { |
---|
993 | // Remove edge. |
---|
994 | edges.erase(lb); |
---|
995 | |
---|
996 | lb = ub; |
---|
997 | } |
---|
998 | else |
---|
999 | { |
---|
1000 | lb++; |
---|
1001 | } |
---|
1002 | } |
---|
1003 | } |
---|
1004 | |
---|
1005 | if (edges.find(v2) != edges.end()) |
---|
1006 | { |
---|
1007 | lb = edges.lower_bound(v2); |
---|
1008 | ub = edges.upper_bound(v2); |
---|
1009 | |
---|
1010 | while (lb != ub) |
---|
1011 | { |
---|
1012 | if ((*lb).second == v1) |
---|
1013 | { |
---|
1014 | // Remove edge. |
---|
1015 | edges.erase(lb); |
---|
1016 | |
---|
1017 | lb = ub; |
---|
1018 | } |
---|
1019 | else |
---|
1020 | { |
---|
1021 | lb++; |
---|
1022 | } |
---|
1023 | } |
---|
1024 | } |
---|
1025 | |
---|
1026 | } |
---|
1027 | |
---|
1028 | //------------------------------------------------------------------------- |
---|
1029 | // Checks if the edge (v1,v2) exists. |
---|
1030 | //------------------------------------------------------------------------- |
---|
1031 | bool EdgesMultimap::exists(int v1,int v2) |
---|
1032 | { |
---|
1033 | bool found; |
---|
1034 | multimap<int,int>::iterator lb; |
---|
1035 | multimap<int,int>::iterator ub; |
---|
1036 | |
---|
1037 | found = false; |
---|
1038 | |
---|
1039 | // Find range. |
---|
1040 | lb = edges.lower_bound(v1); |
---|
1041 | ub = edges.upper_bound(v1); |
---|
1042 | |
---|
1043 | // Search for all v1 edges. |
---|
1044 | while (lb != ub) |
---|
1045 | { |
---|
1046 | // If edge is found. |
---|
1047 | if ((*lb).second = v2) |
---|
1048 | { |
---|
1049 | found = true; |
---|
1050 | lb = ub; |
---|
1051 | } |
---|
1052 | else |
---|
1053 | { |
---|
1054 | // Next iteration. |
---|
1055 | lb++; |
---|
1056 | } |
---|
1057 | } |
---|
1058 | |
---|
1059 | return found; |
---|
1060 | } |
---|
1061 | |
---|
1062 | //------------------------------------------------------------------------- |
---|
1063 | // Change vertex v1 to v2 in the map. |
---|
1064 | //------------------------------------------------------------------------- |
---|
1065 | void EdgesMultimap::contract(int v1,int v2) |
---|
1066 | { |
---|
1067 | multimap<int,int>::iterator it; |
---|
1068 | int v_aux; |
---|
1069 | |
---|
1070 | // Remove contracted edge. |
---|
1071 | remove(v1,v2); |
---|
1072 | |
---|
1073 | // Modify all edges where appears v1 to v2. |
---|
1074 | while ((it = edges.find(v1)) != edges.end()) |
---|
1075 | { |
---|
1076 | v_aux = (*it).second; |
---|
1077 | |
---|
1078 | // Remove current edge. |
---|
1079 | remove(v1,v_aux); |
---|
1080 | |
---|
1081 | // Modify edge. |
---|
1082 | insert(v2,v_aux); |
---|
1083 | } |
---|
1084 | } |
---|
1085 | |
---|
1086 | //------------------------------------------------------------------------- |
---|
1087 | // TextureConserver class |
---|
1088 | //------------------------------------------------------------------------- |
---|
1089 | TextureConserver::TextureConserver() |
---|
1090 | { |
---|
1091 | mEdges = new EdgesMultimap(); |
---|
1092 | mGeoMesh = new Mesh(); |
---|
1093 | } |
---|
1094 | |
---|
1095 | TextureConserver::~TextureConserver() |
---|
1096 | { |
---|
1097 | delete mGeoMesh; |
---|
1098 | } |
---|
1099 | |
---|
1100 | //------------------------------------------------------------------------- |
---|
1101 | // Join twin vertices into a unique vertex. |
---|
1102 | //------------------------------------------------------------------------- |
---|
1103 | void TextureConserver::JoinVertices(Mesh *mesh) |
---|
1104 | { |
---|
1105 | map<_coord_, int> uniquevertices; |
---|
1106 | map<int, int> newindices; |
---|
1107 | multimap<int, int>::iterator it; |
---|
1108 | map<int, int>::iterator it_map; |
---|
1109 | VertexBuffer *vertex_buffer; |
---|
1110 | VertexBuffer *mGMVB; |
---|
1111 | unsigned int vertex_count; |
---|
1112 | unsigned int i; |
---|
1113 | unsigned int aux_i; |
---|
1114 | unsigned int submesh; |
---|
1115 | SubMesh *geosubmesh; |
---|
1116 | _coord_ vertex_aux; |
---|
1117 | |
---|
1118 | // Copy actual mesh. |
---|
1119 | *mGeoMesh = *mesh; |
---|
1120 | |
---|
1121 | // Gets the shared vertex buffer. |
---|
1122 | vertex_buffer = mGeoMesh->mVertexBuffer; |
---|
1123 | |
---|
1124 | // Initialize unique vertices count. |
---|
1125 | vertex_count = 0; |
---|
1126 | |
---|
1127 | // Fill up vertices. |
---|
1128 | for (i = 0; i < vertex_buffer->mVertexCount; i++) |
---|
1129 | { |
---|
1130 | vertex_aux.x = vertex_buffer->mPosition[i].x; |
---|
1131 | vertex_aux.y = vertex_buffer->mPosition[i].y; |
---|
1132 | vertex_aux.z = vertex_buffer->mPosition[i].z; |
---|
1133 | |
---|
1134 | // New index. |
---|
1135 | if (uniquevertices.find(vertex_aux) == uniquevertices.end()) |
---|
1136 | { |
---|
1137 | uniquevertices[vertex_aux] = vertex_count; |
---|
1138 | newindices[i] = vertex_count; |
---|
1139 | |
---|
1140 | mVertices.insert(pair<int,int>(vertex_count,i)); |
---|
1141 | |
---|
1142 | // Increments unique vertices count. |
---|
1143 | vertex_count++; |
---|
1144 | } |
---|
1145 | // The map of unique vertices already contains this vertex. |
---|
1146 | else |
---|
1147 | { |
---|
1148 | int newindex = uniquevertices[vertex_aux]; |
---|
1149 | newindices[i] = newindex; |
---|
1150 | |
---|
1151 | mVertices.insert(pair<int,int>(newindex,i)); |
---|
1152 | } |
---|
1153 | } |
---|
1154 | |
---|
1155 | // Delete vertices. |
---|
1156 | delete mesh->mVertexBuffer; |
---|
1157 | |
---|
1158 | mesh->mVertexBuffer = new VertexBuffer(); |
---|
1159 | |
---|
1160 | mesh->mVertexBuffer->mPosition = new Vector3[vertex_count]; |
---|
1161 | mesh->mVertexBuffer->mNormal = new Vector3[vertex_count]; |
---|
1162 | mesh->mVertexBuffer->mTexCoords = new Vector2[vertex_count]; |
---|
1163 | |
---|
1164 | mesh->mVertexBuffer->mVertexInfo = vertex_buffer->mVertexInfo; |
---|
1165 | mesh->mVertexBuffer->mVertexCount = vertex_count; |
---|
1166 | |
---|
1167 | mGMVB = mesh->mVertexBuffer; |
---|
1168 | |
---|
1169 | // Fill up unique vertices. |
---|
1170 | for (i = 0; i < vertex_count; i++) |
---|
1171 | { |
---|
1172 | // Find first ocurrence of unique vertex. |
---|
1173 | it = mVertices.find(i); |
---|
1174 | |
---|
1175 | aux_i = (*it).second; |
---|
1176 | |
---|
1177 | mGMVB->mPosition[i].x = vertex_buffer->mPosition[aux_i].x; |
---|
1178 | mGMVB->mPosition[i].y = vertex_buffer->mPosition[aux_i].y; |
---|
1179 | mGMVB->mPosition[i].z = vertex_buffer->mPosition[aux_i].z; |
---|
1180 | |
---|
1181 | mGMVB->mNormal[i].x = vertex_buffer->mNormal[aux_i].x; |
---|
1182 | mGMVB->mNormal[i].y = vertex_buffer->mNormal[aux_i].y; |
---|
1183 | mGMVB->mNormal[i].z = vertex_buffer->mNormal[aux_i].z; |
---|
1184 | |
---|
1185 | mGMVB->mTexCoords[i].x = vertex_buffer->mTexCoords[aux_i].x; |
---|
1186 | mGMVB->mTexCoords[i].y = vertex_buffer->mTexCoords[aux_i].y; |
---|
1187 | } |
---|
1188 | |
---|
1189 | // Fill up indices to unique vertices. |
---|
1190 | // For each submesh. |
---|
1191 | for (submesh = 0; submesh < mesh->mSubMeshCount; submesh++) |
---|
1192 | { |
---|
1193 | geosubmesh = &mesh->mSubMesh[submesh]; |
---|
1194 | |
---|
1195 | // Change indices. |
---|
1196 | for (i = 0; i < geosubmesh->mIndexCount; i++) |
---|
1197 | { |
---|
1198 | // Unique index asociated to the given one. |
---|
1199 | it_map = newindices.find(geosubmesh->mIndex[i]); |
---|
1200 | |
---|
1201 | geosubmesh->mIndex[i] = (*it_map).second; |
---|
1202 | } |
---|
1203 | } |
---|
1204 | } |
---|
1205 | |
---|
1206 | //------------------------------------------------------------------------- |
---|
1207 | // Modify the joined vertices sequento to an split. |
---|
1208 | //------------------------------------------------------------------------- |
---|
1209 | MeshSimplificationSequence * |
---|
1210 | TextureConserver::WriteRealSimpSeq(MeshSimplificationSequence *simpseq) |
---|
1211 | { |
---|
1212 | bool edge_found; |
---|
1213 | |
---|
1214 | size_t v0; |
---|
1215 | size_t v1; |
---|
1216 | size_t vertex_count; |
---|
1217 | size_t i,j; |
---|
1218 | |
---|
1219 | VertexBuffer *mVB; |
---|
1220 | GeoVertex new_vertex; |
---|
1221 | |
---|
1222 | multimap<int, int>::iterator lb0; |
---|
1223 | multimap<int, int>::iterator ub0; |
---|
1224 | multimap<int, int>::iterator lb1; |
---|
1225 | multimap<int, int>::iterator ub1; |
---|
1226 | multimap<int, int>::iterator it0; |
---|
1227 | |
---|
1228 | MeshSimplificationSequence *real_seq; |
---|
1229 | MeshSimplificationSequence::Step current_step; |
---|
1230 | |
---|
1231 | // Initialize vertex count. |
---|
1232 | vertex_count = mGeoMesh->mVertexBuffer->mVertexCount; |
---|
1233 | |
---|
1234 | // Creates new simplification sequence. |
---|
1235 | real_seq = new MeshSimplificationSequence(); |
---|
1236 | |
---|
1237 | // For each simplification step. |
---|
1238 | for (i = 0; i < simpseq->mSteps.size(); i++) |
---|
1239 | { |
---|
1240 | lb0 = mVertices.lower_bound(simpseq->mSteps[i].mV0); |
---|
1241 | ub0 = mVertices.upper_bound(simpseq->mSteps[i].mV0); |
---|
1242 | lb1 = mVertices.lower_bound(simpseq->mSteps[i].mV1); |
---|
1243 | ub1 = mVertices.upper_bound(simpseq->mSteps[i].mV1); |
---|
1244 | |
---|
1245 | // Removed vertex. |
---|
1246 | while (lb1 != ub1) |
---|
1247 | { |
---|
1248 | // Real index. |
---|
1249 | v1 = (*lb1).second; |
---|
1250 | |
---|
1251 | // Begin of iteration v0. |
---|
1252 | it0 = lb0; |
---|
1253 | |
---|
1254 | edge_found = false; |
---|
1255 | |
---|
1256 | // Remaining vertex. |
---|
1257 | while (it0 != ub0) |
---|
1258 | { |
---|
1259 | // Real index. |
---|
1260 | v0 = (*it0).second; |
---|
1261 | |
---|
1262 | // If edge exists. |
---|
1263 | if (mEdges->exists(v0,v1)) |
---|
1264 | { |
---|
1265 | mEdges->contract(v1,v0); |
---|
1266 | |
---|
1267 | // Simplification sequence. |
---|
1268 | current_step.mV0 = v0; |
---|
1269 | current_step.mV1 = v1; |
---|
1270 | |
---|
1271 | real_seq->mSteps.push_back(current_step); |
---|
1272 | |
---|
1273 | edge_found = true; |
---|
1274 | } |
---|
1275 | |
---|
1276 | it0++; |
---|
1277 | } |
---|
1278 | |
---|
1279 | // If not found a valid edge. |
---|
1280 | if (!edge_found) |
---|
1281 | { |
---|
1282 | // Id new vertex. |
---|
1283 | new_vertex.id = vertex_count; |
---|
1284 | new_vertex.bonefrom = v0; |
---|
1285 | |
---|
1286 | // Simplification sequence. |
---|
1287 | current_step.mV0 = new_vertex.id; |
---|
1288 | current_step.mV1 = v1; |
---|
1289 | |
---|
1290 | real_seq->mSteps.push_back(current_step); |
---|
1291 | |
---|
1292 | // Contracts lonely vertex. |
---|
1293 | mEdges->contract(v1,new_vertex.id); |
---|
1294 | |
---|
1295 | // New vertex adquires position of v0. |
---|
1296 | new_vertex.position.x = mGeoMesh->mVertexBuffer->mPosition[v0].x; |
---|
1297 | new_vertex.position.y = mGeoMesh->mVertexBuffer->mPosition[v0].y; |
---|
1298 | new_vertex.position.z = mGeoMesh->mVertexBuffer->mPosition[v0].z; |
---|
1299 | |
---|
1300 | // New vertex adquires normal of lonely one. |
---|
1301 | new_vertex.normal.x = mGeoMesh->mVertexBuffer->mNormal[v1].x; |
---|
1302 | new_vertex.normal.y = mGeoMesh->mVertexBuffer->mNormal[v1].y; |
---|
1303 | new_vertex.normal.z = mGeoMesh->mVertexBuffer->mNormal[v1].z; |
---|
1304 | |
---|
1305 | // New vertex adquires the texture cood of the lonely one. |
---|
1306 | new_vertex.texcoord.x = mGeoMesh->mVertexBuffer->mTexCoords[v1].x; |
---|
1307 | new_vertex.texcoord.y = mGeoMesh->mVertexBuffer->mTexCoords[v1].y; |
---|
1308 | |
---|
1309 | // New vertex stored. |
---|
1310 | real_seq->mNewVertices.push_back(new_vertex); |
---|
1311 | |
---|
1312 | // New vertex added. |
---|
1313 | vertex_count++; |
---|
1314 | } |
---|
1315 | |
---|
1316 | lb1++; |
---|
1317 | } |
---|
1318 | } |
---|
1319 | |
---|
1320 | // Assigns new simplification sequence. |
---|
1321 | delete simpseq; |
---|
1322 | |
---|
1323 | return real_seq; |
---|
1324 | /* |
---|
1325 | // New vertex buffer to add new vertices. |
---|
1326 | mVB = new VertexBuffer(); |
---|
1327 | |
---|
1328 | mVB->mPosition = new Vector3[vertex_count]; |
---|
1329 | mVB->mNormal = new Vector3[vertex_count]; |
---|
1330 | mVB->mTexCoords = new Vector2[vertex_count]; |
---|
1331 | |
---|
1332 | mVB->mVertexInfo = mGeoMesh->mVertexBuffer->mVertexInfo; |
---|
1333 | |
---|
1334 | // Copy original vertices. |
---|
1335 | for (i = 0; i < mGeoMesh->mVertexBuffer->mVertexCount; i++) |
---|
1336 | { |
---|
1337 | mVB->mPosition[i].x = mGeoMesh->mVertexBuffer->mPosition[i].x; |
---|
1338 | mVB->mPosition[i].y = mGeoMesh->mVertexBuffer->mPosition[i].y; |
---|
1339 | mVB->mPosition[i].z = mGeoMesh->mVertexBuffer->mPosition[i].z; |
---|
1340 | |
---|
1341 | mVB->mNormal[i].x = mGeoMesh->mVertexBuffer->mNormal[i].x; |
---|
1342 | mVB->mNormal[i].y = mGeoMesh->mVertexBuffer->mNormal[i].y; |
---|
1343 | mVB->mNormal[i].z = mGeoMesh->mVertexBuffer->mNormal[i].z; |
---|
1344 | |
---|
1345 | mVB->mTexCoords[i].x = mGeoMesh->mVertexBuffer->mTexCoords[i].x; |
---|
1346 | mVB->mTexCoords[i].y = mGeoMesh->mVertexBuffer->mTexCoords[i].y; |
---|
1347 | } |
---|
1348 | |
---|
1349 | // Add new vertices. |
---|
1350 | j = mGeoMesh->mVertexBuffer->mVertexCount; |
---|
1351 | |
---|
1352 | for (i = 0; i < new_vertices.size(); i++) |
---|
1353 | { |
---|
1354 | mVB->mPosition[j].x = new_vertices[i].position.x; |
---|
1355 | mVB->mPosition[j].y = new_vertices[i].position.y; |
---|
1356 | mVB->mPosition[j].z = new_vertices[i].position.z; |
---|
1357 | |
---|
1358 | mVB->mNormal[j].x = new_vertices[i].normal.x; |
---|
1359 | mVB->mNormal[j].y = new_vertices[i].normal.y; |
---|
1360 | mVB->mNormal[j].z = new_vertices[i].normal.z; |
---|
1361 | |
---|
1362 | mVB->mTexCoords[j].x = new_vertices[i].texcoord.x; |
---|
1363 | mVB->mTexCoords[j].y = new_vertices[i].texcoord.y; |
---|
1364 | } |
---|
1365 | |
---|
1366 | // Free memory. |
---|
1367 | delete []mGeoMesh->mVertexBuffer; |
---|
1368 | |
---|
1369 | // Reassigns main vertex buffer with new vertices. |
---|
1370 | mGeoMesh->mVertexBuffer = mVB; |
---|
1371 | |
---|
1372 | // Reassign submesh vertex buffers. |
---|
1373 | for (i = 0; i < mGeoMesh->mSubMeshCount; i++) |
---|
1374 | { |
---|
1375 | mGeoMesh->mSubMesh[i].mVertexBuffer = mVB; |
---|
1376 | } |
---|
1377 | */ |
---|
1378 | } |
---|
1379 | |
---|
1380 | //------------------------------------------------------------------------- |
---|
1381 | // Return mesh that may have repeated vertices for texture aparence. |
---|
1382 | //------------------------------------------------------------------------- |
---|
1383 | Mesh *TextureConserver::GetMesh() |
---|
1384 | { |
---|
1385 | return mGeoMesh; |
---|
1386 | } |
---|
1387 | |
---|