#define STB_IMAGE_IMPLEMENTATION #include #include "Texture.hpp" #include namespace renderer { Texture::Texture () : mID( 0 ) {} Texture::~Texture () { if( mID ) glDeleteTextures( 1, &mID ); } bool Texture::loadFromFile ( const std::string& path, bool flip ) { stbi_set_flip_vertically_on_load( flip ); int w, h, channels; unsigned char* data = stbi_load( path.c_str(), &w, &h, &channels, 0 ); if( !data ) { std::cerr << "Failed to load texture: " << path << std::endl; return false; } GLenum format = GL_RGB; if( channels == 1 ) format = GL_RED; else if( channels == 3 ) format = GL_RGB; else if( channels == 4 ) format = GL_RGBA; glGenTextures( 1, &mID ); glBindTexture( GL_TEXTURE_2D, mID ); glTexImage2D( GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, data ); glGenerateMipmap( GL_TEXTURE_2D ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); stbi_image_free( data ); glBindTexture( GL_TEXTURE_2D, 0 ); return true; } void Texture::bind ( unsigned unit ) const { glActiveTexture( GL_TEXTURE0 + unit ); glBindTexture( GL_TEXTURE_2D, mID ); } } // namespace renderer