updated keyframe data structures, and updated md2 mesh exporting to use the chunk structs
This commit is contained in:
parent
1165b7b0f9
commit
978f2aa08e
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,9 +7,28 @@
|
|||
|
||||
struct KeyFramesChunk
|
||||
{
|
||||
std::vector<KeyFrame> frames;
|
||||
std::vector<KeyFrame*> 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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "md2.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
|
|
Reference in a new issue