Actually added textures to loaded texture buffer so we can skip loading already loaded texetures.

This commit is contained in:
Warwick 2022-06-09 13:47:55 +01:00
parent 18a785613d
commit 3dd235397a
3 changed files with 22 additions and 9 deletions

View file

@ -55,8 +55,6 @@ void Mesh::draw(ShaderLoader &shader) {
shader.setFloat(("material." + name + number).c_str(), i); shader.setFloat(("material." + name + number).c_str(), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id); glBindTexture(GL_TEXTURE_2D, textures[i].id);
error.log(std::to_string(textures[i].id));
error.log(std::to_string(GL_TEXTURE0 + i));
} }
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);

View file

@ -34,10 +34,10 @@ void Model::loadModel(std::string path) {
return; return;
} }
directory = path.substr(0, path.find_last_of('/')) + '/'; directory = path.substr(0, path.find_last_of('/')) + '/';
error.log(directory);
processNode(scene->mRootNode, scene); processNode(scene->mRootNode, scene);
} }
void Model::processNode(aiNode *node, const aiScene *scene) { void Model::processNode(aiNode *node, const aiScene *scene) {
error.log("Processing Node");
// if the node has meshes process them // if the node has meshes process them
for (unsigned int i = 0; i < node->mNumMeshes; i++) { for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]]; aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
@ -53,7 +53,9 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
std::vector<unsigned int> indecies; std::vector<unsigned int> indecies;
std::vector<Texture> textures; std::vector<Texture> textures;
error.log("Processing Mesh");
error.log("Loading vertices");
for (unsigned int i = 0; i < mesh->mNumVertices; i++) { for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex; Vertex vertex;
// process vertex postions and add to our mesh // process vertex postions and add to our mesh
@ -72,6 +74,7 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
} }
// Handle indeces // Handle indeces
// Loop through the meshes faces to get the correct order // Loop through the meshes faces to get the correct order
error.log("Loading indecies");
for (unsigned int i = 0; i < mesh->mNumFaces; i++) { for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i]; aiFace face = mesh->mFaces[i];
// loop through and add each face's indecies // loop through and add each face's indecies
@ -81,13 +84,15 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
} }
} }
// Handle Assimps material format // Handle Assimps material format
error.log("loading textures");
if (mesh->mMaterialIndex >= 0) { if (mesh->mMaterialIndex >= 0) {
error.log("material index is greater than 0");
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex]; aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
std::vector<Texture> diffuseMaps = loadMaterialTextures( std::vector<Texture> diffuseMaps = loadMaterialTextures(
material, aiTextureType_DIFFUSE, "texture_diffuse"); material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
std::vector<Texture> specularMaps = loadMaterialTextures( std::vector<Texture> specularMaps = loadMaterialTextures(
material, aiTextureType_DIFFUSE, "texture_diffuse"); material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
} }
return Mesh(vertices, indecies, textures); return Mesh(vertices, indecies, textures);
@ -101,9 +106,17 @@ std::vector<Texture> Model::loadMaterialTextures(aiMaterial *material,
aiString str; aiString str;
material->GetTexture(type, i, &str); material->GetTexture(type, i, &str);
bool skip = false; bool skip = false;
// check we're not loading in a texture we already have // check we're not loading in a texture we already have
error.log(std::to_string(textures_loaded.size()));
for (unsigned int loadedtex = 0; loadedtex < textures_loaded.size(); for (unsigned int loadedtex = 0; loadedtex < textures_loaded.size();
loadedtex++) { loadedtex++) {
error.log(std::string("loaded texture: ") +
textures_loaded[loadedtex].path.data());
if (std::strcmp(textures_loaded[loadedtex].path.data(), str.C_Str()) == if (std::strcmp(textures_loaded[loadedtex].path.data(), str.C_Str()) ==
0) { 0) {
textures.push_back(textures_loaded[loadedtex]); textures.push_back(textures_loaded[loadedtex]);
@ -118,6 +131,9 @@ std::vector<Texture> Model::loadMaterialTextures(aiMaterial *material,
texture.type = typeName; texture.type = typeName;
texture.path = str.C_Str(); texture.path = str.C_Str();
textures.push_back(texture); textures.push_back(texture);
// Store the texture in the models loaded texture bank so we can check if
// we've already loaded it
textures_loaded.push_back(texture);
} }
} }
return textures; return textures;
@ -159,6 +175,5 @@ unsigned int Model::loadTextureFromFile(std::string file,
SDL_FreeSurface(image); SDL_FreeSurface(image);
image = nullptr; image = nullptr;
error.log("loaded texture: " + std::to_string(texture));
return texture; return texture;
} }

View file

@ -186,8 +186,8 @@ int main(int argc, char **argv) {
// model.translate(glm::vec3(1.0f, 0.0f, 0.0f)); // model.translate(glm::vec3(1.0f, 0.0f, 0.0f));
// Model backpack(ROOT_DIR "data/models/backpack/backpack.obj"); Model backpack(ROOT_DIR "data/models/backpack/backpack.obj");
Model cube(ROOT_DIR "data/models/cube/cube.obj"); // Model cube(ROOT_DIR "data/models/cube/cube.obj");
// Mess with perspective // Mess with perspective
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
@ -230,8 +230,8 @@ int main(int argc, char **argv) {
// Draw Meshes // Draw Meshes
// model2.draw(shader); // model2.draw(shader);
// model.draw(shader); // model.draw(shader);
cube.draw(shader); // cube.draw(shader);
// backpack.draw(shader); backpack.draw(shader);
// Finally render everything // Finally render everything
shader.use(); shader.use();