Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL blending mode and depth buffer

The read circle is nearer to the viewer and the texture has a transparent background.

(Both objects are squares with the same size, just different texture and x, z coords).

I want:

enter image description here

But I have:

enter image description here

I know I have to do something with blending modes and maybe the depth buffer, but I don't know exactly what. Can someone help me?

The current code to load the texture:

public void initTexture(GL10 gl, Bitmap bitmap) {
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    int[] texture = new int[1];
    gl.glGenTextures(1, texture, 0);

    textureId = texture[0];
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();
}

The relevant part of drawing, for each of these objects:

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glFrontFace(GL10.GL_CW);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    gl.glDrawElements(GLES20.GL_TRIANGLES, indices.length,  GLES10.GL_UNSIGNED_SHORT, indicesBuffer);

Thanks!

Note: I'm using OpenGL ES 1.

like image 622
User Avatar asked Feb 01 '26 12:02

User


1 Answers

You don't want blending, because that would mix the colors. What you want is alphatesting. Use glEnable(GL_ALPHA_TEST) and glAlphaFunc to set it appropriately.

like image 162
Bartek Banachewicz Avatar answered Feb 04 '26 01:02

Bartek Banachewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!