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);
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);

View file

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