From 6936f30b36e6f000aac685c658ecae541e2587c5 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Sat, 10 May 2014 20:49:30 +0200 Subject: [PATCH] Premultiply alpha for billboards. --- data/shaders/billboard.frag | 2 +- src/graphics/glwrap.cpp | 15 +++++++++++++-- src/graphics/glwrap.hpp | 2 +- src/graphics/stkbillboard.cpp | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/data/shaders/billboard.frag b/data/shaders/billboard.frag index 6df5ad558..0eb0af806 100644 --- a/data/shaders/billboard.frag +++ b/data/shaders/billboard.frag @@ -12,5 +12,5 @@ varying vec2 uv; void main(void) { vec4 color = texture(tex, uv); - FragColor = vec4(color.rgb * color.a, color.a); + FragColor = vec4(color.rgb, color.a); } diff --git a/src/graphics/glwrap.cpp b/src/graphics/glwrap.cpp index 996760e02..aacb64ba2 100644 --- a/src/graphics/glwrap.cpp +++ b/src/graphics/glwrap.cpp @@ -315,7 +315,7 @@ void resetTextureTable() AlreadyTransformedTexture.clear(); } -void compressTexture(irr::video::ITexture *tex, bool srgb) +void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha) { if (AlreadyTransformedTexture.find(tex) != AlreadyTransformedTexture.end()) return; @@ -338,7 +338,7 @@ void compressTexture(irr::video::ITexture *tex, bool srgb) } size_t w = tex->getSize().Width, h = tex->getSize().Height; - char *data = new char[w * h * 4]; + unsigned char *data = new unsigned char[w * h * 4]; memcpy(data, tex->lock(), w * h * 4); tex->unlock(); unsigned internalFormat, Format; @@ -347,6 +347,17 @@ void compressTexture(irr::video::ITexture *tex, bool srgb) else Format = GL_BGR; + if (premul_alpha) + { + for (unsigned i = 0; i < w * h; i++) + { + float alpha = pow(data[4 * i + 3] / 255., 1. / 2.2); + data[4 * i] *= alpha; + data[4 * i + 1] *= alpha; + data[4 * i + 2] *= alpha; + } + } + if (!UserConfigParams::m_texture_compression) { if (srgb) diff --git a/src/graphics/glwrap.hpp b/src/graphics/glwrap.hpp index 5cec12f90..bd64792d7 100644 --- a/src/graphics/glwrap.hpp +++ b/src/graphics/glwrap.hpp @@ -181,7 +181,7 @@ public: GLuint getTextureGLuint(irr::video::ITexture *tex); GLuint getDepthTexture(irr::video::ITexture *tex); void resetTextureTable(); -void compressTexture(irr::video::ITexture *tex, bool srgb); +void compressTexture(irr::video::ITexture *tex, bool srgb, bool premul_alpha = false); bool loadCompressedTexture(const std::string& compressed_tex); void saveCompressedTexture(const std::string& compressed_tex); void blitFBO(GLuint Src, GLuint Dst, size_t width, size_t height); diff --git a/src/graphics/stkbillboard.cpp b/src/graphics/stkbillboard.cpp index 7b71977b5..4ce29f479 100644 --- a/src/graphics/stkbillboard.cpp +++ b/src/graphics/stkbillboard.cpp @@ -35,7 +35,7 @@ void STKBillboard::render() core::vector3df pos = getAbsolutePosition(); glBindVertexArray(billboardvao); video::ITexture *tex = Material.getTexture(0); - compressTexture(tex, true); + compressTexture(tex, true, true); GLuint texid = getTextureGLuint(tex); setTexture(0, texid, GL_LINEAR, GL_LINEAR); glUseProgram(MeshShader::BillboardShader::Program);