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