7 #include <GLFW/glfw3.h> 8 #include <FreeImagePlus.h> 16 const std::string filePath;
19 Texture(
const std::string &filePath);
23 NEAREST_MIPMAP_OFF = 0,
24 NEAREST_MIPMAP_NEAREST = 1,
25 NEAREST_MIPMAP_LINEAR = 2,
26 LINEAR_MIPMAP_OFF = 3,
27 LINEAR_MIPMAP_NEAREST = 4,
28 LINEAR_MIPMAP_LINEAR = 5
50 inline Texture::Texture(
const std::string &filePath_)
53 glGenTextures(1, &handle);
54 glActiveTexture(GL_TEXTURE0);
55 glBindTexture(GL_TEXTURE_2D, handle);
59 if (!img.load(filePath.c_str(), 0)) {
60 std::cerr <<
"ERROR: FreeImage could not load image file '" << filePath <<
"'." << std::endl;
68 if (img.isTransparent()) {
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, img.getWidth(), img.getHeight(), 0,
70 GL_BGRA, GL_UNSIGNED_BYTE, img.accessPixels());
71 std::cout <<
"found texture with alpha channel: " << filePath << std::endl;
73 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, img.getWidth(), img.getHeight(), 0,
74 GL_BGR, GL_UNSIGNED_BYTE, img.accessPixels());
81 glGenerateMipmap(GL_TEXTURE_2D);
86 inline Texture::~Texture()
88 glDeleteTextures(1, &handle);
93 glActiveTexture(GL_TEXTURE0 + unit);
94 glBindTexture(GL_TEXTURE_2D, handle);
100 case NEAREST_MIPMAP_OFF:
102 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
104 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
106 case NEAREST_MIPMAP_NEAREST:
109 GL_TEXTURE_MIN_FILTER,
110 GL_NEAREST_MIPMAP_NEAREST);
112 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
114 case NEAREST_MIPMAP_LINEAR:
116 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
118 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120 case LINEAR_MIPMAP_OFF:
122 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126 case LINEAR_MIPMAP_NEAREST:
128 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
130 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
132 case LINEAR_MIPMAP_LINEAR:
134 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
136 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Texture class.
Definition: texture.hpp:13
void bind(int unit)
binds this texture to the given opengl texture unit
Definition: texture.hpp:91
void setFilterMode(FilterType filterType)
set texture minification and magnification filters minification: how to filter texture in case of und...
Definition: texture.hpp:97
std::string getFilePath() const
get the texture file path
Definition: texture.hpp:141