Skip to content

Commit f787832

Browse files
committed
Add 2d drawing shader with custom alpha
1 parent ce1c188 commit f787832

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
uniform sampler2D tex;
2+
uniform float custom_alpha;
3+
4+
in vec2 uv;
5+
out vec4 FragColor;
6+
7+
void main()
8+
{
9+
FragColor = texture(tex, uv) * custom_alpha;
10+
}

src/graphics/2dutils.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class UniformColoredTextureRectShader : public TextureShader<UniformColoredTextu
5858
}; // UniformColoredTextureRectShader
5959

6060
// ============================================================================
61-
class TextureRectShader : public TextureShader<TextureRectShader, 1,
61+
class TextureRectShader : public TextureShader<TextureRectShader, 1,
6262
core::vector2df, core::vector2df,
6363
core::vector2df, core::vector2df,
6464
float>
@@ -74,6 +74,24 @@ class TextureRectShader : public TextureShader<TextureRectShader, 1,
7474
} // TextureRectShader
7575
}; // TextureRectShader
7676

77+
// ============================================================================
78+
class TextureRectCustomAlphaShader : public TextureShader<TextureRectCustomAlphaShader, 1,
79+
core::vector2df, core::vector2df,
80+
core::vector2df, core::vector2df,
81+
float, float>
82+
{
83+
public:
84+
TextureRectCustomAlphaShader()
85+
{
86+
loadProgram(OBJECT, GL_VERTEX_SHADER, "texturedquad.vert",
87+
GL_FRAGMENT_SHADER, "texturedquad_custom_alpha.frag");
88+
assignUniforms("center", "size", "texcenter", "texsize", "rotation",
89+
"custom_alpha");
90+
91+
assignSamplerNames(0, "tex", ST_BILINEAR_CLAMPED_FILTERED);
92+
} // TextureRectCustomAlphaShader
93+
}; // TextureRectCustomAlphaShader
94+
7795
// ============================================================================
7896
class ColoredRectShader : public Shader<ColoredRectShader, core::vector2df,
7997
core::vector2df, video::SColor>
@@ -521,6 +539,61 @@ void draw2DImage(const video::ITexture* texture,
521539
glGetError();
522540
} // draw2DImage
523541

542+
// ----------------------------------------------------------------------------
543+
void draw2DImageCustomAlpha(const irr::video::ITexture* texture,
544+
const irr::core::rect<irr::s32>& destRect,
545+
const irr::core::rect<irr::s32>& sourceRect,
546+
const irr::core::rect<irr::s32>* clipRect,
547+
float rotation, float custom_alpha)
548+
{
549+
if (!CVS->isGLSL())
550+
return;
551+
552+
float width, height, center_pos_x, center_pos_y, tex_width, tex_height;
553+
float tex_center_pos_x, tex_center_pos_y;
554+
555+
getSize(texture->getSize().Width, texture->getSize().Height,
556+
texture->isRenderTarget(), destRect, sourceRect, width, height,
557+
center_pos_x, center_pos_y, tex_width, tex_height,
558+
tex_center_pos_x, tex_center_pos_y);
559+
560+
glEnable(GL_BLEND);
561+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
562+
if (clipRect)
563+
{
564+
if (!clipRect->isValid())
565+
return;
566+
567+
glEnable(GL_SCISSOR_TEST);
568+
const core::dimension2d<u32>& render_target_size =
569+
irr_driver->getActualScreenSize();
570+
glScissor(clipRect->UpperLeftCorner.X,
571+
(s32)render_target_size.Height - clipRect->LowerRightCorner.Y +
572+
irr_driver->getDevice()->getMovedHeight(),
573+
clipRect->getWidth(), clipRect->getHeight());
574+
}
575+
576+
TextureRectCustomAlphaShader::getInstance()->use();
577+
glBindVertexArray(SharedGPUObjects::getUI_VAO());
578+
579+
TextureRectCustomAlphaShader::getInstance()->setTextureUnits(texture->getOpenGLTextureName());
580+
TextureRectCustomAlphaShader::getInstance()->setUniforms(
581+
core::vector2df(center_pos_x, center_pos_y),
582+
core::vector2df(width, height),
583+
core::vector2df(tex_center_pos_x, tex_center_pos_y),
584+
core::vector2df(tex_width, tex_height), rotation, custom_alpha);
585+
586+
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
587+
glBindVertexArray(0);
588+
glBindBuffer(GL_ARRAY_BUFFER, 0);
589+
590+
if (clipRect)
591+
glDisable(GL_SCISSOR_TEST);
592+
glUseProgram(0);
593+
594+
glGetError();
595+
} // draw2DImage
596+
524597
// ----------------------------------------------------------------------------
525598
void draw2DImage(const video::ITexture* texture,
526599
const core::rect<float>& destRect,

src/graphics/2dutils.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ void draw2DImage(const irr::video::ITexture* texture,
5959
bool useAlphaChannelOfTexture, bool draw_translucently = false,
6060
float rotation = 0.0f);
6161

62+
void draw2DImageCustomAlpha(const irr::video::ITexture* texture,
63+
const irr::core::rect<irr::s32>& destRect,
64+
const irr::core::rect<irr::s32>& sourceRect,
65+
const irr::core::rect<irr::s32>* clipRect,
66+
float rotation, float custom_alpha);
67+
6268
void draw2DImage(const irr::video::ITexture* texture,
6369
const irr::core::rect<float>& destRect,
6470
const irr::core::rect<irr::s32>& sourceRect,

0 commit comments

Comments
 (0)