Fix alpha masking when using shader.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 5m11s

This commit is contained in:
2025-04-18 11:59:17 -04:00
parent 02370c6bfa
commit f7eff123be
3 changed files with 17 additions and 14 deletions

View File

@@ -6,9 +6,6 @@ varying vec4 v_color;
// The number of texture units that have been set.
uniform int TEXTURE_UNIT_SET_COUNT;
// True if we should perform the alpha mask operation.
uniform bool ALPHA_MASK;
// True if we're rendering text.
uniform bool TEXT;
@@ -36,7 +33,6 @@ void DrawColorOnly() {
gl_FragColor = v_color;
}
// TODO fix positive alpha mask.
void SampleTextureUnits() {
if (TEXT)
gl_FragColor = vec4(v_color.rgb, v_color.a * texture2D(GL_TEXTURE0, GL_TEXTURE0_COORD).a);
@@ -44,6 +40,14 @@ void SampleTextureUnits() {
// Draw sprite, partial sprite, mirror sprite, render target, partial render target.
else if (TEXTURE_UNIT_SET_COUNT == 1)
gl_FragColor = v_color * texture2D(GL_TEXTURE0, GL_TEXTURE0_COORD);
// Draw alpha masked sprite.
else if (TEXTURE_UNIT_SET_COUNT == 2) {
vec4 color_texture = texture2D(GL_TEXTURE0, GL_TEXTURE0_COORD);
float alpha_mask = texture2D(GL_TEXTURE1, GL_TEXTURE1_COORD).a;
gl_FragColor = vec4(v_color.rgb * color_texture.rgb, v_color.a * alpha_mask);
}
}
void main() {

View File

@@ -112,7 +112,7 @@ JGL::Font FreeSans;
Texture* image;
Texture* image_mask;
RenderTarget* j2d_render_target;
Shader shader;
Shader* shader;
class JGLDemoWindow : public ReWindow::OpenGLWindow
{
@@ -137,7 +137,7 @@ public:
//Texture::MultiplyByAlphaMask(*image, *image_mask);
shader = Shader(std::filesystem::path("assets/shader_programs/test_vertex.glsl"), std::filesystem::path("assets/shader_programs/test_fragment.glsl"));
shader = new Shader(std::filesystem::path("assets/shader_programs/test_vertex.glsl"), std::filesystem::path("assets/shader_programs/test_fragment.glsl"));
}
EulerAngleXYZ textAngle = {0,0,0};
@@ -190,7 +190,7 @@ public:
J3D::WireframeAABB(Colors::Yellow, {0.5, 0, 0.5}, {0.125, 0.125, 0.125}, 1);
J3D::End();
J2D::Begin(j2d_render_target, &shader, true);
J2D::Begin(j2d_render_target, shader, true);
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::DrawSprite(image, {300, 400}, sprite_radians * 0.10f, {0.5,0.5}, {1, 1}, Colors::White);
J2D::DrawMirrorSprite(image, {400, 300}, Direction::Horizontal | Direction::Vertical, sprite_radians, {0.5,0.5}, {1, 1}, Colors::White);
@@ -231,15 +231,11 @@ public:
J2D::End();
J2D::Begin(nullptr, &shader, true);
J2D::Begin(nullptr, shader, true);
J2D::DrawRenderTarget(j2d_render_target, {0, 0});
J2D::DrawSprite(image, image_mask, {0, 0}, 0.25, {0.5, 0.5}, {1,1});
J2D::End();
}
void OnRefresh(float elapsed) override {

View File

@@ -428,12 +428,15 @@ void J2D::DrawSprite(const Texture& texture, const Texture& alpha_mask, const Ve
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
if (current_state.current_shader)
current_state.current_shader->SetInt("TEXTURE_UNIT_SET_COUNT", 2);
current_state.current_shader->SetInt("TEXTURE_UNIT_SET_COUNT", 2),
current_state.current_shader->SetInt("GL_TEXTURE1", 1);
// Draw.
glDrawArrays(GL_QUADS, 0, 4);
if (current_state.current_shader)
current_state.current_shader->SetInt("TEXTURE_UNIT_SET_COUNT", 0);
current_state.current_shader->SetInt("TEXTURE_UNIT_SET_COUNT", 0),
current_state.current_shader->SetInt("GL_TEXTURE1", 0);
// Reset Texture 1.
glBindTexture(GL_TEXTURE_2D, 0);