Alireza Peer
Alireza Peer

Reputation: 908

Opengl-Using a shader, is it necessary?

in my Android game im drawing a rectangle and binding a texture to it using this code:

        gl.glVertexPointer(3, GLES10.GL_FLOAT, 0, mBufVertices);
        // Enable color array.
        gl.glEnableClientState(GLES10.GL_COLOR_ARRAY);
        gl.glColorPointer(4, GLES10.GL_FLOAT, 0, mBufColors);
        gl.glEnable(GL10.GL_BLEND);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[1]);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
        gl.glDisable(GL10.GL_BLEND);
        gl.glDisable(GL10.GL_TEXTURE_2D);

every thing is fine, But the problem is that i want the rectangle to be completely transparent and just draw the texture visible on it, meaning i want the yellow part to be transparent:

enter image description here

i tried to change the rectangle vertex color and make them transparent, but this made the Whole page transparent even the texture.

does i have to use a shader to do what i want?

Upvotes: 2

Views: 111

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

This is a very common mistake with using blend function. What happens is you overwrite the alpha channel with .0 at the same time even though the color components persist from the background.

What you need to do is either use glBlendFuncSeparate where you can use to preserve the destination alpha value using (..., ..., GL_ZERO, GL_ONE). Or you can simply disable the drawing to alpha channel using the color mask glColorMask where the last parameter is false keeping the rest to true (by default all are true).

In any case do not forget to reset the values once done using it.

Upvotes: 1

Related Questions