Skip to content

Commit 6c000ad

Browse files
authored
Merge pull request #21 from Pilzschaf/tutorial26-modelloading
Model loading
2 parents a299ce7 + ce7fc07 commit 6c000ad

5 files changed

Lines changed: 129 additions & 15 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*.out
3232
*.app
3333
opengl_tutorial
34+
tools/modelexporter
3435

3536
# Dolphin
3637
.directory

main.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22
#include <cmath>
3+
#include <vector>
4+
#include <fstream>
35
#define GLEW_STATIC
46
#include <GL/glew.h>
57
#define SDL_MAIN_HANDLED
@@ -77,24 +79,40 @@ int main(int argc, char** argv) {
7779
glDebugMessageCallback(openGLDebugCallback, 0);
7880
#endif
7981

80-
Vertex vertices[] = {
81-
Vertex{-0.5f, -0.5f, -0.0f,
82-
1.0f, 0.0f, 0.0f, 1.0f},
83-
Vertex{0.5f, -0.5f, -0.0f,
84-
0.0, 1.0f, 0.0f, 1.0f},
85-
Vertex{0.0f, 0.5f, -0.0f,
86-
0.0f, 0.0f, 1.0f, 1.0f}
87-
};
88-
uint32 numVertices = 3;
82+
std::vector<Vertex> vertices;
83+
uint64 numVertices = 0;
84+
85+
std::vector<uint32> indices;
86+
uint64 numIndices = 0;
8987

90-
uint32 indices[] = {
91-
0, 1, 2
92-
};
93-
uint32 numIndices = 3;
88+
std::ifstream input = std::ifstream("models/monkey.bmf", std::ios::in | std::ios::binary);
89+
if(!input.is_open()) {
90+
std::cout << "Error reading model file" << std::endl;
91+
return 1;
92+
}
93+
input.read((char*)&numVertices, sizeof(uint64));
94+
input.read((char*)&numIndices, sizeof(uint64));
95+
96+
for(uint64 i = 0; i < numVertices; i++) {
97+
Vertex vertex;
98+
input.read((char*)&vertex.x, sizeof(float));
99+
input.read((char*)&vertex.y, sizeof(float));
100+
input.read((char*)&vertex.z, sizeof(float));
101+
vertex.r = 1.0f;
102+
vertex.g = 1.0f;
103+
vertex.b = 1.0f;
104+
vertex.a = 1.0f;
105+
vertices.push_back(vertex);
106+
}
107+
for(uint64 i = 0; i < numIndices; i++) {
108+
uint32 index;
109+
input.read((char*)&index, sizeof(uint32));
110+
indices.push_back(index);
111+
}
94112

95-
IndexBuffer indexBuffer(indices, numIndices, sizeof(indices[0]));
113+
IndexBuffer indexBuffer(indices.data(), numIndices, sizeof(indices[0]));
96114

97-
VertexBuffer vertexBuffer(vertices, numVertices);
115+
VertexBuffer vertexBuffer(vertices.data(), numVertices);
98116
vertexBuffer.unbind();
99117

100118
Shader shader("shaders/basic.vs", "shaders/basic.fs");

models/cube.bmf

448 Bytes
Binary file not shown.

models/monkey.bmf

45 KB
Binary file not shown.

tools/modelexporter.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)