-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
208 lines (164 loc) · 6.38 KB
/
Copy pathModel.cpp
File metadata and controls
208 lines (164 loc) · 6.38 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Credit to: LearnOpenGL.com
#include "Model.h"
Model::Model(const char* path, Shader* shader) : shader_(shader) {
load(path);
}
void Model::load(const std::string& path) {
Assimp::Importer import;
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
return;
}
dir = path.substr(0, path.find_last_of('/'));
process_node(scene->mRootNode, scene);
}
void Model::update(GameObject& g) {
glm::mat4 matrix(g.get_matrix());
//if(g.has_collision) {
glm::vec3 pos(g.get_position());
g.bounds = new AABB(min + pos, max + pos, centre + pos);
//}
shader_->use();
shader_->setMat4("model", matrix);
for(unsigned int i = 0; i < meshes.size(); i++) {
meshes[i].Draw(*shader_, g.wire);
}
}
void Model::receive(std::string component, std::string action) {
if(component != "Model") { return; }
if(action == "scale") {}
}
void Model::activate() {
}
void Model::deactivate() {
}
void Model::process_node(aiNode* node, const aiScene* scene) {
// Parent nodes
unsigned int i = 0;
for(i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(process_mesh(mesh, scene));
}
centre *= (float) (1.0f / (float) i);
// Child nodes, recursively process all nodes and meshes.
for(i = 0; i < node->mNumChildren; i++) {
process_node(node->mChildren[i], scene);
}
}
Mesh Model::process_mesh(aiMesh* mesh, const aiScene* scene) {
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
glm::vec3 temp_avg {0};
unsigned i = 0;
for(i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex;
// Vertex positions, Normals, Textures-coords
vertex.Position = glm::vec3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
vertex.Normal = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
vertex.TexCoords = (mesh->mTextureCoords[0]) ? glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y) : glm::vec2(0, 0);
// Store Bounday information
temp_avg += vertex.Position;
if(min.x > vertex.Position.x) { min.x = vertex.Position.x; }
if(min.y > vertex.Position.y) { min.y = vertex.Position.y; }
if(min.z > vertex.Position.z) { min.z = vertex.Position.z; }
if(max.x < vertex.Position.x) { max.x = vertex.Position.x; }
if(max.y < vertex.Position.y) { max.y = vertex.Position.y; }
if(max.z < vertex.Position.z) { max.z = vertex.Position.z; }
// Push the vertex to the vector list
vertices.push_back(vertex);
}
centre += (temp_avg * ((float) (1.0f / (float) i)));
// Indices; Each mesh has faces, each face has indices for each vertex
for(i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for(unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
// TODO: process material
if(mesh->mMaterialIndex >= 0) {
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
// Get and insert all diffuse textures into the texture vector
std::vector<Texture> diffuse_map = load_material_textures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuse_map.begin(), diffuse_map.end());
// Get and insert all specular textures into the texture vector
std::vector<Texture> specular_map = load_material_textures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specular_map.begin(), specular_map.end());
}
// Get the centre average of the shape
return Mesh(vertices, indices, textures);
}
std::vector<Texture> Model::load_material_textures(aiMaterial* mat, aiTextureType type, std::string name) {
std::vector<Texture> textures;
unsigned texture_count = mat->GetTextureCount(type);
if(texture_count <= 0 && type != aiTextureType_SPECULAR) {
Texture texture;
texture.id = texture_from_file("resources/graphics_objects/_blank.jpg", dir);
texture.type = type;
texture.path = "resources/graphics_objects/_blank.jpg";
textures.push_back(texture);
}
for(unsigned int i = 0; i < texture_count; i++) {
aiString str;
mat->GetTexture(type, i, &str);
bool skip = false;
for(unsigned int j = 0; j < loaded_textures.size(); j++) {
if(std::strcmp(loaded_textures[j].path.data(), str.C_Str()) == 0) {
textures.push_back(loaded_textures[j]);
skip = true;
break;
}
}
if(!skip) {
Texture texture;
texture.id = texture_from_file(str.C_Str(), dir);
texture.type = type;
texture.path = str.C_Str();
textures.push_back(texture);
}
}
return textures;
}
unsigned int Model::texture_from_file(const char* path, const std::string& dir, bool gamma) {
std::string filename = dir + '/' + std::string(path);
unsigned int textureID;
glGenTextures(1, &textureID);
// Load and store the texture in a char array
int width, height, components;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &components, 0);
if(!data) {
// if loading from obj data does not work, try loading direct from obj location
filename = dir + '/' + std::string(path).substr(std::string(path).find_last_of("/\\") + 1);
data = stbi_load(filename.c_str(), &width, &height, &components, 0);
if(!data) {
// If there is still no texture file found, just throw an error
std::cout << "Model::texture_from_file: Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
return textureID;
}
}
GLenum format;
// Check the type of texture then set the format
if(components == 1) {
format = GL_RED;
} else if(components == 3) {
format = GL_RGB;
} else if(components == 4) {
format = GL_RGBA;
}
// Standard binding of textureID, and size information
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// How the texture will be displayed in different situations like wrapping etc
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Free up the space used
stbi_image_free(data);
// return the textureID as it is bound and can be referred to through its ID
return textureID;
}