|
|
|
@ -1,5 +1,6 @@
@@ -1,5 +1,6 @@
|
|
|
|
|
#include "mesh.h" |
|
|
|
|
#include "stb_image.h" |
|
|
|
|
#include "glerror.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mesh::Mesh(std::vector<struct vertex> vertices, std::vector<unsigned int> indices,
|
|
|
|
@ -36,16 +37,19 @@ void Mesh::setupMesh()
@@ -36,16 +37,19 @@ void Mesh::setupMesh()
|
|
|
|
|
glEnableVertexAttribArray(2);
|
|
|
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)offsetof(vertex, texture_coords)); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
glBindVertexArray(0); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Mesh::draw(Shader &shader) { |
|
|
|
|
glCheckError(); |
|
|
|
|
unsigned int specularNr = 1; |
|
|
|
|
unsigned int diffuseNr = 1; |
|
|
|
|
for(unsigned int i = 0; i < textures.size(); i++) |
|
|
|
|
{ |
|
|
|
|
glActiveTexture(GL_TEXTURE0 + i); // activate proper texture unit before binding
|
|
|
|
|
glCheckError(); |
|
|
|
|
// retrieve texture number (the N in diffuse_textureN)
|
|
|
|
|
std::string number; |
|
|
|
|
std::string name = textures[i].type; |
|
|
|
@ -54,15 +58,18 @@ void Mesh::draw(Shader &shader) {
@@ -54,15 +58,18 @@ void Mesh::draw(Shader &shader) {
|
|
|
|
|
else if(name == "texture_specular") |
|
|
|
|
number = std::to_string(specularNr++); |
|
|
|
|
|
|
|
|
|
shader.setFloat(("material." + name + number).c_str(), i); |
|
|
|
|
glCheckError(); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[i].id); |
|
|
|
|
glCheckError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// draw mesh
|
|
|
|
|
glCheckError(); |
|
|
|
|
glBindVertexArray(VAO); |
|
|
|
|
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); |
|
|
|
|
glBindVertexArray(0); |
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
|
glCheckError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -90,6 +97,7 @@ void Model::load_model(std::string path) {
@@ -90,6 +97,7 @@ void Model::load_model(std::string path) {
|
|
|
|
|
directory = path.substr(0, path.find_last_of('/')); |
|
|
|
|
|
|
|
|
|
process_node(scene->mRootNode, scene); |
|
|
|
|
glCheckError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Model::process_node(aiNode *node, const aiScene *scene)
|
|
|
|
@ -103,6 +111,7 @@ void Model::process_node(aiNode *node, const aiScene *scene)
@@ -103,6 +111,7 @@ void Model::process_node(aiNode *node, const aiScene *scene)
|
|
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; i++) { |
|
|
|
|
process_node(node->mChildren[i], scene); |
|
|
|
|
} |
|
|
|
|
glCheckError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
|
|
|
|
@ -151,6 +160,7 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
@@ -151,6 +160,7 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
return Mesh(vertices, indices, textures); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -191,6 +201,7 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory)
@@ -191,6 +201,7 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory)
|
|
|
|
|
stbi_image_free(data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
return textureID; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -220,6 +231,7 @@ std::vector<texture> Model::load_material_textures(aiMaterial *mat, aiTextureTyp
@@ -220,6 +231,7 @@ std::vector<texture> Model::load_material_textures(aiMaterial *mat, aiTextureTyp
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
return textures; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|