简体   繁体   English

OpenglES 2.0 PNG alpha纹理重叠

[英]OpenglES 2.0 PNG alpha texture overlap

I'm trying to draw multiple hexagons on the screen that have an alpha channel. 我正在尝试在屏幕上绘制具有Alpha通道的多个六边形。 the image is this: 图像是这样的:

http://img834.imageshack.us/img834/571/hexagon.png

So, I load the texture into the program and that's ok. 所以,我将纹理加载到程序中,这没关系。 When it runs, the alpha channel is blended with the background color and that's ok but, when two hexagons overlap themselves, the overlapped part becomes the color of the background! 当它运行时,alpha通道与背景颜色混合即可,但是,当两个六边形重叠时,重叠的部分将成为背景的颜色! Below the picture: 图片下方:

http://img259.imageshack.us/img259/213/20120731163847.png

Of course, this is not the effect that I expected.. I want them to overlap without this background being drawn over the other texture. 当然,这不是我预期的效果。我希望它们重叠而不将此背景绘制在其他纹理上。 Here is my code for drawing: 这是我的绘图代码:

    GLES20.glUseProgram(Program);

    hVertex  = GLES20.glGetAttribLocation(Program,"vPosition");
    hColor   = GLES20.glGetUniformLocation(Program, "vColor");
    uTexture = GLES20.glGetUniformLocation(Program, "u_Texture");
    hTexture = GLES20.glGetAttribLocation(Program, "a_TexCoordinate");
    hMatrix  = GLES20.glGetUniformLocation(Program, "uMVPMatrix");

    GLES20.glVertexAttribPointer(hVertex, 3, GLES20.GL_FLOAT, false, 0, bVertex);
    GLES20.glEnableVertexAttribArray(hVertex);
    GLES20.glUniform4fv(hColor, 1, Color, 0);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, hTexture);
    GLES20.glUniform1i(uTexture, 0);
    GLES20.glVertexAttribPointer(hTexture, 2, GLES20.GL_FLOAT, false, 0, bTexture);
    GLES20.glEnableVertexAttribArray(hTexture);

    GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    GLES20.glEnable(GLES20.GL_BLEND);

    x=-1;y=0;z=0;
    for (int i=0;i<10;i++) {
        Matrix.setIdentityM(ModelMatrix, 0);
        Matrix.translateM(ModelMatrix, 0, x, y, z);
        x+=0.6f;
        Matrix.multiplyMM(ModelMatrix, 0, ModelMatrix, 0, ProjectionMatrix, 0);
        GLES20.glUniformMatrix4fv(hMatrix, 1, false, ModelMatrix, 0);
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, DrawOrder.length, GLES20.GL_UNSIGNED_SHORT, bDrawOrder);
    }

    GLES20.glDisable(GLES20.GL_BLEND);
    GLES20.glDisableVertexAttribArray(hVertex);
}

And My fragment shader: 我的片段着色器:

public final String fragmentShaderCode =
        "precision mediump float;" +
        "uniform vec4 vColor;" +
        "uniform sampler2D u_Texture;" +
        "varying vec2 v_TexCoordinate;" +
        "void main() {" +
        "  gl_FragColor = vColor * texture2D(u_Texture, v_TexCoordinate);" +
        "}";

and my renderer code: 和我的渲染器代码:

    super(context);
    setEGLContextClientVersion(2);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);
    setEGLConfigChooser(8, 8, 8, 8, 8, 8);
    renderer = new GLRenderer(context);
    setRenderer(renderer);

I already tried to use diferent functions on glBlendFunc but nothing seems to work.. Does Anyone knows what the problem is? 我已经尝试在glBlendFunc上使用不同的函数,但似乎没有任何工作..有谁知道问题是什么? I'm really lost.. If needs anymore code just ask! 我真的迷路..如果需要更多的代码只是问!

Thank you! 谢谢!

My guess is that you need to disable the depth test when drawing these. 我的猜测是你需要在绘制这些时禁用深度测试。 Since they all appear at the same depth, when you draw your leftmost ring, it writes into the depth buffer for every pixel in the quad, even the transparent ones. 由于它们都出现在相同的深度,当你绘制最左边的环时,它会为四边形中的每个像素写入深度缓冲区,甚至是透明的。

Then when you draw the next quad to the right, the pixels which overlap don't get drawn because they fail the depth test, so you just get a blank area where it intersects with the first quad. 然后,当您向右绘制下一个四边形时,重叠的像素不会被绘制,因为它们未通过深度测试,因此您只需获得一个与第一个四边形相交的空白区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM