如何在LibGDX中进行混合

Eze*_*aga 15 java opengl libgdx

我基本上想在LibGDX中使用混合模式,但不知道该怎么做.我在互联网上找到了这个图像.我想在LibGDX上做同样的事情.有人可以教我如何.

在此输入图像描述

我一直在玩Scene2D.这是我的非工作片段.

private class MyGroup extends Group {

    Image red, blue;

    public MyGroup() {
        Texture texture = new Texture(Gdx.files.internal("images/red.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        red = new Image(texture);

        texture = new Texture(Gdx.files.internal("images/blue.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        blue = new Image(texture);

        red.setX(-25);
        blue.setX(25);
    }
    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.end();
        batch.begin();
        batch.enableBlending();

        red.draw(batch, parentAlpha);

        Gdx.gl.glEnable(Gdx.gl20.GL_BLEND);
        Gdx.gl.glBlendFuncSeparate(
                Gdx.gl20.GL_DST_COLOR, 
                Gdx.gl20.GL_SRC_COLOR,
                Gdx.gl20.GL_ONE,
                Gdx.gl20.GL_ONE);

        blue.draw(batch, parentAlpha);
    }
}
Run Code Online (Sandbox Code Playgroud)

Dou*_*ble 14

我意识到这不是一个新问题,但我想我会为其他任何人分享一些信息,而不知道在OpenGL中渲染这些问题/答案(了解这些术语有很多帮助,所以你不只是猜测混合- 并且匹配)请注意,这个网站是我自己学习大部分内容的方式,因此可以在那里找到更完整的信息.

目标颜色:缓冲区中的颜色,除非用新值修改或覆盖,否则将(最终)绘制颜色.

源颜色:来自其他渲染命令的颜色,可能与目标颜色交互,也可能不与目标颜色交互(取决于我们的设置)

默认的混合方程式: Final Color =(SourceColor*SourceBlendingFactor)+(DestinationColor*DestinationBlendingFactor)(此默认方程式可以更改,但我建议在顶部阅读我的源链接以获取更多信息)

两个BlendingFactors是我们可以搞砸的.我们可以将它们设置为:

GL_ZERO:RGB(0,0,0)A(0)
GL_ONE:RGB(1,1,1)A(1)
GL_SOURCE_COLOR:RGB(sourceR,sourceG,sourceB)A(sourceA)
GL_ONE_MINUS_SRC_COLOR:RGB(1-sourceR, 1-sourceG,1-sourceB)A(1-sourceA)
GL_DST_COLOR:RGB(destinationR,destinationG,destinationB)A(destinationA)
GL_ONE_MINUS_DST_COLOR:RGB(1-destinationR,1-destinationG,1-destinationB)A(1- destinationA)
GL_SRC_ALPHA:RGB(sourceA,sourceA,sourceA)A(sourceA)
GL_ONE_MINUS_SRC_ALPHA:RGB(1-sourceA,1-sourceA,1-sourceA)A(1-sourceA)
GL_DST_ALPHA:RGB(destinationA,destinationA,destinationA)A(destinationA)
GL_ONE_MINUS_DST_ALPHA:RGB(1-destinationA,1-destinationA,1-destinationA)A(1-destinationA)
GL_SRC_ALPHA_SATURATE:RGB(min(sourceA,1-destinationA),min(sourceA,1-destinationA),min(sourceA,1-) destinationA))A(1)

下面还使用了一些预定义的常量颜色,默认情况下它是黑色
GL_CONSTANT_COLOR:RGB(constantR,constantG,constantB)A(constantA)
GL_ONE_MINUS_CONSTANT_COLOR:RGB(1-constantR,1-constantG,1-cons)tantB)A(1-常数A)
GL_CONSTANT_ALPHA:RGB(constantA,constantA,constantA)A(constantA)
GL_ONE_MINUS_CONSTANT_ALPHA:RGB(1-常数A,1-常数A,1-常数A)A(1-常数A)

因此,所有这些都只是预定义的浮点值,它们与我们的源或目标相乘,然后添加到另一个.

从图像中最容易观察到的是GL_ZERO和GL_ONE.我们最终得到了ONE的任何一张图片.


使用GL_DST_COLOR了解GL_ZERO

当GL_ZERO在目的地时,我们忽略当前缓冲区中的任何颜色信息(因为将所有内容乘以零).但是,对于源图像上的GL_DST_COLOR,我们最终将r,g,b乘以源和目标的值.

由于示例图像的性质,这在图像中看起来很好.一个用作纯色图像,而另一个灰度图像看起来和行为几乎像一束光,从我们的GL_ZERO设置中"显示"颜色.

希望这有助于解释我们上面可以看到的图像,并帮助每个人了解这些图像实际上是如何混合在一起的.


Eze*_*aga 5

好的,部分回答我的问题,这是我使用的技巧。如果我做错了什么,请告诉我。注意:这对其他函数不起作用。就像当我结合 GL_DST_COLOR 和 GL_ZERO 时,它不会输出我想要的东西。但其他人会。所以就玩玩吧。我仍然在这里寻找其他答案。

这是代码:

private class MyGroup extends Group {

    Texture dst, src;

    public MyGroup() {
        dst = new Texture(Gdx.files.internal("images/dst.png"));
        dst.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        src = new Texture(Gdx.files.internal("images/src.png"));
        src.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        // We need to cast to use blending function
        SpriteBatch sb = (SpriteBatch)batch;

        // draw our destination image
        sb.draw(dst, 0, 0);
        sb.end();

        // remember SpriteBatch's current functions
        int srcFunc = sb.getBlendSrcFunc();
        int dstFunc = sb.getBlendDstFunc();

        // Let's enable blending
        sb.enableBlending();
        sb.begin();

        // blend them
        b.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_SRC_ALPHA);
        sb.draw(src, 0, 0);

        // Reset
        sb.end();
        sb.begin();
        sb.setBlendFunction(srcFunc, dstFunc);

    }
}
Run Code Online (Sandbox Code Playgroud)