include core Texture hpp std map std string std pair unsigned GLuint T

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#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;
}