Browse Source

fix Mesh::draw bug

master
alistair 3 years ago
parent
commit
1a9fb193de
  1. 12
      src/glerror.h
  2. 6
      src/main.cpp
  3. 14
      src/mesh.cpp

12
src/glerror.h

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
#ifndef GL_ERRORH
#define GL_ERRORH
#define glCheckError() glCheckError_(__FILE__, __LINE__)
#include <GL/glew.h>
GLenum glCheckError_(const char *file, int line);
#endif

6
src/main.cpp

@ -20,9 +20,7 @@ @@ -20,9 +20,7 @@
#include "shaders.h"
#include "mesh.h"
#include "stb_image.h"
#define glCheckError() glCheckError_(__FILE__, __LINE__)
#include "glerror.h"
GLenum glCheckError_(const char *file, int line)
{
@ -40,7 +38,7 @@ GLenum glCheckError_(const char *file, int line) @@ -40,7 +38,7 @@ GLenum glCheckError_(const char *file, int line)
case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break;
}
std::cout << error << " | " << file << " (" << line << ")" << std::endl;
std::cerr << error << " | " << file << " (" << line << ")" << std::endl;
}
return errorCode;
}

14
src/mesh.cpp

@ -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;
}

Loading…
Cancel
Save