diff --git a/data/shaders/texturedquad_custom_alpha.frag b/data/shaders/texturedquad_custom_alpha.frag new file mode 100644 index 000000000..5f6f748ea --- /dev/null +++ b/data/shaders/texturedquad_custom_alpha.frag @@ -0,0 +1,10 @@ +uniform sampler2D tex; +uniform float custom_alpha; + +in vec2 uv; +out vec4 FragColor; + +void main() +{ + FragColor = texture(tex, uv) * custom_alpha; +} diff --git a/src/graphics/2dutils.cpp b/src/graphics/2dutils.cpp index b46d48e42..293191773 100644 --- a/src/graphics/2dutils.cpp +++ b/src/graphics/2dutils.cpp @@ -58,7 +58,7 @@ public: }; // UniformColoredTextureRectShader // ============================================================================ -class TextureRectShader : public TextureShader @@ -74,6 +74,24 @@ public: } // TextureRectShader }; // TextureRectShader +// ============================================================================ +class TextureRectCustomAlphaShader : public TextureShader +{ +public: + TextureRectCustomAlphaShader() + { + loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert", + GL_FRAGMENT_SHADER, "texturedquad_custom_alpha.frag"); + assignUniforms("center", "size", "texcenter", "texsize", "rotation", + "custom_alpha"); + + assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED); + } // TextureRectCustomAlphaShader +}; // TextureRectCustomAlphaShader + // ============================================================================ class ColoredRectShader : public Shader @@ -521,6 +539,61 @@ void draw2DImage(const video::ITexture* texture, glGetError(); } // draw2DImage +// ---------------------------------------------------------------------------- +void draw2DImageCustomAlpha(const irr::video::ITexture* texture, + const irr::core::rect& destRect, + const irr::core::rect& sourceRect, + const irr::core::rect* clipRect, + float rotation, float custom_alpha) +{ + if (!CVS->isGLSL()) + return; + + float width, height, center_pos_x, center_pos_y, tex_width, tex_height; + float tex_center_pos_x, tex_center_pos_y; + + getSize(texture->getSize().Width, texture->getSize().Height, + texture->isRenderTarget(), destRect, sourceRect, width, height, + center_pos_x, center_pos_y, tex_width, tex_height, + tex_center_pos_x, tex_center_pos_y); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + if (clipRect) + { + if (!clipRect->isValid()) + return; + + glEnable(GL_SCISSOR_TEST); + const core::dimension2d& render_target_size = + irr_driver->getActualScreenSize(); + glScissor(clipRect->UpperLeftCorner.X, + (s32)render_target_size.Height - clipRect->LowerRightCorner.Y + + irr_driver->getDevice()->getMovedHeight(), + clipRect->getWidth(), clipRect->getHeight()); + } + + TextureRectCustomAlphaShader::getInstance()->use(); + glBindVertexArray(SharedGPUObjects::getUI_VAO()); + + TextureRectCustomAlphaShader::getInstance()->setTextureUnits(texture->getOpenGLTextureName()); + TextureRectCustomAlphaShader::getInstance()->setUniforms( + core::vector2df(center_pos_x, center_pos_y), + core::vector2df(width, height), + core::vector2df(tex_center_pos_x, tex_center_pos_y), + core::vector2df(tex_width, tex_height), rotation, custom_alpha); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + if (clipRect) + glDisable(GL_SCISSOR_TEST); + glUseProgram(0); + + glGetError(); +} // draw2DImage + // ---------------------------------------------------------------------------- void draw2DImage(const video::ITexture* texture, const core::rect& destRect, diff --git a/src/graphics/2dutils.hpp b/src/graphics/2dutils.hpp index e2fd31da0..f59b6fc57 100644 --- a/src/graphics/2dutils.hpp +++ b/src/graphics/2dutils.hpp @@ -59,6 +59,12 @@ void draw2DImage(const irr::video::ITexture* texture, bool useAlphaChannelOfTexture, bool draw_translucently = false, float rotation = 0.0f); +void draw2DImageCustomAlpha(const irr::video::ITexture* texture, + const irr::core::rect& destRect, + const irr::core::rect& sourceRect, + const irr::core::rect* clipRect, + float rotation, float custom_alpha); + void draw2DImage(const irr::video::ITexture* texture, const irr::core::rect& destRect, const irr::core::rect& sourceRect,