diff options
Diffstat (limited to 'temporary/converter.c')
| -rw-r--r-- | temporary/converter.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/temporary/converter.c b/temporary/converter.c new file mode 100644 index 0000000..ce91965 --- /dev/null +++ b/temporary/converter.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assimp/cimport.h> +#include <assimp/scene.h> +#include <assimp/postprocess.h> + +void export_as_obj(const struct aiScene* scene, const char* output_filename) { + FILE* file = fopen(output_filename, "w"); + if (!file) { + fprintf(stderr, "Error opening output file: %s\n", output_filename); + return; + } + + fprintf(file, "# OBJ file exported from MD3 using Assimp\n"); + + // Write vertices + for (unsigned int m = 0; m < scene->mNumMeshes; m++) { + const struct aiMesh* mesh = scene->mMeshes[m]; + for (unsigned int v = 0; v < mesh->mNumVertices; v++) { + fprintf(file, "v %f %f %f\n", + mesh->mVertices[v].x, + mesh->mVertices[v].y, + mesh->mVertices[v].z); + } + } + + // Write faces (indices) + unsigned int vertex_offset = 1; // OBJ indices start at 1 + for (unsigned int m = 0; m < scene->mNumMeshes; m++) { + const struct aiMesh* mesh = scene->mMeshes[m]; + fprintf(file, "o Mesh_%d\n", m); + + for (unsigned int f = 0; f < mesh->mNumFaces; f++) { + const struct aiFace face = mesh->mFaces[f]; + fprintf(file, "f"); + + for (unsigned int i = 0; i < face.mNumIndices; i++) { + fprintf(file, " %u", face.mIndices[i] + vertex_offset); + } + + fprintf(file, "\n"); + } + + vertex_offset += mesh->mNumVertices; + } + + fclose(file); + printf("Successfully exported to: %s\n", output_filename); +} + +int main(int argc, char** argv) { + if (argc != 3) { + printf("Usage: %s <input.md3> <output.obj>\n", argv[0]); + return 1; + } + + const char* input_file = argv[1]; + const char* output_file = argv[2]; + + // Import the MD3 file + const struct aiScene* scene = aiImportFile( + input_file, + aiProcess_Triangulate | // Ensure triangles + aiProcess_JoinIdenticalVertices // Combine duplicate vertices + ); + + if (!scene) { + fprintf(stderr, "Error loading MD3 file: %s\n", aiGetErrorString()); + return 1; + } + + printf("Successfully loaded MD3 file: %s\n", input_file); + printf("Meshes: %u\n", scene->mNumMeshes); + + // Export as OBJ + export_as_obj(scene, output_file); + + // Clean up + aiReleaseImport(scene); + + return 0; +} |
