What I have is a set of 9 squares in a 3x3 grid being rendered for testing this blending function. Here's the idea.
On Pass 1:
- Bind texture1 (dirt)
- Render the squares using tex1 = Dirt
On Pass 2:
- Bind texture2 (letter)
- Render the same squares using tex2 = Letter W
This is for an in-game editor. We want to be able to change the color of letters from a text atlas as they are typed. The shader is supposed to check the alpha value of the fragment from the texture. Anything that is not equal to 0 (i.e. Visible) should be changed to the desired color that is passed into the shader as an attribute.
//Draw all Squares
glBindTexture(GL_TEXTURE_2D, textureID1); //Dirt Texture
for (int Index = 0; Index < 9; Index++)
{
//Pass in Vertex Positions...
//Pass in UV's...
glDrawArrays(GL_TRIANGLES, 0, 6); // From index 0 to 6 -> 2 triangle
}
//Redraw Squares with letter texture bound.
glBindTexture(GL_TEXTURE_2D, textureID2); //Letter Texture
glVertexAttrib3f(2,255,0,0); //Desired Color of letter, passed to shaders.
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.05f); //Anything with alpha < 0.05 is discarded.
//Draw all Squares
for (int Index = 0; Index < 9; Index++)
{
//Pass in Vertex Positions...
//Pass in UV's...
glDrawArrays(GL_TRIANGLES, 0, 6); // From index 0 to 6 -> 2 triangle
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisable(GL_ALPHA_TEST);
//Draw blue vertex dots...etc...
Here's a picture showing the source images, and the output of 2 slightly different Fragment shaders. Along with the desired result.
Image Sources, with Frag Shaders and Desired Result
The textures are loaded, both have alpha channels (GL_BGRA, Targas). I'm using GL_NEAREST as MIN/MAG FILTERS.
The odd thing is, this shader works for me if I use 99% white (i.e. 254) in the alpha channel of the LETTER TEXTURE and fails when I use full white (i.e. 255).
I've tried enabling GL_BLEND and using various BlendFunc's, but they don't seem to work.
I guess the question is. Without using a multitexture shader, and without rendering to an FBO, is this the right way to do this sort of blending?