From 18a785613d68c399936480af7cff8862b0d66708 Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 31 May 2022 14:43:44 +0100 Subject: [PATCH] The engine can now import models with textures but bugs. More complex models have a bug where the same textures are potentially being loaded too many times. --- src/Mesh.cpp | 2 + src/Model.cpp | 12 +++++ src/main.cpp | 147 +++++++++++++++++++++++++------------------------- 3 files changed, 88 insertions(+), 73 deletions(-) diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 6b9b227..5e792f1 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -55,6 +55,8 @@ 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); diff --git a/src/Model.cpp b/src/Model.cpp index 5b09e4b..1f3c7ae 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -130,8 +130,19 @@ unsigned int Model::loadTextureFromFile(std::string file, if (image == nullptr) { error.crash("SDL2_image was unable to load a texture", IMG_GetError()); } + // Generate the texture and put its reference id in the texture variable glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + // set some textue defaults + float borderColor[] = {1.0f, 1.0f, 0.0f, 1.0f}; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR); // Handle different SDL Surface data types int mode = GL_RGB; @@ -148,5 +159,6 @@ unsigned int Model::loadTextureFromFile(std::string file, SDL_FreeSurface(image); image = nullptr; + error.log("loaded texture: " + std::to_string(texture)); return texture; } diff --git a/src/main.cpp b/src/main.cpp index 01c8857..51ee656 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,30 +77,30 @@ int main(int argc, char **argv) { ROOT_DIR "data/shaders/fragment.glsl"); // Load texture image - SDL_Surface *image = IMG_Load(ROOT_DIR "data/container.jpg"); - if (image == nullptr) { - error.crash("SDL2_image was unable to load a texture", IMG_GetError()); - } + // SDL_Surface *image = IMG_Load(ROOT_DIR "data/container.jpg"); + // if (image == nullptr) { + // error.crash("SDL2_image was unable to load a texture", IMG_GetError()); + //} // create and bind texture object - unsigned int texture; - glGenTextures(1, &texture); - // glBindTexture(GL_TEXTURE_2D, texture); + // unsigned int texture; + // glGenTextures(1, &texture); + //// glBindTexture(GL_TEXTURE_2D, texture); - // Handle different SDL Surface data types - int mode = GL_RGB; - if (image->format->BytesPerPixel == 4) { - mode = GL_RGBA; - } + //// Handle different SDL Surface data types + // int mode = GL_RGB; + // if (image->format->BytesPerPixel == 4) { + // mode = GL_RGBA; + //} - // Generate texture and mipmap from image - glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode, - GL_UNSIGNED_BYTE, image->pixels); - glGenerateMipmap(GL_TEXTURE_2D); + //// Generate texture and mipmap from image + // glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode, + // GL_UNSIGNED_BYTE, image->pixels); + // glGenerateMipmap(GL_TEXTURE_2D); // remove image surface now it's no longer needed to create texture - SDL_FreeSurface(image); - image = nullptr; + // SDL_FreeSurface(image); + // image = nullptr; // Generate Mesh // Define some useful structures to be used in the mesh object @@ -121,70 +121,70 @@ int main(int argc, char **argv) { -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left }; - unsigned int indices[] = { - 0, 1, 3, // First triangle - 1, 2, 3 // Second triangle - }; + // unsigned int indices[] = { + // 0, 1, 3, // First triangle + // 1, 2, 3 // Second triangle + //}; - std::vector mvertices; - Vertex vertex; - vertex.Position = glm::vec3(0.5f, 0.5f, 0.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(1.0f, 1.0f); - mvertices.push_back(vertex); - vertex.Position = glm::vec3(0.5f, -0.5f, 0.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(1.0f, 0.0f); - mvertices.push_back(vertex); - vertex.Position = glm::vec3(-0.5f, -0.5f, 0.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(0.0f, 0.0f); - mvertices.push_back(vertex); - vertex.Position = glm::vec3(-0.5f, 0.5f, 0.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(0.0f, 1.0f); - mvertices.push_back(vertex); + // std::vector mvertices; + // Vertex vertex; + // vertex.Position = glm::vec3(0.5f, 0.5f, 0.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(1.0f, 1.0f); + // mvertices.push_back(vertex); + // vertex.Position = glm::vec3(0.5f, -0.5f, 0.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(1.0f, 0.0f); + // mvertices.push_back(vertex); + // vertex.Position = glm::vec3(-0.5f, -0.5f, 0.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(0.0f, 0.0f); + // mvertices.push_back(vertex); + // vertex.Position = glm::vec3(-0.5f, 0.5f, 0.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(0.0f, 1.0f); + // mvertices.push_back(vertex); - std::vector mindices{ - 0, 3, 1, // First triangle - 1, 3, 2 // Second triangle - }; + // std::vector mindices{ + // 0, 3, 1, // First triangle + // 1, 3, 2 // Second triangle + //}; - std::vector mtextures; - Texture mtexture; - mtexture.id = texture; - mtexture.type = "texture_diffuse"; + // std::vector mtextures; + // Texture mtexture; + // mtexture.id = texture; + // mtexture.type = "texture_diffuse"; - Mesh mesh(mvertices, mindices, mtextures); + // Mesh mesh(mvertices, mindices, mtextures); - std::vector mvertices2; - vertex.Position = glm::vec3(0.5f, 0.5f, 1.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(1.0f, 1.0f); - mvertices2.push_back(vertex); - vertex.Position = glm::vec3(0.5f, -0.5f, 1.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(1.0f, 0.0f); - mvertices2.push_back(vertex); - vertex.Position = glm::vec3(-0.5f, -0.5f, 1.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(0.0f, 0.0f); - mvertices2.push_back(vertex); - vertex.Position = glm::vec3(-0.5f, 0.5f, 1.0f); - vertex.Normal = glm::vec3(); - vertex.TexCoords = glm::vec2(0.0f, 1.0f); - mvertices2.push_back(vertex); + // std::vector mvertices2; + // vertex.Position = glm::vec3(0.5f, 0.5f, 1.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(1.0f, 1.0f); + // mvertices2.push_back(vertex); + // vertex.Position = glm::vec3(0.5f, -0.5f, 1.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(1.0f, 0.0f); + // mvertices2.push_back(vertex); + // vertex.Position = glm::vec3(-0.5f, -0.5f, 1.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(0.0f, 0.0f); + // mvertices2.push_back(vertex); + // vertex.Position = glm::vec3(-0.5f, 0.5f, 1.0f); + // vertex.Normal = glm::vec3(); + // vertex.TexCoords = glm::vec2(0.0f, 1.0f); + // mvertices2.push_back(vertex); - Mesh mesh2(mvertices2, mindices, mtextures); + // Mesh mesh2(mvertices2, mindices, mtextures); - std::vector modelMeshes; - modelMeshes.push_back(mesh); - modelMeshes.push_back(mesh2); - Model model(modelMeshes); + // std::vector modelMeshes; + // modelMeshes.push_back(mesh); + // modelMeshes.push_back(mesh2); + // Model model(modelMeshes); - Model model2(mesh); + // Model model2(mesh); - 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 cube(ROOT_DIR "data/models/cube/cube.obj"); @@ -231,6 +231,7 @@ int main(int argc, char **argv) { // model2.draw(shader); // model.draw(shader); cube.draw(shader); + // backpack.draw(shader); // Finally render everything shader.use();