21 #include "hx3d/graphics/texture.hpp" 23 #include "hx3d/utils/file.hpp" 24 #include "hx3d/utils/log.hpp" 26 #include "hx3d/graphics/font.hpp" 27 #include "hx3d/graphics/error.hpp" 29 #define STB_IMAGE_IMPLEMENTATION 30 #include <stb_image.h> 42 _width = image._width;
43 _height = image._height;
45 glGenTextures(1, &_id);
46 glBindTexture(GL_TEXTURE_2D, _id);
48 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image._buffer);
56 glBindTexture(GL_TEXTURE_2D, 0);
67 if (glIsTexture(_id)) {
68 glDeleteTextures(1, &_id);
76 unsigned char* file_content =
reinterpret_cast<unsigned char*
>(file->getData());
77 unsigned char* content = stbi_load_from_memory(file_content, file->getSize(), (
int*)&_width, (
int*)&_height, &bpp, 0);
79 glGenTextures(1, &_id);
80 glBindTexture(GL_TEXTURE_2D, _id);
82 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, content);
90 glBindTexture(GL_TEXTURE_2D, 0);
92 Log.
Info(
"Texture %s loaded: %d x %d", pathToImage.c_str(), _width, _height);
94 stbi_image_free(content);
122 glBindTexture(GL_TEXTURE_2D, _id);
123 glTexParameteri(GL_TEXTURE_2D, filterType, filterValue);
124 glBindTexture(GL_TEXTURE_2D, 0);
128 glBindTexture(GL_TEXTURE_2D, texture->_id);
132 Font::Data& data = font->getFontData(characterSize);
133 glBindTexture(GL_TEXTURE_2D, data.
atlas->id);
137 glBindTexture(GL_TEXTURE_2D, 0);
143 texture->_width = width;
144 texture->_height = height;
146 glGenTextures(1, &texture->_id);
147 glBindTexture(GL_TEXTURE_2D, texture->_id);
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->_width, texture->_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
157 glBindTexture(GL_TEXTURE_2D, 0);
174 glBindTexture(GL_TEXTURE_2D, _id);
175 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
176 glBindTexture(GL_TEXTURE_2D, 0);
183 texture->_height = 1;
185 glGenTextures(1, &texture->_id);
186 glBindTexture(GL_TEXTURE_2D, texture->_id);
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->_width, texture->_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
200 glBindTexture(GL_TEXTURE_2D, 0);
Texture()
Construct an empty texture. See load to build the texture.
unsigned int getWidth()
Get the texture width.
FilterValue
Filtering value.
void setFilter(FilterType type, FilterValue value)
Set the texture filters.
GLuint getID()
Get the texture ID.
void updateZone(unsigned int x, unsigned int y, unsigned int w, unsigned int h, Uint8 *data)
Update a zone in the texture.
bool load(std::string pathToImage)
Build the texture from an image.
texture_atlas_t * atlas
Internal atlas.
unsigned int getHeight()
Get the texture height.
static hx3d::LogImpl Log
Current log implementation.
static Ptr< Texture > createColorBuffer(unsigned int width, unsigned int height)
Create a color buffer (used in framebuffers).
Real-time editable texture.
static Ptr< Texture > Blank
Default blank texture.
FilterType
Filtering type.
2D/3D texture management.
static void disable()
Clear the current texture for drawing.
static void use(Ptr< Texture > texture)
Use the current texture for drawing.
static Ptr< File > loadBinaryFile(std::string path)
Load a binary file from a path.
static void generateBlankTexture()
Used to create the Texture::Blank texture.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
void Info(const std::string fmt,...)
Write an info message.