Main Questions:
1) How can I get the fragment shader to not update the current pixels to a transparent pixel ONLY if destination color is not transparent?
2) Does gl_LastFragColor work for android 2.3 OpenGL ES 2.0?
Background:
I am trying to write text with OpenGL ES 2.0 using batch texturing. The challenge in the fragment shader is to only update the pixel colors for the text if the destination pixel color has alpha of 1, which I know it means that the pixel doesn't belong to the text.
The texture of the text atlas is stored in a bitmap: Bitmap.Config.ALPHA_8
After several hours I noticed three problems:
First problem: Somehow alpha of 1 is being used as opaque and 0 as transparent.
Second problem: Suppose I display "Hi". Although my draworder is supposed to draw "Hi" by texturing H, then i, it is doing it as: "i", then "H".
I know this because if I manually reverse the order of the vertices for "Hi" to start from "i", then I get my text. This is a problem because of problem 3.
Third problem: I believe this is the main problem. Say the letter H has texture box of exactly the width of 0.5, but I use texture of width 1, where the first 0.25 is white with alpha 1, the next 0.5 is white with alpha 0, last 0.25 is white with alpha 1. Same for "i".
When "i" is textured first, then "H" is textured second, the widths will intersect (on purpose, so "DD" and "dd" have different combined widths for example). But after "H" is rendered after "i", it is updating all the pixels across its width to be transparent, and most of the letter o disappears. This is correct behavior if "i" was rendered first then "H" and blending is disabled.
So I tried to use gl_LastFragColor to check the previous pixel before updating it but I got: Undeclared Identifier. So I am stuck with this fragment code:
Fragment code:
vec4 myTexture;
myTexture = texture2D(u_Texture, v_TexCoordinate);
if( myTexture[3] < 0.01){ // The texture is Bitmap.Config.ALPHA_8. If alpha is 0 . . .
gl_FragColor = vec4(0.0,0.0,0.0,0.0); // 0.0 is somehow transparent.
}
else{
gl_FragColor = vec4(1.0,1.0,1.0,1.0); // display text in white. Somehow 1.0 is opaque.
gl_FragColor = myTexture;
}
I am calling draw as:
GLES20.glEnable (GLES20.GL_BLEND);
GLES20.glBlendFunc (GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
// GLES20.glBlendFunc (GLES20.GL_ONE_MINUS_SRC_ALPHA, GLES20.GL_SRC_ALPHA); This makes alpha of 1 transparent!!
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrderLength, GLES20.GL_UNSIGNED_SHORT, 0);
GLES20.glDisable (GLES20.GL_BLEND);
P.s. If I switch alphas from 1 to 0, or equivalent trick, I get the same problem, with transparency inverted.