From 05470a99b5e0f869e27289002a58e07ec2c4104e Mon Sep 17 00:00:00 2001 From: gered Date: Wed, 23 Jun 2010 22:32:24 -0400 Subject: [PATCH] implemented SM to MESH file conversion added extra console messages to indicate what is being done --- MeshConverter/src/main.cpp | 10 +++- MeshConverter/src/sm/sm.cpp | 101 +++++++++++++++++++++++++++++++++++- MeshConverter/src/sm/sm.h | 2 +- 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/MeshConverter/src/main.cpp b/MeshConverter/src/main.cpp index f1b9152..2a6454c 100644 --- a/MeshConverter/src/main.cpp +++ b/MeshConverter/src/main.cpp @@ -41,6 +41,8 @@ int main(int argc, char **argv) if (extension == ".obj") { + printf("Using OBJ converter.\n"); + Obj *obj = new Obj(); if (!obj->Load(file, "./")) { @@ -55,6 +57,8 @@ int main(int argc, char **argv) } else if (extension == ".md2") { + printf("Using MD2 converter.\n"); + Md2 *md2 = new Md2(); if (!md2->Load(file)) { @@ -69,8 +73,10 @@ int main(int argc, char **argv) } else if (extension == ".sm") { + printf("Using SM converer.\n"); + StaticModel *sm = new StaticModel(); - if (!sm->Load(file, "./")) + if (!sm->Load(file)) { printf("Error loading SM file.\n\n"); return 1; @@ -87,6 +93,8 @@ int main(int argc, char **argv) return 1; } + printf("Finished converting to %s\n", meshFile.c_str()); + return 0; } \ No newline at end of file diff --git a/MeshConverter/src/sm/sm.cpp b/MeshConverter/src/sm/sm.cpp index 39bba20..986fb45 100644 --- a/MeshConverter/src/sm/sm.cpp +++ b/MeshConverter/src/sm/sm.cpp @@ -28,7 +28,7 @@ void StaticModel::Release() delete[] m_normals; } -bool StaticModel::Load(const std::string &file, const std::string &texturePath) +bool StaticModel::Load(const std::string &file) { FILE *fp; unsigned short numMaterials; @@ -97,7 +97,7 @@ bool StaticModel::Load(const std::string &file, const std::string &texturePath) if (c) texture += c; } while (c != '\0'); - m_materials[i].material->SetTexture(texturePath + texture); + m_materials[i].material->SetTexture(texture); } // Read in triangle definitions (all are indexes into raw data following) @@ -217,6 +217,103 @@ bool StaticModel::ConvertToMesh(const std::string &file) unsigned char version = 1; fwrite(&version, 1, 1, fp); + // vertices chunk + fputs("VTX", fp); + long numVertices = m_numVertices; + long sizeofVertices = (sizeof(float) * 3) * numVertices + sizeof(long); + fwrite(&sizeofVertices, sizeof(long), 1, fp); + fwrite(&numVertices, sizeof(long), 1, fp); + for (long i = 0; i < numVertices; ++i) + { + const Vector3 *vector = &m_vertices[i]; + fwrite(&vector->x, sizeof(float), 1, fp); + fwrite(&vector->y, sizeof(float), 1, fp); + fwrite(&vector->z, sizeof(float), 1, fp); + } + + // normals chunk + fputs("NRL", fp); + long numNormals = m_numNormals; + long sizeofNormals = (sizeof(float) * 3) * numNormals + sizeof(long); + fwrite(&sizeofNormals, sizeof(long), 1, fp); + fwrite(&numNormals, sizeof(long), 1, fp); + for (long i = 0; i < numNormals; ++i) + { + const Vector3 *normal = &m_normals[i]; + fwrite(&normal->x, sizeof(float), 1, fp); + fwrite(&normal->y, sizeof(float), 1, fp); + fwrite(&normal->z, sizeof(float), 1, fp); + } + + // texture coordinates chunk + fputs("TXT", 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) + { + const Vector2 *texCoord = &m_texCoords[i]; + fwrite(&texCoord->x, sizeof(float), 1, fp); + fwrite(&texCoord->y, sizeof(float), 1, fp); + } + + // materials chunk + fputs("MTL", fp); + + long numMaterials = m_numMaterials; + + // figure out the size of all the material texture filename strings + long sizeofNames = 0; + for (int i = 0; i < numMaterials; ++i) + sizeofNames += m_materials[i].material->GetTexture().length() + 1; + + long sizeofMaterials = numMaterials + sizeof(long); + fwrite(&sizeofMaterials, sizeof(long), 1, fp); + fwrite(&numMaterials, sizeof(long), 1, fp); + for (long i = 0; i < numMaterials; ++i) + { + const SmMaterial *material = &m_materials[i]; + fputs(material->material->GetTexture().c_str(), fp); + fwrite("\0", 1, 1, fp); + } + + // triangles chunk + fputs("TRI", fp); + long numPolys = m_numPolygons; + long sizeofPolys = ((sizeof(long) * 3) * 3 + sizeof(long)) * numPolys + sizeof(long); + fwrite(&sizeofPolys, sizeof(long), 1, fp); + fwrite(&numPolys, sizeof(long), 1, fp); + for (long i = 0; i < numPolys; ++i) + { + const SmPolygon *triangle = &m_polygons[i]; + long data; + + data = triangle->vertices[0]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->vertices[1]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->vertices[2]; + fwrite(&data, sizeof(long), 1, fp); + + data = triangle->normals[0]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->normals[1]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->normals[2]; + fwrite(&data, sizeof(long), 1, fp); + + data = triangle->texcoords[0]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->texcoords[1]; + fwrite(&data, sizeof(long), 1, fp); + data = triangle->texcoords[2]; + fwrite(&data, sizeof(long), 1, fp); + + data = triangle->material; + fwrite(&data, sizeof(long), 1, fp); + } + fclose(fp); return true; } diff --git a/MeshConverter/src/sm/sm.h b/MeshConverter/src/sm/sm.h index 39311bd..ade4af7 100644 --- a/MeshConverter/src/sm/sm.h +++ b/MeshConverter/src/sm/sm.h @@ -44,7 +44,7 @@ public: virtual ~StaticModel() { Release(); } void Release(); - bool Load(const std::string &file, const std::string &texturePath); + bool Load(const std::string &file); bool ConvertToMesh(const std::string &file); SmMaterial* GetMaterial(unsigned short index) { return &m_materials[index]; }