From 978f2aa08eaab747f32baa736a764e7774dae004 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 25 Apr 2011 19:00:25 -0400 Subject: [PATCH] updated keyframe data structures, and updated md2 mesh exporting to use the chunk structs --- MeshConverter/src/chunks/chunks.cpp | 27 ++++-- MeshConverter/src/chunks/keyframes.h | 36 ++++++-- MeshConverter/src/geometry/keyframe.h | 19 +++- MeshConverter/src/md2/md2.cpp | 126 +++++++++----------------- 4 files changed, 104 insertions(+), 104 deletions(-) diff --git a/MeshConverter/src/chunks/chunks.cpp b/MeshConverter/src/chunks/chunks.cpp index 5cc5b4d..aa2df21 100644 --- a/MeshConverter/src/chunks/chunks.cpp +++ b/MeshConverter/src/chunks/chunks.cpp @@ -147,20 +147,27 @@ void WriteChunk(KeyFramesChunk *chunk, FILE *fp) fputs("KFR", fp); fwrite(&size, 4, 1, fp); - uint32_t count = chunk->GetCount(); - fwrite(&count, 4, 1, fp); + uint32_t numFrames = chunk->GetNumFrames(); + fwrite(&numFrames, 4, 1, fp); + fwrite(&chunk->numVertices, 4, 1, fp); - for (uint32_t i = 0; i < count; ++i) + for (uint32_t i = 0; i < numFrames; ++i) { - KeyFrame *f = &chunk->frames[i]; + KeyFrame *f = chunk->frames[i]; - fwrite(&f->vertex.x, sizeof(float), 1, fp); - fwrite(&f->vertex.y, sizeof(float), 1, fp); - fwrite(&f->vertex.z, sizeof(float), 1, fp); + for (uint32_t j = 0; j < chunk->numVertices; ++j) + { + fwrite(&f->vertices->x, sizeof(float), 1, fp); + fwrite(&f->vertices->y, sizeof(float), 1, fp); + fwrite(&f->vertices->z, sizeof(float), 1, fp); + } - fwrite(&f->normal.x, sizeof(float), 1, fp); - fwrite(&f->normal.y, sizeof(float), 1, fp); - fwrite(&f->normal.z, sizeof(float), 1, fp); + for (uint32_t j = 0; j < chunk->numVertices; ++j) + { + fwrite(&f->normals->x, sizeof(float), 1, fp); + fwrite(&f->normals->y, sizeof(float), 1, fp); + fwrite(&f->normals->z, sizeof(float), 1, fp); + } } } diff --git a/MeshConverter/src/chunks/keyframes.h b/MeshConverter/src/chunks/keyframes.h index 9c860a2..6cf68ad 100644 --- a/MeshConverter/src/chunks/keyframes.h +++ b/MeshConverter/src/chunks/keyframes.h @@ -7,9 +7,28 @@ struct KeyFramesChunk { - std::vector frames; + std::vector frames; + uint32_t numVertices; - uint32_t GetCount() + KeyFramesChunk() + { + numVertices = 0; + } + + ~KeyFramesChunk() + { + for (uint32_t i = 0; i < frames.size(); ++i) + SAFE_DELETE(frames[i]); + } + + KeyFrame* AddFrame() + { + KeyFrame *newFrame = new KeyFrame(numVertices); + frames.push_back(newFrame); + return newFrame; + } + + uint32_t GetNumFrames() { return frames.size(); } @@ -20,11 +39,14 @@ struct KeyFramesChunk return 0; uint32_t size = 4; // count - size += - ( - (sizeof(float) * 3) // vertex - + (sizeof(float) * 3) // normal - ) * frames.size(); + for (uint32_t i = 0; i < frames.size(); ++i) + { + size += + ( + (sizeof(float) * 3) // vertex + + (sizeof(float) * 3) // normal + ) * frames[i]->count; + } return size; } diff --git a/MeshConverter/src/geometry/keyframe.h b/MeshConverter/src/geometry/keyframe.h index be576e4..e56b229 100644 --- a/MeshConverter/src/geometry/keyframe.h +++ b/MeshConverter/src/geometry/keyframe.h @@ -1,13 +1,28 @@ #ifndef __GEOMETRY_KEYFRAME_H_INCLUDED__ #define __GEOMETRY_KEYFRAME_H_INCLUDED__ +#include "../common.h" #include "vector3.h" class KeyFrame { public: - Vector3 vertex; - Vector3 normal; + uint32_t count; + Vector3 *vertices; + Vector3 *normals; + + KeyFrame(uint32_t numVertices) + { + count = numVertices; + vertices = new Vector3[count]; + normals = new Vector3[count]; + } + + ~KeyFrame() + { + SAFE_DELETE_ARRAY(vertices); + SAFE_DELETE_ARRAY(normals); + } }; #endif diff --git a/MeshConverter/src/md2/md2.cpp b/MeshConverter/src/md2/md2.cpp index 1dee97f..384df44 100644 --- a/MeshConverter/src/md2/md2.cpp +++ b/MeshConverter/src/md2/md2.cpp @@ -1,6 +1,7 @@ #include "md2.h" #include +#include "../chunks/chunks.h" Md2::Md2() { @@ -297,104 +298,59 @@ bool Md2::ConvertToMesh(const std::string &file) if (fp == NULL) return false; - fputs("MESH", fp); - unsigned char version = 1; - fwrite(&version, 1, 1, fp); - // keyframes chunk - fputs("KFR", fp); - long numFrames = m_numFrames; - long numVertices = m_numVertices; - long sizeofFrames = ((sizeof(float) * 3 * 2) * numVertices) * numFrames + (sizeof(long) * 2); - fwrite(&sizeofFrames, sizeof(long), 1, fp); - fwrite(&numFrames, sizeof(long), 1, fp); - fwrite(&numVertices, sizeof(long), 1, fp); - for (long i = 0; i < numFrames; ++i) + WriteFileHeader(fp); + + KeyFramesChunk *keyFramesChunk = new KeyFramesChunk(); + keyFramesChunk->numVertices = m_numVertices; + for (long i = 0; i < m_numFrames; ++i) { - // vertices + KeyFrame *frame = keyFramesChunk->AddFrame(); + for (int j = 0; j < m_numVertices; ++j) - { - const Vector3 *vertex = &m_frames[i].vertices[j]; - fwrite(&vertex->x, sizeof(float), 1, fp); - fwrite(&vertex->y, sizeof(float), 1, fp); - fwrite(&vertex->z, sizeof(float), 1, fp); - } - - // normals + frame->vertices[j] = m_frames[i].vertices[j]; + for (int j = 0; j < m_numVertices; ++j) - { - const Vector3 *normal = &m_frames[i].normals[j]; - fwrite(&normal->x, sizeof(float), 1, fp); - fwrite(&normal->y, sizeof(float), 1, fp); - fwrite(&normal->z, sizeof(float), 1, fp); - } + frame->normals[j] = m_frames[i].normals[j]; } + WriteChunk(keyFramesChunk, fp); - // textures chunk - fputs("KTX", fp); - long numTexCoords = m_numTexCoords; - long sizeofTexCoords = (sizeof(float) * 2) * numTexCoords + sizeof(long); - fwrite(&sizeofTexCoords, sizeof(long), 1, fp); - fwrite(&numTexCoords, sizeof(long), 1, fp); - for (long i = 0; i < numTexCoords; ++i) + TexCoordsChunk *texCoordsChunk = new TexCoordsChunk(); + for (long i = 0; i < m_numTexCoords; ++i) + texCoordsChunk->texCoords.push_back(m_texCoords[i]); + WriteChunk(texCoordsChunk, fp); + + KeyFrameTrianglesChunk *trianglesChunk = new KeyFrameTrianglesChunk(); + for (long i = 0; i < m_numPolys; ++i) { - const Vector2 *texCoord = &m_texCoords[i]; - fwrite(&texCoord->x, sizeof(float), 1, fp); - fwrite(&texCoord->y, sizeof(float), 1, fp); - } - - // triangles chunk - fputs("KTR", fp); - long numPolys = m_numPolys; - long sizeofPolys = (sizeof(long) * 3 * 2) * numPolys + sizeof(long); - fwrite(&sizeofPolys, sizeof(long), 1, fp); - fwrite(&numPolys, sizeof(long), 1, fp); - for (long i = 0; i < numPolys; ++i) - { - long data; - - // vertex indices - data = m_polys[i].vertex[0]; - fwrite(&data, sizeof(long), 1, fp); - data = m_polys[i].vertex[1]; - fwrite(&data, sizeof(long), 1, fp); - data = m_polys[i].vertex[2]; - fwrite(&data, sizeof(long), 1, fp); - - // tex coord indices - data = m_polys[i].texCoord[0]; - fwrite(&data, sizeof(long), 1, fp); - data = m_polys[i].texCoord[1]; - fwrite(&data, sizeof(long), 1, fp); - data = m_polys[i].texCoord[2]; - fwrite(&data, sizeof(long), 1, fp); + KeyFrameTriangle t; + + t.vertices[0] = m_polys[i].vertex[0]; + t.vertices[1] = m_polys[i].vertex[1]; + t.vertices[2] = m_polys[i].vertex[2]; + + t.texCoords[0] = m_polys[i].texCoord[0]; + t.texCoords[1] = m_polys[i].texCoord[1]; + t.texCoords[2] = m_polys[i].texCoord[2]; + + trianglesChunk->triangles.push_back(t); } + WriteChunk(trianglesChunk, fp); if (m_animations.size() > 0) { - // figure out the size of all the animation name strings - long sizeofNames = 0; - for (int i = 0; i < m_animations.size(); ++i) - sizeofNames += m_animations[i].name.length() + 1; - - // animations chunk - fputs("ANI", fp); - long numAnimations = m_animations.size(); - long sizeofAnimations = (sizeof(long) * 2) * numAnimations + sizeofNames + sizeof(long); - fwrite(&sizeofAnimations, sizeof(long), 1, fp); - fwrite(&numAnimations, sizeof(long), 1, fp); - for (long i = 0; i < numAnimations; ++i) + AnimationsChunk *animationsChunk = new AnimationsChunk(); + for (long i = 0; i < m_animations.size(); ++i) { - long data; - const Md2Animation *animation = &m_animations[i]; - //fputs(animation->name.c_str(), fp); - fputs(animation->name.c_str(), fp); - fwrite("\0", 1, 1, fp); - data = animation->startFrame; - fwrite(&data, sizeof(long), 1, fp); - data = animation->endFrame; - fwrite(&data, sizeof(long), 1, fp); + AnimationSequence a; + + a.name = m_animations[i].name; + a.start = m_animations[i].startFrame; + a.end = m_animations[i].endFrame; + + animationsChunk->animations.push_back(a); } + WriteChunk(animationsChunk, fp); } fclose(fp);