diff options
Diffstat (limited to 'texture.cpp')
-rw-r--r-- | texture.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/texture.cpp b/texture.cpp index 4c9cb6d..ba7d9c7 100644 --- a/texture.cpp +++ b/texture.cpp @@ -1,7 +1,33 @@ #include <stdexcept> #include "texture.h" +#include "stb_image.h" #include "gl.h" +Texture::Texture() { + glGenTextures(1, &texture); +} + +Texture::Texture(const std::string& filename) { + glGenTextures(1, &texture); + load(filename); +} + +void Texture::load(const std::string& filename) { + int w, h, ch; + uint8_t* data = stbi_load(filename.c_str(), &w, &h, &ch, 4); + + unsigned int format; + if(ch == 4) { + format = GL_RGBA; + } else { + format = GL_RGB; + } + + build(data, format, w, h); + + stbi_image_free(data); +} + void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, texture); } @@ -26,7 +52,6 @@ void Texture::build(void* data, unsigned int format, unsigned int w, unsigned in width = w; height = h; - glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |