forked from cricklet/Hatched
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
141 lines (116 loc) · 3.6 KB
/
Copy pathmodel.cpp
File metadata and controls
141 lines (116 loc) · 3.6 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "model.h"
#include "mesh.h"
#include "helper.h"
template<typename Func>
static void recursivelyProcess(aiNode* node, const aiScene* scene, Func processMesh) {
for (GLuint i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
processMesh(mesh, scene);
}
for (GLuint i = 0; i < node->mNumChildren; i++) {
aiNode *child = node->mChildren[i];
recursivelyProcess(child, scene, processMesh);
}
}
static shared_ptr<Mesh> generateMesh(aiMesh *mesh, const aiScene *scene) {
vector<Vertex> vertices;
vector<GLuint> indices;
// Process vertices
for (GLuint i = 0; i < mesh->mNumVertices; i++) {
aiVector3D vertex = mesh->mVertices[i];
aiVector3D normal = mesh->mNormals[i];
Vertex v;
v.position.x = vertex.x;
v.position.y = vertex.y;
v.position.z = vertex.z;
v.normal.x = normal.x;
v.normal.y = normal.y;
v.normal.z = normal.z;
if (mesh->mMaterialIndex >= 0) {
aiVector3D uv = mesh->mTextureCoords[0][i];
v.uv.x = uv.x;
v.uv.y = uv.y;
} else {
v.uv.x = 0;
v.uv.y = 0;
}
vertices.push_back(v);
}
// Process indices
for (GLuint i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (GLuint j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
return make_shared<Mesh>(vertices, indices);
}
static Bounds computeBounds(vector<shared_ptr<Mesh>> meshes) {
auto overall = meshes[0]->GetBounds();
for (auto mesh : meshes) {
auto local = mesh->GetBounds();
if (local.minx < overall.minx)
overall.minx = local.minx;
if (local.maxx > overall.maxx)
overall.maxx = local.maxx;
if (local.miny < overall.miny)
overall.miny = local.miny;
if (local.maxy > overall.maxy)
overall.maxy = local.maxy;
if (local.minz < overall.minz)
overall.minz = local.minz;
if (local.maxz > overall.maxz)
overall.maxz = local.maxz;
}
return overall;
}
static float maxDimension(Bounds b) {
return max(max(b.maxx - b.minx, b.maxy - b.miny), b.maxz - b.minz);
}
Model::Model(string path, glm::mat4 transform) {
if (path.length() == 0) {
return;
}
Assimp::Importer importer;
unsigned int flags = aiProcess_GenNormals | aiProcess_Triangulate | aiProcess_FlipUVs;
const aiScene* scene = importer.ReadFile(path, flags);
if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
cerr << "Couldn't load scene: " << importer.GetErrorString() << endl;
return;
}
string directory = path.substr(0, path.find_last_of('/'));
auto addMesh = [&] (aiMesh *mesh, const aiScene *scene) {
this->meshes.push_back(generateMesh(mesh, scene));
};
recursivelyProcess(scene->mRootNode, scene, addMesh);
this->bounds = computeBounds(this->meshes);
/*cout << "Model bounds: "
<< this->bounds.minx << ", "
<< this->bounds.miny << ", "
<< this->bounds.minz << " - "
<< this->bounds.maxx << ", "
<< this->bounds.maxy << ", "
<< this->bounds.maxz << "\n";*/
this->transform = transform;
}
void Model::Render(Uniforms uniforms, glm::mat4 parentTransform) {
auto modelTransform = parentTransform * this->transform;
for (auto m : this->meshes) {
m->Render(uniforms, modelTransform);
checkErrors();
}
}
float Model::GetSize() {
return maxDimension(this->bounds);
}
Bounds Model::GetBounds() {
return this->bounds;
}
void Model::SetTransform(glm::mat4 transform) {
this->transform = transform;
}
void Model::BindToShader(GLuint shaderProgram) {
for (auto m : this->meshes) {
m->BindToShader(shaderProgram);
checkErrors();
}
}