Assimp loading of huge meshes full of textures is working

This commit is contained in:
Hurlu 2019-03-27 15:43:55 +09:00
parent 34d4ba33e2
commit 1ea93fa5e2
2 changed files with 55 additions and 34 deletions

View File

@ -29,6 +29,8 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
vbo[TEXCOORD_BUFFER] = NULL; vbo[TEXCOORD_BUFFER] = NULL;
vbo[COLOR_BUFFER] = NULL; vbo[COLOR_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL; vbo[INDEX_BUFFER] = NULL;
vbo[TANGENTS_BUFFER] = NULL;
vbo[BITTANGENTS_BUFFER] = NULL;
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glBindVertexArray(vao); glBindVertexArray(vao);
@ -93,7 +95,7 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
} }
Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname) Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
{ {
renderType = OBJ; renderType = OBJ;
parent = m; parent = m;
@ -220,6 +222,15 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::st
material->Get(AI_MATKEY_COLOR_DIFFUSE, dcolor); material->Get(AI_MATKEY_COLOR_DIFFUSE, dcolor);
material->Get(AI_MATKEY_COLOR_AMBIENT, acolor); material->Get(AI_MATKEY_COLOR_AMBIENT, acolor);
material->Get(AI_MATKEY_COLOR_SPECULAR, scolor); material->Get(AI_MATKEY_COLOR_SPECULAR, scolor);
if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture
aiString Path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL,
NULL, NULL) == AI_SUCCESS) {
std::string FullPath = parent->directory + Path.data; //texture file
textures.push_back(Texture(FullPath));
}
}
} }
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -250,6 +261,14 @@ Mesh::MeshEntry::~MeshEntry() {
glDeleteBuffers(1, &vbo[COLOR_BUFFER]); glDeleteBuffers(1, &vbo[COLOR_BUFFER]);
} }
if (vbo[BITTANGENTS_BUFFER]) {
glDeleteBuffers(1, &vbo[BITTANGENTS_BUFFER]);
}
if (vbo[TANGENTS_BUFFER]) {
glDeleteBuffers(1, &vbo[TANGENTS_BUFFER]);
}
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
} }
@ -258,6 +277,7 @@ Mesh::MeshEntry::~MeshEntry() {
**/ **/
void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) { void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
textureBinding(shd);
glBindVertexArray(vao); glBindVertexArray(vao);
if (renderType == NO_INDEX) if (renderType == NO_INDEX)
{ {
@ -269,9 +289,31 @@ void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL); glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL);
} }
textureUnbinding();
glBindVertexArray(0); glBindVertexArray(0);
} }
void Mesh::MeshEntry::textureBinding(Shader * shd)
{
for (GLuint i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textures[i].tex_ref);
shd->addUniform("tex[" + std::to_string(i) + "]", (int)i);
}
if (textures.size() > 0)
shd->addUniform("TexCount", (int)textures.size());
}
void Mesh::MeshEntry::textureUnbinding()
{
for (GLuint i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
/** /**
* Mesh constructor, loads the specified filename if supported by Assimp * Mesh constructor, loads the specified filename if supported by Assimp
**/ **/
@ -281,8 +323,8 @@ Mesh::Mesh(const char *filename, Shader *sh)
std::string fullname; std::string fullname;
fullname = std::string("./Models/")+ std::string(filename); fullname = std::string("./Models/")+ std::string(filename);
std::string dirname = fullname; directory = fullname;
dirname.resize(fullname.find_last_of('/')); directory.resize(fullname.find_last_of('/') + 1);
Assimp::Importer importer; //aiProcessPreset_TargetRealtime_Fast Assimp::Importer importer; //aiProcessPreset_TargetRealtime_Fast
const aiScene* scene = importer.ReadFile(fullname.c_str(), const aiScene* scene = importer.ReadFile(fullname.c_str(),
@ -296,34 +338,8 @@ Mesh::Mesh(const char *filename, Shader *sh)
return; return;
} }
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
const aiMaterial* pMaterial = scene->mMaterials[i];
if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) { //we only care diffuse texture
aiString Path;
if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL,
NULL, NULL) == AI_SUCCESS) {
std::string FullPath = dirname + "/" + Path.data; //texture file
aiColor3D diffuse;
aiColor3D specular;
aiColor3D ambient;
pMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse);
pMaterial->Get(AI_MATKEY_COLOR_SPECULAR, specular);
pMaterial->Get(AI_MATKEY_COLOR_AMBIENT, ambient);
textures.push_back(Texture(FullPath));
textures.back().mat.enabled = true;
textures.back().mat.ka = glm::vec3(ambient.r, ambient.g, ambient.b);
textures.back().mat.kd = glm::vec3(diffuse.r, diffuse.g, diffuse.b);
textures.back().mat.ks = glm::vec3(specular.r, specular.g, specular.b);
}
}
}
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this, dirname)); meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this));
} }
} }

View File

@ -52,20 +52,25 @@ public:
glm::vec3 ssbo_data[3]; glm::vec3 ssbo_data[3];
aiColor3D dcolor; aiColor3D dcolor;
aiColor3D acolor; aiColor3D acolor;
aiColor3D scolor; aiColor3D scolor;
std::vector<Texture> textures;
float shininessStrength; float shininessStrength;
MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m, std::string dirname); MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m);
MeshEntry(Dataset &set, Mesh *m); MeshEntry(Dataset &set, Mesh *m);
~MeshEntry(); ~MeshEntry();
Mesh * parent; Mesh * parent;
void render(SceneContext &ctx, Shader *shd); void render(SceneContext &ctx, Shader *shd);
private:
void textureBinding(Shader *shd);
void textureUnbinding();
}; };
public: public:
Mesh(const char *filename, Shader *sh); Mesh(const char *filename, Shader *sh);
Mesh(Dataset &dataset, Shader *sh); Mesh(Dataset &dataset, Shader *sh);
~Mesh(void); ~Mesh(void);
std::string directory;
std::vector<MeshEntry*> meshEntries; std::vector<MeshEntry*> meshEntries;
std::vector<Texture> textures; std::vector<Texture> textures;
@ -74,8 +79,8 @@ private:
void enableCulling(); void enableCulling();
void textureBinding(Shader *shd); void textureBinding(Shader *shd);
void disableCulling();
void textureUnbinding(); void textureUnbinding();
void disableCulling();
public: public:
Model model; Model model;
CullFace cullMode = NONE; CullFace cullMode = NONE;