如何在LibGDX中进行混合

如何在LibGDX中进行混合

本文介绍了如何在LibGDX中进行混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I basically want to play around with blending modes in LibGDX but don't know how to do it. I found this image on internet. I want to do the same thing on LibGDX. Can someone teach me how.

我一直在使用Scene2D。这是我的非工作代码段。

I've been playing around using Scene2D. Here's my non-working snippet.

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);
    }
}


推荐答案

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

I realize this is not a new question, but I thought I would share some information for anyone else who makes it to this question/answer without knowing much about rendering in OpenGL (Knowing these terms helps a lot so you aren't just guessing mix-and-matching) Note that this site is how I learned most of this myself, so more complete information can be found there.

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

Destination Color: the color in the buffer, which will (eventually) be drawn unless it is modified or overwritten with new values.

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

Source Color: the color coming in from additional rendering commands, which may or may not interact with the destination color (depending on our settings)

默认混合等式:最终颜色=(SourceColor * SourceBlendingFactor)+(DestinationColor * DestinationBlendingFactor)

The default blending equation: Final Color = (SourceColor*SourceBlendingFactor)+(DestinationColor*DestinationBlendingFactor)

两个BlendingFactors就是我们可以搞得的。我们可以将它们设置为:

The two BlendingFactors are what we can mess with. We can set them to:

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(so) urceA,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-constantB)A(1-constantA)

GL_CONSTANT_ALPHA :RGB(constantA,constantA,constantA)A( constantA)

GL_ONE_MINUS_CONSTANT_ALPHA :RGB(1-constantA,1-constantA,1-constantA)A(1-constantA)

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)

The following also uses some predefined constant color, by default it is black
GL_CONSTANT_COLOR: RGB(constantR, constantG, constantB) A(constantA)
GL_ONE_MINUS_CONSTANT_COLOR: RGB(1-constantR, 1-constantG, 1-constantB) A(1-constantA)
GL_CONSTANT_ALPHA: RGB(constantA, constantA, constantA) A(constantA)
GL_ONE_MINUS_CONSTANT_ALPHA: RGB(1-constantA, 1-constantA, 1-constantA) A(1-constantA)

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

So all of these are just predefined float values that are being multiplied with either our source or destination, and then added to the other.

最容易观察到的图像是GL_ZERO和GL_ONE。我们最终会得到ONE的任何一张图片。

The easiest to observe from the image is GL_ZERO and GL_ONE. We end up with whichever image has the ONE.

了解GL_ZERO GL_DST_COLOR

Understanding GL_ZERO with GL_DST_COLOR

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

When GL_ZERO is on the destination, we are ignoring any color information currently in the buffer (because multiplying everything by zero). However, With GL_DST_COLOR also on the source image, we end up multiplying the r, g, b, a values of the source and destination.

这在图像中看起来不错因为示例图像的性质。一个用作纯色图像,而另一个灰度图像看起来和行为几乎像一束光从我们的GL_ZERO设置中显示颜色。

This looks good in the image because of the nature of the example images. One acts as a solid color image, while the other grayscale image looks and acts almost like a beam of light to "reveal" the color out from our GL_ZERO setting.

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

Hopefully, this helps explain the images we can see above and helps everyone understand how those images are actually being blended together.

这篇关于如何在LibGDX中进行混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:30