#include "core/Texture.hpp"
std::map<std::string, std::pair<unsigned, GLuint>> Texture::textureCache = std::map<std::string, std::pair<unsigned, GLuint>>();
Texture::Texture(std::string path, int layer, std::string uniformName) {
this->layer = layer;
this->uniformName = uniformName;
this->path = path;
if(textureCache.find(path) == textureCache.end()) {
std::vector<unsigned char> image;
unsigned error = lodepng::decode(image, width, height, path.c_str() );
texture = 0;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/*
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,
GL_RGBA,
width, height,
GL_RGBA,
GL_UNSIGNED_BYTE,
&image[0]);
//glTexImage2D(GL_TEXTURE_2D, 0 , 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);
image.clear();
textureCache[path] = std::pair<unsigned, GLuint>(1, texture);
}
else {
textureCache[path].first++;
texture = textureCache[path].second;
}
}
Texture::~Texture() {
textureCache[path].first--;
if(textureCache[path].first==0 && texture) {
glDeleteTextures(1, &texture);
}
}
void Texture::unbindTexture(){
glActiveTexture( GL_TEXTURE0+layer );
glBindTexture( GL_TEXTURE_2D, 0 );
}
void Texture::bindTexture( ) {
glActiveTexture(GL_TEXTURE0+layer);
glBindTexture( GL_TEXTURE_2D, texture );
}
int Texture::getLayer() {
return this->layer;
}
void Texture::setLayer(int layer) {
this->layer = layer;
}
void Texture::setUniformName(std::string uniformName) {
this->uniformName = uniformName;
}
std::string Texture::getUniformName() {
return this->uniformName;
}