1 | /* ==========================================================================
|
---|
2 | * (C) 2005 Universitat Jaume I
|
---|
3 | * ==========================================================================
|
---|
4 | * PROYECT: GAME TOOLS
|
---|
5 | * ==========================================================================*/
|
---|
6 | /** CONTENT:
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * @file GeoMeshSaver.cpp
|
---|
10 | /*===========================================================================*/
|
---|
11 | #include <GeoMeshSaver.h>
|
---|
12 |
|
---|
13 | using namespace Geometry;
|
---|
14 | using namespace std;
|
---|
15 |
|
---|
16 | //------------------------------------------
|
---|
17 | // Public:
|
---|
18 | //------------------------------------------
|
---|
19 |
|
---|
20 | ////////////////////
|
---|
21 | // Constructors. //
|
---|
22 | ////////////////////
|
---|
23 | GeoMeshSaver::GeoMeshSaver()
|
---|
24 | {
|
---|
25 | }
|
---|
26 |
|
---|
27 | //////////////////
|
---|
28 | // Destroyer. //
|
---|
29 | //////////////////
|
---|
30 | GeoMeshSaver::~GeoMeshSaver()
|
---|
31 | {
|
---|
32 | delete mGeoMesh;
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Saves a Mesh into a file.
|
---|
36 | int GeoMeshSaver::save(Mesh *geoMesh, const char *fileNameMesh)
|
---|
37 | {
|
---|
38 | int size;
|
---|
39 | String name(fileNameMesh);
|
---|
40 |
|
---|
41 | mGeoMesh = new Mesh();
|
---|
42 |
|
---|
43 | // Set the mesh.
|
---|
44 | *mGeoMesh = *geoMesh;
|
---|
45 |
|
---|
46 | // Debug.
|
---|
47 | cout << endl
|
---|
48 | << endl
|
---|
49 | << "-----------------------------------"
|
---|
50 | << endl
|
---|
51 | << "\t SAVE MESH"
|
---|
52 | << endl
|
---|
53 | << "-----------------------------------"
|
---|
54 | << endl
|
---|
55 | << endl;
|
---|
56 |
|
---|
57 | // Unnormalize geometry model.
|
---|
58 | unnormalizeModel(mGeoMesh);
|
---|
59 |
|
---|
60 | // Open the file.
|
---|
61 | mSerializer = new Serializer( name,
|
---|
62 | Serializer::WRITE);
|
---|
63 |
|
---|
64 |
|
---|
65 | // Write the file header.
|
---|
66 | writeFileHeader();
|
---|
67 |
|
---|
68 | // Write the mesh data.
|
---|
69 | writeMesh(mGeoMesh);
|
---|
70 |
|
---|
71 | size = mSerializer->GetSize();
|
---|
72 |
|
---|
73 | // Close the file.
|
---|
74 | delete mSerializer;
|
---|
75 |
|
---|
76 | // Return the number of bytes written.
|
---|
77 | return size;
|
---|
78 | }
|
---|
79 |
|
---|
80 | //------------------------------------------
|
---|
81 | // Private:
|
---|
82 | //------------------------------------------
|
---|
83 |
|
---|
84 | // Write the main mesh.
|
---|
85 | void GeoMeshSaver::writeMesh(Mesh *geoMesh)
|
---|
86 | {
|
---|
87 | // Header
|
---|
88 | writeChunkHeader(M_MESH, calcMeshSize(geoMesh));
|
---|
89 |
|
---|
90 | // Debug.
|
---|
91 | cout << " M_MESH"
|
---|
92 | << endl;
|
---|
93 |
|
---|
94 | // Write the skeletally animated flag.
|
---|
95 | writeBools(geoMesh->hasSkeleton,1);
|
---|
96 |
|
---|
97 | // Write shared geometry.
|
---|
98 | if (geoMesh->mVertexBuffer->mVertexCount > 0)
|
---|
99 | {
|
---|
100 | writeGeometry(geoMesh->mVertexBuffer);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // Write submeshes.
|
---|
104 | for (int i = 0; i < geoMesh->mSubMeshCount; i++)
|
---|
105 | {
|
---|
106 | writeSubMesh(&geoMesh->mSubMesh[i]);
|
---|
107 | }
|
---|
108 |
|
---|
109 | //OSCAR
|
---|
110 | // Write skeleton info if required
|
---|
111 | if (geoMesh->hasSkeleton)
|
---|
112 | {
|
---|
113 | // Write skeleton link
|
---|
114 | writeSkeletonLink(geoMesh->mSkeletonName);
|
---|
115 |
|
---|
116 | // Write bone assignments
|
---|
117 | if (!geoMesh->mBones.empty())
|
---|
118 | {
|
---|
119 |
|
---|
120 | for (int i=0; i< geoMesh->mBones.size(); i++)
|
---|
121 | {
|
---|
122 | writeMeshBoneAssignment(geoMesh->mBones[i]);
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | // Write the mesh bounds.
|
---|
129 | writeMeshBounds(geoMesh);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | // Write LOD data if any
|
---|
133 | if (pMesh->getNumLodLevels() > 1)
|
---|
134 | {
|
---|
135 | LogManager::getSingleton().logMessage("Exporting LOD information....");
|
---|
136 | writeLodInfo(pMesh);
|
---|
137 | LogManager::getSingleton().logMessage("LOD information exported.");
|
---|
138 |
|
---|
139 | }
|
---|
140 | // Write bounds information
|
---|
141 | LogManager::getSingleton().logMessage("Exporting bounds information....");
|
---|
142 | writeBoundsInfo(pMesh);
|
---|
143 | LogManager::getSingleton().logMessage("Bounds information exported.");
|
---|
144 |
|
---|
145 | // Write submesh name table
|
---|
146 | LogManager::getSingleton().logMessage("Exporting submesh name table...");
|
---|
147 | writeSubMeshNameTable(pMesh);
|
---|
148 | LogManager::getSingleton().logMessage("Submesh name table exported.");
|
---|
149 |
|
---|
150 | // Write edge lists
|
---|
151 | if (pMesh->isEdgeListBuilt())
|
---|
152 | {
|
---|
153 | LogManager::getSingleton().logMessage("Exporting edge lists...");
|
---|
154 | writeEdgeList(pMesh);
|
---|
155 | LogManager::getSingleton().logMessage("Edge lists exported");
|
---|
156 | }
|
---|
157 | */
|
---|
158 | }// End write mesh.
|
---|
159 |
|
---|
160 | // Write a submesh.
|
---|
161 | void GeoMeshSaver::writeSubMesh(SubMesh *geoSubMesh)
|
---|
162 | {
|
---|
163 | bool idx32bit;
|
---|
164 | unsigned long indexCount;
|
---|
165 | Index *index;
|
---|
166 | Index *indexBegin;
|
---|
167 | Index *indexEnd;
|
---|
168 |
|
---|
169 | // Header.
|
---|
170 | writeChunkHeader(M_SUBMESH, calcSubMeshSize(geoSubMesh));
|
---|
171 |
|
---|
172 | // Debug.
|
---|
173 | cout << " M_SUBMESH"
|
---|
174 | << endl;
|
---|
175 |
|
---|
176 | // Material Name.
|
---|
177 | mSerializer->WriteData(geoSubMesh->mMaterialName);
|
---|
178 |
|
---|
179 | // bool useSharedVertices
|
---|
180 | writeBools(geoSubMesh->mSharedVertexBuffer, 1);
|
---|
181 |
|
---|
182 | indexCount = geoSubMesh->mIndexCount;
|
---|
183 |
|
---|
184 | // If the submesh is in triangle strips.
|
---|
185 | if (geoSubMesh->mType == GEO_TRIANGLE_STRIPS)
|
---|
186 | {
|
---|
187 | indexCount += 2 * geoSubMesh->mStripCount - 2;
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Write index count.
|
---|
191 | writeInts(indexCount, 1);
|
---|
192 |
|
---|
193 | // bool indexes32Bit
|
---|
194 | idx32bit = true;
|
---|
195 | writeBools(idx32bit, 1);
|
---|
196 |
|
---|
197 | // If the submesh is in triangle strips.
|
---|
198 | if (geoSubMesh->mType == GEO_TRIANGLE_STRIPS)
|
---|
199 | {
|
---|
200 | // For each one of the strips.
|
---|
201 | for (int strip = 0; strip < geoSubMesh->mStripCount; strip++)
|
---|
202 | {
|
---|
203 | // First index of the strip.
|
---|
204 | indexBegin = geoSubMesh->mStrip[strip];
|
---|
205 |
|
---|
206 | // If is the final strip
|
---|
207 | if (strip == (geoSubMesh->mStripCount - 1))
|
---|
208 | {
|
---|
209 | // The end of the index array.
|
---|
210 | indexEnd = &geoSubMesh->mIndex[geoSubMesh->mIndexCount];
|
---|
211 | }
|
---|
212 | else
|
---|
213 | {
|
---|
214 | // The beginning of the next strip.
|
---|
215 | indexEnd = geoSubMesh->mStrip[strip + 1];
|
---|
216 | }
|
---|
217 |
|
---|
218 | int i;
|
---|
219 | i = 0;
|
---|
220 |
|
---|
221 | if (strip != 0)
|
---|
222 | {
|
---|
223 | writeInts(indexBegin[i], 1);
|
---|
224 | }
|
---|
225 |
|
---|
226 | // For each index of the strip.
|
---|
227 | for (index = indexBegin; index < indexEnd; index++)
|
---|
228 | {
|
---|
229 | writeInts(indexBegin[i], 1);
|
---|
230 |
|
---|
231 | // Increments i.
|
---|
232 | i++;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (strip != (geoSubMesh->mStripCount - 1))
|
---|
236 | {
|
---|
237 | writeInts(indexBegin[i - 1], 1);
|
---|
238 | }
|
---|
239 |
|
---|
240 | }
|
---|
241 | }
|
---|
242 | // If the submesh is in triangle list.
|
---|
243 | else
|
---|
244 | {
|
---|
245 | // Write the index array.
|
---|
246 | for (int i = 0; i < geoSubMesh->mIndexCount; i++)
|
---|
247 | {
|
---|
248 | writeInts(geoSubMesh->mIndex[i], 1);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | // M_GEOMETRY stream (Optional: present only if useSharedVertices = false)
|
---|
253 | if (!geoSubMesh->mSharedVertexBuffer)
|
---|
254 | {
|
---|
255 | writeGeometry(geoSubMesh->mVertexBuffer);
|
---|
256 | }
|
---|
257 |
|
---|
258 | // Operation type
|
---|
259 | writeSubMeshOperation(geoSubMesh);
|
---|
260 |
|
---|
261 | // Bone assignments
|
---|
262 | if (!geoSubMesh->mBones.empty())
|
---|
263 | {
|
---|
264 | for (int i = 0; i < geoSubMesh->mBones.size(); i++)
|
---|
265 | {
|
---|
266 | writeSubMeshBoneAssignment(geoSubMesh->mBones[i]);
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | // Write the submesh operation chunk and data.
|
---|
272 | void GeoMeshSaver::writeSubMeshOperation(const SubMesh *geoSubMesh)
|
---|
273 | {
|
---|
274 | unsigned short opType;
|
---|
275 | unsigned long size;
|
---|
276 |
|
---|
277 | size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned short);
|
---|
278 |
|
---|
279 | // Header
|
---|
280 | writeChunkHeader(M_SUBMESH_OPERATION, size);
|
---|
281 |
|
---|
282 | // Debug.
|
---|
283 | cout << " M_SUBMESH_OPERATION"
|
---|
284 | << endl;
|
---|
285 |
|
---|
286 | // If the mesh is in triangle strips.
|
---|
287 | if (geoSubMesh->mType == GEO_TRIANGLE_STRIPS)
|
---|
288 | {
|
---|
289 | opType = 5;
|
---|
290 | }
|
---|
291 | // If the mesh is in triangle list.
|
---|
292 | else
|
---|
293 | {
|
---|
294 | opType = 4;
|
---|
295 | }
|
---|
296 |
|
---|
297 | writeShorts(opType, 1);
|
---|
298 | }
|
---|
299 |
|
---|
300 | // Write geometry.
|
---|
301 | void GeoMeshSaver::writeGeometry(VertexBuffer *vertexBuffer)
|
---|
302 | {
|
---|
303 | unsigned short element;
|
---|
304 | unsigned long size;
|
---|
305 | unsigned short buffer_count;
|
---|
306 |
|
---|
307 | buffer_count = 3;
|
---|
308 |
|
---|
309 |
|
---|
310 | // Calculate the size in bytes of the geometry chunk.
|
---|
311 | size = calcGeometrySize(vertexBuffer);
|
---|
312 |
|
---|
313 | // Header.
|
---|
314 | //
|
---|
315 | // Write the M_GEOMETRY header.
|
---|
316 | writeChunkHeader(M_GEOMETRY, size);
|
---|
317 |
|
---|
318 | // Debug.
|
---|
319 | cout << " M_GEOMETRY"
|
---|
320 | << endl;
|
---|
321 |
|
---|
322 | writeInts(vertexBuffer->mVertexCount,1);
|
---|
323 |
|
---|
324 | // Vertex declaration.
|
---|
325 | // Calculate the size of vertex declaration.
|
---|
326 | size = CHUNK_OVERHEAD_SIZE
|
---|
327 | +
|
---|
328 | buffer_count
|
---|
329 | *
|
---|
330 | (CHUNK_OVERHEAD_SIZE + (sizeof(unsigned short) * 5));
|
---|
331 |
|
---|
332 | // Write the vertex declaration header.
|
---|
333 | writeChunkHeader( M_GEOMETRY_VERTEX_DECLARATION,size);
|
---|
334 |
|
---|
335 | // Debug.
|
---|
336 | cout << " M_GEOMETRY_VERTEX_DECLARATION"
|
---|
337 | << endl;
|
---|
338 |
|
---|
339 | // Obtain the size of the vertex element chunk.
|
---|
340 | size = CHUNK_OVERHEAD_SIZE + (sizeof(unsigned short) * 5);
|
---|
341 |
|
---|
342 | // Positions.
|
---|
343 | // Write the vertex element header for position.
|
---|
344 | writeChunkHeader( M_GEOMETRY_VERTEX_ELEMENT,size);
|
---|
345 |
|
---|
346 | // Debug.
|
---|
347 | cout << " M_GEOMETRY_VERTEX_ELEMENT"
|
---|
348 | << endl;
|
---|
349 |
|
---|
350 | element = 0;
|
---|
351 | writeShorts(element,1);
|
---|
352 |
|
---|
353 | element = VET_FLOAT3;
|
---|
354 | writeShorts(element,1);
|
---|
355 |
|
---|
356 | element = VES_POSITION;
|
---|
357 | writeShorts(element,1);
|
---|
358 |
|
---|
359 | element = 0;
|
---|
360 | writeShorts(element,1);
|
---|
361 |
|
---|
362 | element = 0;
|
---|
363 | writeShorts(element,1);
|
---|
364 |
|
---|
365 | // Normals.
|
---|
366 | // Write the vertex element header for position.
|
---|
367 | writeChunkHeader( M_GEOMETRY_VERTEX_ELEMENT,size);
|
---|
368 |
|
---|
369 | // Debug.
|
---|
370 | cout << " M_GEOMETRY_VERTEX_ELEMENT"
|
---|
371 | << endl;
|
---|
372 |
|
---|
373 | element = 1;
|
---|
374 | writeShorts(element,1);
|
---|
375 |
|
---|
376 | element = VET_FLOAT3;
|
---|
377 | writeShorts(element,1);
|
---|
378 |
|
---|
379 | element = VES_NORMAL;
|
---|
380 | writeShorts(element,1);
|
---|
381 |
|
---|
382 | element = 0;
|
---|
383 | writeShorts(element,1);
|
---|
384 |
|
---|
385 | element = 0;
|
---|
386 | writeShorts(element,1);
|
---|
387 |
|
---|
388 | // Textures.
|
---|
389 | // Write the vertex element header for position.
|
---|
390 | writeChunkHeader(M_GEOMETRY_VERTEX_ELEMENT,size);
|
---|
391 |
|
---|
392 | // Debug.
|
---|
393 | cout << " M_GEOMETRY_VERTEX_ELEMENT"
|
---|
394 | << endl;
|
---|
395 |
|
---|
396 | element = 2;
|
---|
397 | writeShorts(element,1);
|
---|
398 |
|
---|
399 | element = VET_FLOAT2;
|
---|
400 | writeShorts(element,1);
|
---|
401 |
|
---|
402 | element = VES_TEXTURE_COORDINATES;
|
---|
403 | writeShorts(element,1);
|
---|
404 |
|
---|
405 | element = 0;
|
---|
406 | writeShorts(element,1);
|
---|
407 |
|
---|
408 | element = 0;
|
---|
409 | writeShorts(element,1);
|
---|
410 |
|
---|
411 | // Obtain the size for vertex buffer header for positons.
|
---|
412 | size = (2 * CHUNK_OVERHEAD_SIZE) + (2 * sizeof(unsigned short));
|
---|
413 |
|
---|
414 | // Write the vertex buffer header for positions.
|
---|
415 | writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER, size);
|
---|
416 |
|
---|
417 | // Debug.
|
---|
418 | cout << " M_GEOMETRY_VERTEX_BUFFER"
|
---|
419 | << endl;
|
---|
420 |
|
---|
421 | element = 0;
|
---|
422 | writeShorts(element,1);
|
---|
423 |
|
---|
424 | element = 12;
|
---|
425 | writeShorts(element,1);
|
---|
426 |
|
---|
427 | // Obtain the size for the vertex buffer data header for positions.
|
---|
428 | size = CHUNK_OVERHEAD_SIZE
|
---|
429 | +
|
---|
430 | ((sizeof(float) * 3) * vertexBuffer->mVertexCount);
|
---|
431 |
|
---|
432 | // Write the vertex buffer data header for positions.
|
---|
433 | writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER_DATA, size);
|
---|
434 |
|
---|
435 | // Debug.
|
---|
436 | cout << " M_GEOMETRY_VERTEX_BUFFER_DATA"
|
---|
437 | << endl;
|
---|
438 |
|
---|
439 | // Write all the positions coords.
|
---|
440 | mSerializer->WriteArray(vertexBuffer->mPosition,
|
---|
441 | vertexBuffer->mVertexCount);
|
---|
442 |
|
---|
443 | // Obtain the size for vertex buffer header for normals.
|
---|
444 | size = (2 * CHUNK_OVERHEAD_SIZE) + (2 * sizeof(unsigned short));
|
---|
445 |
|
---|
446 | // Write the vertex buffer header.
|
---|
447 | writeChunkHeader( M_GEOMETRY_VERTEX_BUFFER, size);
|
---|
448 |
|
---|
449 | // Debug.
|
---|
450 | cout << " M_GEOMETRY_VERTEX_BUFFER"
|
---|
451 | << endl;
|
---|
452 |
|
---|
453 | element = 1;
|
---|
454 | writeShorts(element,1);
|
---|
455 |
|
---|
456 | element = 12;
|
---|
457 | writeShorts(element,1);
|
---|
458 |
|
---|
459 | // Obtain the size for the vertex buffer data header for normals.
|
---|
460 | size = CHUNK_OVERHEAD_SIZE
|
---|
461 | +
|
---|
462 | ((sizeof(float) * 3) * vertexBuffer->mVertexCount);
|
---|
463 |
|
---|
464 | // Write the vertex buffer data header for normals.
|
---|
465 | writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER_DATA, size);
|
---|
466 |
|
---|
467 | // Debug.
|
---|
468 | cout << " M_GEOMETRY_VERTEX_BUFFER_DATA"
|
---|
469 | << endl;
|
---|
470 |
|
---|
471 | // Write all the normals coords.
|
---|
472 | mSerializer->WriteArray(vertexBuffer->mNormal,
|
---|
473 | vertexBuffer->mVertexCount);
|
---|
474 |
|
---|
475 | // Obtain the size for vertex buffer header for textures.
|
---|
476 | size = (2 * CHUNK_OVERHEAD_SIZE) + (2 * sizeof(unsigned short));
|
---|
477 |
|
---|
478 | // Write the vertex buffer header for textures.
|
---|
479 | writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER, size);
|
---|
480 |
|
---|
481 | // Debug.
|
---|
482 | cout << " M_GEOMETRY_VERTEX_BUFFER"
|
---|
483 | << endl;
|
---|
484 |
|
---|
485 | element = 2;
|
---|
486 | writeShorts(element,1);
|
---|
487 |
|
---|
488 | element = 8;
|
---|
489 | writeShorts(element,1);
|
---|
490 |
|
---|
491 | // Obtain the size for the vertex buffer data header for textures.
|
---|
492 | size = CHUNK_OVERHEAD_SIZE
|
---|
493 | +
|
---|
494 | ((sizeof(float) * 2) * vertexBuffer->mVertexCount);
|
---|
495 |
|
---|
496 | // Write the vertex buffer data header for textures.
|
---|
497 | writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER_DATA, size);
|
---|
498 |
|
---|
499 | // Debug.
|
---|
500 | cout << " M_GEOMETRY_VERTEX_BUFFER_DATA"
|
---|
501 | << endl;
|
---|
502 |
|
---|
503 | // Write all the texture coords.
|
---|
504 | mSerializer->WriteArray(vertexBuffer->mTexCoords,
|
---|
505 | vertexBuffer->mVertexCount);
|
---|
506 | }
|
---|
507 |
|
---|
508 | // Write Mesh Bounds.
|
---|
509 | void GeoMeshSaver::writeMeshBounds(Mesh *geoMesh)
|
---|
510 | {
|
---|
511 | size_t size;
|
---|
512 |
|
---|
513 | size = CHUNK_OVERHEAD_SIZE
|
---|
514 | +
|
---|
515 | (sizeof(float) * 7);
|
---|
516 |
|
---|
517 | writeChunkHeader(M_MESH_BOUNDS, size);
|
---|
518 |
|
---|
519 | // Debug.
|
---|
520 | cout << " M_MESH_BOUNDS"
|
---|
521 | << endl;
|
---|
522 |
|
---|
523 | writeFloats(geoMesh->mMeshBounds.minX,1);
|
---|
524 | writeFloats(geoMesh->mMeshBounds.minY,1);
|
---|
525 | writeFloats(geoMesh->mMeshBounds.minZ,1);
|
---|
526 | writeFloats(geoMesh->mMeshBounds.maxX,1);
|
---|
527 | writeFloats(geoMesh->mMeshBounds.maxY,1);
|
---|
528 | writeFloats(geoMesh->mMeshBounds.maxZ,1);
|
---|
529 | writeFloats(geoMesh->mMeshBounds.radius,1);
|
---|
530 | }
|
---|
531 |
|
---|
532 | // Calculate the mesh size in bytes.
|
---|
533 | size_t GeoMeshSaver::calcMeshSize(const Mesh *geoMesh)
|
---|
534 | {
|
---|
535 | size_t size = CHUNK_OVERHEAD_SIZE;
|
---|
536 |
|
---|
537 | // Number of shared vertices
|
---|
538 | size += sizeof(uint32);
|
---|
539 |
|
---|
540 | // Geometry
|
---|
541 | if (geoMesh->mVertexBuffer->mVertexCount > 0)
|
---|
542 | {
|
---|
543 | size += calcGeometrySize(geoMesh->mVertexBuffer);
|
---|
544 | }
|
---|
545 |
|
---|
546 | // Submeshes
|
---|
547 | for (unsigned short i = 0; i < geoMesh->mSubMeshCount; ++i)
|
---|
548 | {
|
---|
549 | size += calcSubMeshSize(&geoMesh->mSubMesh[i]);
|
---|
550 | }
|
---|
551 |
|
---|
552 | // Mesh Bounds size added.
|
---|
553 | size += CHUNK_OVERHEAD_SIZE
|
---|
554 | +
|
---|
555 | (sizeof(float) * 7);
|
---|
556 |
|
---|
557 | // Skeleton link
|
---|
558 | if (geoMesh->hasSkeleton)
|
---|
559 | {
|
---|
560 | size += calcSkeletonLinkSize(geoMesh);
|
---|
561 | }
|
---|
562 |
|
---|
563 | /*
|
---|
564 | // Submesh name table
|
---|
565 | size += calcSubMeshNameTableSize(geoMesh);
|
---|
566 |
|
---|
567 | // Edge list
|
---|
568 | if (geoMesh->isEdgeListBuilt())
|
---|
569 | {
|
---|
570 | size += calcEdgeListSize(geoMesh);
|
---|
571 | }
|
---|
572 | */
|
---|
573 |
|
---|
574 | return size;
|
---|
575 | }
|
---|
576 |
|
---|
577 | // Calc the size in bytes for the submesh.
|
---|
578 | size_t GeoMeshSaver::calcSubMeshSize(const SubMesh *geoSubMesh)
|
---|
579 | {
|
---|
580 | size_t size = CHUNK_OVERHEAD_SIZE;
|
---|
581 |
|
---|
582 | // Material name
|
---|
583 | size += strlen(geoSubMesh->mMaterialName);
|
---|
584 |
|
---|
585 | // bool useSharedVertices
|
---|
586 | size += sizeof(bool);
|
---|
587 | // unsigned int indexCount
|
---|
588 | size += sizeof(unsigned int);
|
---|
589 | // bool indexes32bit
|
---|
590 | size += sizeof(bool);
|
---|
591 |
|
---|
592 | // unsigned int* faceVertexIndices
|
---|
593 | size += sizeof(unsigned int) * geoSubMesh->mIndexCount;
|
---|
594 |
|
---|
595 | // Geometry
|
---|
596 | if (!geoSubMesh->mSharedVertexBuffer)
|
---|
597 | {
|
---|
598 | size += calcGeometrySize(geoSubMesh->mVertexBuffer);
|
---|
599 | }
|
---|
600 |
|
---|
601 | return size;
|
---|
602 | }
|
---|
603 |
|
---|
604 | // Calculate the geometry size in bytes.
|
---|
605 | size_t GeoMeshSaver::calcGeometrySize(const VertexBuffer* vertexBuffer)
|
---|
606 | {
|
---|
607 | unsigned long size;
|
---|
608 | unsigned long buffer_count;
|
---|
609 |
|
---|
610 | // and another for normals.
|
---|
611 | buffer_count = 3;
|
---|
612 |
|
---|
613 | // Calculate the size of the Geometry chunk.
|
---|
614 | size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned int);
|
---|
615 | size = size
|
---|
616 | +
|
---|
617 | CHUNK_OVERHEAD_SIZE
|
---|
618 | +
|
---|
619 | buffer_count
|
---|
620 | *
|
---|
621 | (CHUNK_OVERHEAD_SIZE + (sizeof(unsigned short) * 5));
|
---|
622 | size = size
|
---|
623 | +
|
---|
624 | buffer_count
|
---|
625 | *
|
---|
626 | ( (CHUNK_OVERHEAD_SIZE * 2)
|
---|
627 | +
|
---|
628 | (sizeof(unsigned short) * 2)
|
---|
629 | +
|
---|
630 | vertexBuffer->mVertexCount
|
---|
631 | );
|
---|
632 |
|
---|
633 | return size;
|
---|
634 | }
|
---|
635 |
|
---|
636 | // Calculate the skeleton link size in bytes.
|
---|
637 | size_t GeoMeshSaver::calcSkeletonLinkSize(const Mesh *geoMesh)
|
---|
638 | {
|
---|
639 | size_t size = CHUNK_OVERHEAD_SIZE;
|
---|
640 | size += strlen(geoMesh->mSkeletonName);
|
---|
641 |
|
---|
642 | // Debug.
|
---|
643 | cout << "Length Skeleton Link: "
|
---|
644 | << strlen(geoMesh->mSkeletonName)
|
---|
645 | << endl;
|
---|
646 |
|
---|
647 | return size;
|
---|
648 | }
|
---|
649 |
|
---|
650 | // Write the file header.
|
---|
651 | void GeoMeshSaver::writeFileHeader(void)
|
---|
652 | {
|
---|
653 | String mesh_version("[MeshSerializer_v1.30]\n");
|
---|
654 |
|
---|
655 | writeShorts(M_HEADER, 1);
|
---|
656 |
|
---|
657 | writeString(mesh_version);
|
---|
658 |
|
---|
659 | }
|
---|
660 |
|
---|
661 | // Write a header chunk given.
|
---|
662 | void GeoMeshSaver::writeChunkHeader(unsigned short id,
|
---|
663 | unsigned long size)
|
---|
664 | {
|
---|
665 | mSerializer->WriteData(&id,sizeof(unsigned short),1);
|
---|
666 | mSerializer->WriteData(&size,sizeof(unsigned long),1);
|
---|
667 | }
|
---|
668 |
|
---|
669 | // Write integers into the file.
|
---|
670 | void GeoMeshSaver::writeInts(unsigned long id,
|
---|
671 | unsigned long count)
|
---|
672 | {
|
---|
673 | mSerializer->WriteData(&id,sizeof(id),count);
|
---|
674 | }
|
---|
675 |
|
---|
676 | // Write shorts into the file
|
---|
677 | void GeoMeshSaver::writeShorts(unsigned short id,
|
---|
678 | unsigned long count)
|
---|
679 | {
|
---|
680 | mSerializer->WriteData(&id,sizeof(id),count);
|
---|
681 | }
|
---|
682 |
|
---|
683 | // Write float into the file.
|
---|
684 | void GeoMeshSaver::writeFloats(float id,
|
---|
685 | unsigned long count)
|
---|
686 | {
|
---|
687 | mSerializer->WriteData(&id,sizeof(id),count);
|
---|
688 | }
|
---|
689 |
|
---|
690 | // Write a string into the file.
|
---|
691 | void GeoMeshSaver::writeString(const String &string)
|
---|
692 | {
|
---|
693 | mSerializer->WriteData(string);
|
---|
694 | }
|
---|
695 |
|
---|
696 | // Write booleans into the file.
|
---|
697 | void GeoMeshSaver::writeBools( const bool id,
|
---|
698 | unsigned long count)
|
---|
699 | {
|
---|
700 | mSerializer->WriteData(&id,sizeof(bool),count);
|
---|
701 | }
|
---|
702 |
|
---|
703 | void GeoMeshSaver::writeSkeletonLink(const String& skelName)
|
---|
704 | {
|
---|
705 | writeChunkHeader(M_MESH_SKELETON_LINK, calcSkeletonLinkSize(mGeoMesh));
|
---|
706 |
|
---|
707 | // Debug.
|
---|
708 | cout << " M_MESH_SKELETON_LINK"
|
---|
709 | << endl;
|
---|
710 |
|
---|
711 | writeString(skelName);
|
---|
712 | }
|
---|
713 |
|
---|
714 | void GeoMeshSaver::writeMeshBoneAssignment(const VertexBoneAssignment& assign)
|
---|
715 | {
|
---|
716 | size_t size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned int) + sizeof(unsigned short)+ sizeof(float);
|
---|
717 |
|
---|
718 | writeChunkHeader(M_MESH_BONE_ASSIGNMENT, size);
|
---|
719 |
|
---|
720 | // unsigned int vertexIndex;
|
---|
721 | writeInts(assign.vertexIndex, 1);
|
---|
722 | // unsigned short boneIndex;
|
---|
723 | writeShorts(assign.boneIndex, 1);
|
---|
724 | // float weight;
|
---|
725 | writeFloats(assign.weight, 1);
|
---|
726 | }
|
---|
727 |
|
---|
728 | void GeoMeshSaver::writeSubMeshBoneAssignment(const VertexBoneAssignment& assign)
|
---|
729 | {
|
---|
730 | size_t size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned int) + sizeof(unsigned short)+ sizeof(float);
|
---|
731 |
|
---|
732 | writeChunkHeader(M_SUBMESH_BONE_ASSIGNMENT, size);
|
---|
733 |
|
---|
734 | // unsigned int vertexIndex;
|
---|
735 | writeInts(assign.vertexIndex, 1);
|
---|
736 | // unsigned short boneIndex;
|
---|
737 | writeShorts(assign.boneIndex, 1);
|
---|
738 | // float weight;
|
---|
739 | writeFloats(assign.weight, 1);
|
---|
740 | }
|
---|
741 |
|
---|
742 | // unnormalize geometry model.
|
---|
743 | void GeoMeshSaver::unnormalizeModel(Mesh *geoMesh)
|
---|
744 | {
|
---|
745 | float maxx;
|
---|
746 | float maxy;
|
---|
747 | float maxz;
|
---|
748 | float minx;
|
---|
749 | float miny;
|
---|
750 | float minz;
|
---|
751 | float cx;
|
---|
752 | float cy;
|
---|
753 | float cz;
|
---|
754 | float scale;
|
---|
755 |
|
---|
756 | VertexBuffer *vertex_buffer;
|
---|
757 |
|
---|
758 | maxx = geoMesh->mMeshBounds.maxX;
|
---|
759 | maxy = geoMesh->mMeshBounds.maxY;
|
---|
760 | maxz = geoMesh->mMeshBounds.maxZ;
|
---|
761 | minx = geoMesh->mMeshBounds.minX;
|
---|
762 | miny = geoMesh->mMeshBounds.minY;
|
---|
763 | minz = geoMesh->mMeshBounds.minZ;
|
---|
764 | scale = geoMesh->mMeshBounds.scaleFactor;
|
---|
765 |
|
---|
766 | // Calculate center of the model.
|
---|
767 | cx = (maxx + minx) / 2.0;
|
---|
768 | cy = (maxy + miny) / 2.0;
|
---|
769 | cz = (maxz + minz) / 2.0;
|
---|
770 |
|
---|
771 | // Translate around center then scale.
|
---|
772 | // For each submesh.
|
---|
773 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
774 | {
|
---|
775 | // Gets the actual submesh.
|
---|
776 | vertex_buffer = geoMesh->mSubMesh[submesh].mVertexBuffer;
|
---|
777 |
|
---|
778 | // For each index of the strip.
|
---|
779 | for (int i = 0; i < vertex_buffer->mVertexCount; i++)
|
---|
780 | {
|
---|
781 | vertex_buffer->mPosition[i].x /= scale;
|
---|
782 | vertex_buffer->mPosition[i].y /= scale;
|
---|
783 | vertex_buffer->mPosition[i].z /= scale;
|
---|
784 | vertex_buffer->mPosition[i].x += cx;
|
---|
785 | vertex_buffer->mPosition[i].y += cy;
|
---|
786 | vertex_buffer->mPosition[i].z += cz;
|
---|
787 | }
|
---|
788 |
|
---|
789 | // If is a shared vertex Buffer.
|
---|
790 | if (geoMesh->mSubMesh[submesh].mSharedVertexBuffer)
|
---|
791 | {
|
---|
792 | break;
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|