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);
|
fputs("KFR", fp);
|
||||||
fwrite(&size, 4, 1, fp);
|
fwrite(&size, 4, 1, fp);
|
||||||
uint32_t count = chunk->GetCount();
|
uint32_t numFrames = chunk->GetNumFrames();
|
||||||
fwrite(&count, 4, 1, fp);
|
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);
|
for (uint32_t j = 0; j < chunk->numVertices; ++j)
|
||||||
fwrite(&f->vertex.y, sizeof(float), 1, fp);
|
{
|
||||||
fwrite(&f->vertex.z, sizeof(float), 1, fp);
|
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);
|
for (uint32_t j = 0; j < chunk->numVertices; ++j)
|
||||||
fwrite(&f->normal.y, sizeof(float), 1, fp);
|
{
|
||||||
fwrite(&f->normal.z, sizeof(float), 1, fp);
|
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
|
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();
|
return frames.size();
|
||||||
}
|
}
|
||||||
|
@ -20,11 +39,14 @@ struct KeyFramesChunk
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
uint32_t size = 4; // count
|
uint32_t size = 4; // count
|
||||||
size +=
|
for (uint32_t i = 0; i < frames.size(); ++i)
|
||||||
(
|
{
|
||||||
(sizeof(float) * 3) // vertex
|
size +=
|
||||||
+ (sizeof(float) * 3) // normal
|
(
|
||||||
) * frames.size();
|
(sizeof(float) * 3) // vertex
|
||||||
|
+ (sizeof(float) * 3) // normal
|
||||||
|
) * frames[i]->count;
|
||||||
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,28 @@
|
||||||
#ifndef __GEOMETRY_KEYFRAME_H_INCLUDED__
|
#ifndef __GEOMETRY_KEYFRAME_H_INCLUDED__
|
||||||
#define __GEOMETRY_KEYFRAME_H_INCLUDED__
|
#define __GEOMETRY_KEYFRAME_H_INCLUDED__
|
||||||
|
|
||||||
|
#include "../common.h"
|
||||||
#include "vector3.h"
|
#include "vector3.h"
|
||||||
|
|
||||||
class KeyFrame
|
class KeyFrame
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Vector3 vertex;
|
uint32_t count;
|
||||||
Vector3 normal;
|
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
|
#endif
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "md2.h"
|
#include "md2.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "../chunks/chunks.h"
|
||||||
|
|
||||||
Md2::Md2()
|
Md2::Md2()
|
||||||
{
|
{
|
||||||
|
@ -297,104 +298,59 @@ bool Md2::ConvertToMesh(const std::string &file)
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fputs("MESH", fp);
|
|
||||||
unsigned char version = 1;
|
|
||||||
fwrite(&version, 1, 1, fp);
|
|
||||||
|
|
||||||
// keyframes chunk
|
WriteFileHeader(fp);
|
||||||
fputs("KFR", fp);
|
|
||||||
long numFrames = m_numFrames;
|
KeyFramesChunk *keyFramesChunk = new KeyFramesChunk();
|
||||||
long numVertices = m_numVertices;
|
keyFramesChunk->numVertices = m_numVertices;
|
||||||
long sizeofFrames = ((sizeof(float) * 3 * 2) * numVertices) * numFrames + (sizeof(long) * 2);
|
for (long i = 0; i < m_numFrames; ++i)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
// vertices
|
KeyFrame *frame = keyFramesChunk->AddFrame();
|
||||||
|
|
||||||
for (int j = 0; j < m_numVertices; ++j)
|
for (int j = 0; j < m_numVertices; ++j)
|
||||||
{
|
frame->vertices[j] = m_frames[i].vertices[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
|
|
||||||
for (int j = 0; j < m_numVertices; ++j)
|
for (int j = 0; j < m_numVertices; ++j)
|
||||||
{
|
frame->normals[j] = m_frames[i].normals[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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
WriteChunk(keyFramesChunk, fp);
|
||||||
|
|
||||||
// textures chunk
|
TexCoordsChunk *texCoordsChunk = new TexCoordsChunk();
|
||||||
fputs("KTX", fp);
|
for (long i = 0; i < m_numTexCoords; ++i)
|
||||||
long numTexCoords = m_numTexCoords;
|
texCoordsChunk->texCoords.push_back(m_texCoords[i]);
|
||||||
long sizeofTexCoords = (sizeof(float) * 2) * numTexCoords + sizeof(long);
|
WriteChunk(texCoordsChunk, fp);
|
||||||
fwrite(&sizeofTexCoords, sizeof(long), 1, fp);
|
|
||||||
fwrite(&numTexCoords, sizeof(long), 1, fp);
|
KeyFrameTrianglesChunk *trianglesChunk = new KeyFrameTrianglesChunk();
|
||||||
for (long i = 0; i < numTexCoords; ++i)
|
for (long i = 0; i < m_numPolys; ++i)
|
||||||
{
|
{
|
||||||
const Vector2 *texCoord = &m_texCoords[i];
|
KeyFrameTriangle t;
|
||||||
fwrite(&texCoord->x, sizeof(float), 1, fp);
|
|
||||||
fwrite(&texCoord->y, sizeof(float), 1, fp);
|
t.vertices[0] = m_polys[i].vertex[0];
|
||||||
}
|
t.vertices[1] = m_polys[i].vertex[1];
|
||||||
|
t.vertices[2] = m_polys[i].vertex[2];
|
||||||
// triangles chunk
|
|
||||||
fputs("KTR", fp);
|
t.texCoords[0] = m_polys[i].texCoord[0];
|
||||||
long numPolys = m_numPolys;
|
t.texCoords[1] = m_polys[i].texCoord[1];
|
||||||
long sizeofPolys = (sizeof(long) * 3 * 2) * numPolys + sizeof(long);
|
t.texCoords[2] = m_polys[i].texCoord[2];
|
||||||
fwrite(&sizeofPolys, sizeof(long), 1, fp);
|
|
||||||
fwrite(&numPolys, sizeof(long), 1, fp);
|
trianglesChunk->triangles.push_back(t);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
WriteChunk(trianglesChunk, fp);
|
||||||
|
|
||||||
if (m_animations.size() > 0)
|
if (m_animations.size() > 0)
|
||||||
{
|
{
|
||||||
// figure out the size of all the animation name strings
|
AnimationsChunk *animationsChunk = new AnimationsChunk();
|
||||||
long sizeofNames = 0;
|
for (long i = 0; i < m_animations.size(); ++i)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
long data;
|
AnimationSequence a;
|
||||||
const Md2Animation *animation = &m_animations[i];
|
|
||||||
//fputs(animation->name.c_str(), fp);
|
a.name = m_animations[i].name;
|
||||||
fputs(animation->name.c_str(), fp);
|
a.start = m_animations[i].startFrame;
|
||||||
fwrite("\0", 1, 1, fp);
|
a.end = m_animations[i].endFrame;
|
||||||
data = animation->startFrame;
|
|
||||||
fwrite(&data, sizeof(long), 1, fp);
|
animationsChunk->animations.push_back(a);
|
||||||
data = animation->endFrame;
|
|
||||||
fwrite(&data, sizeof(long), 1, fp);
|
|
||||||
}
|
}
|
||||||
|
WriteChunk(animationsChunk, fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
Reference in a new issue