142 lines
No EOL
4 KiB
C++
142 lines
No EOL
4 KiB
C++
#pragma once
|
|
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
#include "assimpimporter.hpp"
|
|
#include <iostream>
|
|
|
|
//TODO: remove the need for pointer magic in order to parse model files
|
|
class ModelImporter
|
|
{
|
|
public:
|
|
static ImportedModel Import(const std::string& path)
|
|
{
|
|
//if the filesystem isn't packed, that means the model files aren't converted either
|
|
if(!Filesystem::IsPacked())
|
|
{
|
|
return AssimpImporter::Import(path);
|
|
}
|
|
else
|
|
{
|
|
int size;
|
|
File handle = Filesystem::GetFileHandle(Utility::RemoveFileExtension(path) + ".model", size);
|
|
if(!handle)
|
|
{
|
|
Log::Error("Failed to open %s", (Utility::RemoveFileExtension(path) + ".model").c_str());
|
|
return ImportedModel();
|
|
}
|
|
|
|
std::vector<Submesh> submeshes;
|
|
|
|
const char* signature = "GRAPT";
|
|
|
|
char* fileSignature = new char[6];
|
|
memcpy(fileSignature, handle, 6);
|
|
if(strcmp(signature, fileSignature))
|
|
{
|
|
Log::Error("Invalid signature!");
|
|
return ImportedModel();
|
|
}
|
|
|
|
handle += sizeof(signature);
|
|
|
|
unsigned int numMeshes;
|
|
memcpy(&numMeshes, handle, sizeof(unsigned int));
|
|
|
|
handle += sizeof(unsigned int);
|
|
|
|
const char* terminator = "m";
|
|
|
|
for(unsigned int i = 0; i < numMeshes; i++)
|
|
{
|
|
Submesh mesh;
|
|
|
|
handle += sizeof(terminator);
|
|
|
|
unsigned int numVerts;
|
|
memcpy(&numVerts, handle, sizeof(unsigned int));
|
|
|
|
handle += sizeof(unsigned int);
|
|
|
|
unsigned int numIndices;
|
|
memcpy(&numIndices, handle, sizeof(unsigned int));
|
|
|
|
handle += sizeof(unsigned int);
|
|
|
|
for(unsigned int v = 0; v < numVerts; v++)
|
|
{
|
|
Vertex vertex;
|
|
memcpy(&vertex, handle, sizeof(Vertex));
|
|
|
|
handle += sizeof(Vertex);
|
|
|
|
mesh.vertices.push_back(vertex);
|
|
}
|
|
|
|
for(unsigned int e = 0; e < numIndices; e++)
|
|
{
|
|
unsigned int indice;
|
|
memcpy(&indice, handle, sizeof(unsigned int));
|
|
|
|
handle += sizeof(unsigned int);
|
|
|
|
mesh.indices.push_back(indice);
|
|
}
|
|
|
|
submeshes.push_back(mesh);
|
|
}
|
|
|
|
ImportedModel model;
|
|
model.submeshes = submeshes;
|
|
|
|
return model;
|
|
}
|
|
}
|
|
|
|
static void Export(const std::string& inpath, const std::string& outpath)
|
|
{
|
|
auto submeshes = Import(inpath).submeshes;
|
|
|
|
FILE* fp = fopen(outpath.c_str(), "wb");
|
|
if(!fp)
|
|
{
|
|
Log::Error("Failed to open file %s", outpath.c_str());
|
|
return;
|
|
}
|
|
|
|
const char* signature = "GRAPT"; //signature used to check if they are our models
|
|
fwrite(signature, sizeof(signature), 1, fp);
|
|
|
|
const char* terminator = "m"; //terminator used for splitting meshes
|
|
|
|
unsigned int numMeshes = submeshes.size();
|
|
fwrite(&numMeshes, sizeof(unsigned int), 1, fp);
|
|
|
|
for(unsigned int i = 0; i < submeshes.size(); i++)
|
|
{
|
|
Submesh mesh = submeshes[i];
|
|
|
|
//write a terminator to separate meshes
|
|
fwrite(terminator, sizeof(terminator), 1, fp);
|
|
|
|
unsigned int numVertices = mesh.vertices.size();
|
|
fwrite(&numVertices, sizeof(unsigned int), 1, fp);
|
|
|
|
unsigned int numIndices = mesh.indices.size();
|
|
fwrite(&numIndices, sizeof(unsigned int), 1, fp);
|
|
|
|
for(unsigned int v = 0; v < mesh.vertices.size(); v++)
|
|
{
|
|
fwrite(&mesh.vertices[v], sizeof(Vertex), 1, fp);
|
|
}
|
|
|
|
for(unsigned int e = 0; e < mesh.indices.size(); e++)
|
|
{
|
|
fwrite(&mesh.indices[e], sizeof(unsigned int), 1, fp);
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
}
|
|
}; |