1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
}
|