Crude texture caching

This commit is contained in:
2014-04-06 21:25:05 +02:00
parent 412d53a1f7
commit 0896f540fe
3 changed files with 16 additions and 8 deletions
+1 -1
View File
@@ -254,7 +254,7 @@ void Renderer::DrawScene()
glBindVertexArray(model->VAO); glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups) { for (auto texGroup : model->TextureGroups) {
glActiveTexture(GL_TEXTURE0); 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); glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
} }
} }
+8 -4
View File
@@ -6,21 +6,25 @@ Texture::Texture(std::string path)
Load(path); Load(path);
} }
void Texture::Load(std::string 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() void Texture::Bind()
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, m_Texture);
} }
Texture::~Texture() Texture::~Texture()
{ {
glDeleteTextures(1, &texture); glDeleteTextures(1, &m_Texture);
} }
+7 -3
View File
@@ -2,6 +2,7 @@
#define Texture_h__ #define Texture_h__
#include <string> #include <string>
#include <unordered_map>
#include <SOIL.h> #include <SOIL.h>
@@ -9,13 +10,16 @@ class Texture
{ {
public: public:
Texture(std::string path); Texture(std::string path);
~Texture(); ~Texture();
GLuint texture;
void Load(std::string path); void Load(std::string path);
void Bind(); void Bind();
operator GLuint() const { return m_Texture; }
private:
GLuint m_Texture;
std::unordered_map<std::string, GLuint> m_TextureCache;
}; };
#endif // Texture_h__ #endif // Texture_h__