From f88411abc7150ed209efcb6180503f0c6f7c8ef6 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 5 Oct 2010 07:00:04 +0200 Subject: Moved texture loading into base Texture class. --- texture.cpp | 27 ++++++++++++++++++++++++++- texture.h | 6 ++++++ texturestbi.cpp | 20 -------------------- texturestbi.h | 8 -------- 4 files changed, 32 insertions(+), 29 deletions(-) delete mode 100644 texturestbi.cpp delete mode 100644 texturestbi.h diff --git a/texture.cpp b/texture.cpp index 4c9cb6d..ba7d9c7 100644 --- a/texture.cpp +++ b/texture.cpp @@ -1,7 +1,33 @@ #include #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); diff --git a/texture.h b/texture.h index ca7fef1..9fea06f 100644 --- a/texture.h +++ b/texture.h @@ -2,9 +2,15 @@ #define _TEXTURE_H_ #include +#include class Texture { public: + Texture(); + Texture(const std::string& filename); + + void load(const std::string& filename); + void bind() const; unsigned int tex() const; unsigned int w() const; diff --git a/texturestbi.cpp b/texturestbi.cpp deleted file mode 100644 index abd802e..0000000 --- a/texturestbi.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "texturestbi.h" -#include "stb_image.h" - -TextureSTBI::TextureSTBI(const char* filename) { - int w, h, ch; - uint8_t* data = stbi_load(filename, &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); -} diff --git a/texturestbi.h b/texturestbi.h deleted file mode 100644 index ebb6d9d..0000000 --- a/texturestbi.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _TEXTURESOIL_H_ -#define _TEXTURESOIL_H_ -#include "texture.h" -class TextureSTBI : public Texture { - public: - TextureSTBI(const char* filename); -}; -#endif // _TEXTURESOIL_H_ -- cgit v1.2.3