diff --git a/source/Graphics/Texture.cpp b/source/Graphics/Texture.cpp index 3922781..086a4cd 100644 --- a/source/Graphics/Texture.cpp +++ b/source/Graphics/Texture.cpp @@ -39,7 +39,9 @@ Texture::~Texture() void Texture::create(unsigned width, unsigned heigth, PixelFormat format) { - std::vector empty_pixels(width * heigth); + // TODO: Should have getBpp() in PixelFormat instead. + // Always allocate 32 bit (4 byte) pixel buffer for now. + std::vector empty_pixels(width * heigth * 4); if (!m_id) { glGenTextures(1, &m_id); @@ -50,9 +52,15 @@ void Texture::create(unsigned width, unsigned heigth, PixelFormat format) m_size = vec2u(width, heigth); m_format = format; + if (format == PixelFormat::PF_RGBA) { + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); + } else { + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + } + glTexImage2D(GL_TEXTURE_2D, 0, pixelFormatToInternal(format), width, heigth, 0, - GL_RED, GL_UNSIGNED_BYTE, &empty_pixels[0]); + pixelFormatToGL(format), GL_UNSIGNED_BYTE, &empty_pixels[0]); setSmooth(true); setRepeat(false); @@ -79,16 +87,15 @@ void Texture::create(const Image& image) enable(); - if (image.getFormat() == PixelFormat::PF_Alpha) { - glFormat = GL_R8; - srcFormat = GL_RED; - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - } else { - glFormat = GL_RGBA; - srcFormat = GL_RGBA; + if (image.getBpp() == 32) { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); + } else { + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); } + srcFormat = pixelFormatToGL(image.getFormat()); + glFormat = pixelFormatToInternal(image.getFormat()); + glTexImage2D(GL_TEXTURE_2D, 0, glFormat, m_size.x, m_size.y, 0, srcFormat, GL_UNSIGNED_BYTE, image.getPixels()); @@ -140,14 +147,14 @@ void Texture::update(vec2u pos, const Image& image) enable(); - if (image.getFormat() == PixelFormat::PF_Alpha) { - format = GL_RED; - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - } else { - format = GL_RGBA; + if (image.getFormat() == PixelFormat::PF_RGBA) { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); + } else { + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); } + format = pixelFormatToGL(image.getFormat()); + glTexSubImage2D(GL_TEXTURE_2D, 0, pos.x, pos.y, image.getWidth(), image.getHeight(), format, GL_UNSIGNED_BYTE, image.getPixels()); @@ -169,10 +176,7 @@ Image Texture::copyToImage() const enable(); - //glPixelStorei(GL_PACK_ALIGNMENT, 1); - - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + //glPixelStorei(GL_PACK_ALIGNMENT, 4); glGetTexImage(GL_TEXTURE_2D, 0, pixelFormatToGL(m_format),