From 0896f540fe8d66bbf96a12b3f1c1477fdc206737 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sun, 6 Apr 2014 21:25:05 +0200 Subject: [PATCH] Crude texture caching --- src/Renderer.cpp | 2 +- src/Texture.cpp | 12 ++++++++---- src/Texture.h | 10 +++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 146ab7a..0b8ed59 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -254,7 +254,7 @@ void Renderer::DrawScene() glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture); + glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } diff --git a/src/Texture.cpp b/src/Texture.cpp index 05a3904..dad8409 100755 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -6,21 +6,25 @@ Texture::Texture(std::string path) Load(path); } - void Texture::Load(std::string path) { - texture = SOIL_load_OGL_texture(path.c_str(), 0, 0, SOIL_FLAG_INVERT_Y); + auto cachedTexture = m_TextureCache.find(path); + if (cachedTexture == m_TextureCache.end()) { + m_TextureCache[path] = SOIL_load_OGL_texture(path.c_str(), 0, 0, SOIL_FLAG_INVERT_Y); + } + + m_Texture = m_TextureCache[path]; } void Texture::Bind() { glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture); + glBindTexture(GL_TEXTURE_2D, m_Texture); } Texture::~Texture() { - glDeleteTextures(1, &texture); + glDeleteTextures(1, &m_Texture); } diff --git a/src/Texture.h b/src/Texture.h index 9d66f51..0f00b58 100755 --- a/src/Texture.h +++ b/src/Texture.h @@ -2,6 +2,7 @@ #define Texture_h__ #include +#include #include @@ -9,13 +10,16 @@ class Texture { public: Texture(std::string path); - ~Texture(); - GLuint texture; - void Load(std::string path); void Bind(); + + operator GLuint() const { return m_Texture; } + +private: + GLuint m_Texture; + std::unordered_map m_TextureCache; }; #endif // Texture_h__