For a pet project of mine I'm trying to replicate the behavior of the ID3DXFont interface from d3dx9, that is: render arbitrary 2D text with arbitrary color and arbitrary (but uniform) transparency. If you remember what ID3DXFont does, you know exactly what I'm trying to do. No more and no less.
I'm using Direct3D9 because it's the API I'm more familiar with. Direct3D9 can use shaders, but I want to use only the FFP, because if successful I'll port this to older API versions D3D8 and D3D7.
The project is nearly complete but I miss the last piece of the puzzle.
The theory is simple. Prepare a texture. Draw text on the texture. Slap the texture on a 2D quad in transformed space. Render the quad on the backbuffer. The framework is in place and works as intended already.
The help I need concerns the correct setup of RenderStates and maybe the TextureStageStates. Look, I don't know. I never could grasp how blending worked. And as much as I try now I can't seem to overcome this beast by myself.
This is what I do:
1) Create a Texture to work with. This has the same color format of the backbuffer. No Alpha channel allowed because GDI has no well-defined behavior in such case (Microsoft's words, more or less). Let's assume my 'work' Texture is of the X8R8G8B8 format.
2) Use the Windows API DrawText() from GDI to print formatted text on the work Texture. At the moment the text is printed in White color over a Black background. Just like in this sample image:
I repeat, no Alpha information is present. The idea was to use the very texture's colors as the Alpha (255 -opaque- where the White is, and 0 -transparent- where the Black is).
3) Prepare a quad in transformed space, set it to use the work texture. And render it on the backbuffer with a call to DrawPrimitive().
The quad's vertices have an ARGB Diffuse color. This color is meant to provide the final Alpha and RGB color to the text printed on the work texture, such that if I set a Diffuse of ARGB(127, 255, 0, 0) the final text color on the backbuffer should appear 50% translucent Red.
The only thing of the work texture that must NOT show up ever is the Black background. How do I make it disappear?
Please help me.