1+ #include < iostream>
2+ #include < vector>
3+ #include < cassert>
4+ #include < string>
5+ #include < fstream>
6+ #include < assimp/Importer.hpp>
7+ #include < assimp/scene.h>
8+ #include < assimp/postprocess.h>
9+
10+ struct Position {
11+ float x, y, z;
12+ };
13+
14+ std::vector<Position> positions;
15+ std::vector<uint32_t > indices;
16+
17+ void processMesh (aiMesh* mesh, const aiScene* scene) {
18+ for (unsigned int i = 0 ; i < mesh->mNumVertices ; i++) {
19+ Position vertex;
20+ vertex.x = mesh->mVertices [i].x ;
21+ vertex.y = mesh->mVertices [i].y ;
22+ vertex.z = mesh->mVertices [i].z ;
23+ positions.push_back (vertex);
24+ }
25+
26+ for (unsigned int i = 0 ; i < mesh->mNumFaces ; i++) {
27+ aiFace face = mesh->mFaces [i];
28+ assert (face.mNumIndices == 3 );
29+ for (unsigned int j = 0 ; j < face.mNumIndices ; j++) {
30+ indices.push_back (face.mIndices [j]);
31+ }
32+ }
33+ }
34+
35+ void processNode (aiNode* node, const aiScene* scene) {
36+
37+ for (unsigned int i = 0 ; i < node->mNumMeshes ; i++) {
38+ aiMesh* mesh = scene->mMeshes [node->mMeshes [i]];
39+ processMesh (mesh, scene);
40+ }
41+
42+ for (unsigned int i = 0 ; i < node->mNumChildren ; i++) {
43+ processNode (node->mChildren [i], scene);
44+ }
45+ }
46+
47+ char * getFilename (char * filename) {
48+ int len = strlen (filename);
49+ char * lastSlash = filename;
50+ for (int i = 0 ; i < len; i++) {
51+ if (filename[i] == ' /' || filename[i] == ' \\ ' ) {
52+ lastSlash = filename+i+1 ;
53+ }
54+ }
55+ return lastSlash;
56+ }
57+
58+ int main (int argc, char ** argv) {
59+ if (argc <= 0 ) {
60+ return 1 ;
61+ }
62+ if (argc < 2 ) {
63+ std::cout << " Usage: " << argv[0 ] << " <modelfilename>" << std::endl;
64+ return 1 ;
65+ }
66+
67+ Assimp::Importer importer;
68+ const aiScene* scene = importer.ReadFile (argv[argc-1 ], aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph | aiProcess_JoinIdenticalVertices | aiProcess_ImproveCacheLocality);
69+ if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE, !scene->mRootNode ) {
70+ std::cout << " Error while loading model with assimp: " << importer.GetErrorString () << std::endl;
71+ return 1 ;
72+ }
73+
74+ processNode (scene->mRootNode , scene);
75+
76+ std::string filename = std::string (getFilename (argv[argc-1 ]));
77+ std::string filenameWithoutExtension = filename.substr (0 , filename.find_last_of (' .' ));
78+ std::string outputFilename = filenameWithoutExtension + " .bmf" ;
79+
80+ std::ofstream output (outputFilename, std::ios::out | std::ios::binary);
81+ std::cout << " Writing bmf file..." << std::endl;
82+ uint64_t numVertices = positions.size ();
83+ uint64_t numIndices = indices.size ();
84+ output.write ((char *)&numVertices, sizeof (uint64_t ));
85+ output.write ((char *)&numIndices, sizeof (uint64_t ));
86+ for (uint64_t i = 0 ; i < numVertices; i++) {
87+ output.write ((char *)&positions[i].x , sizeof (float ));
88+ output.write ((char *)&positions[i].y , sizeof (float ));
89+ output.write ((char *)&positions[i].z , sizeof (float ));
90+ }
91+ for (uint64_t i = 0 ; i < numIndices; i++) {
92+ output.write ((char *)&indices[i], sizeof (uint32_t ));
93+ }
94+ output.close ();
95+ }
0 commit comments