blend mode for black

Started by
7 comments, last by IStrikerI 20 years, 3 months ago
hi, i have a font texture with a white background and black letters. how do i have to set the blend mode to render only the black letters and not the white background? are there any explanations about the blending stuff with d3d9? because i dont understand the paramters of setrenderstate in the case of blending(D3DRS_SRCBLEND or D3DRS_DESTBLEND?). is that possible or do i have to do some masking shit?
Advertisement
- For text-output, consider using ID3DXFont if you're working with the DX9 summer update SDK. It's fast enough.

- I suggest you take a look at CD3DFont's implementation (DX8 SDK, I don't know if it comes with DX9)

- You naturally would do that using alpha testing. You set the alpha values of all the (white) background pixels to zero, and keep the black ones at 255. Then you'd set a "reference" alpha value (alphaRef) and enable alpha testing.

What alpha testing does is test each pixel's alpha against the reference alpha value using a defined condition. Say your condition is "greater than", then what'd happen is that all pixels with alpha less than or equal to alphaRef will fail the alpha test and won't get drawn.

Given that the background pixels have zero alpha, they'll fail the alpha test - with a "greater than" condition - with any alpha reference value.

You'll need the following renderstate:
D3DRS_ALPHATESTENABLE
D3DRS_ALPHAREF
D3DRS_ALPHAFUNC

For the exact details, consult the SDK documentation (There's an "alpha testing" section).

Muhammad Haggag
[edit]A correction, and clarifications[/edit]

[edited by - Coder on January 16, 2004 1:20:40 PM]

If your image is grayscale (anti-aliased text) then you can process the texture on load and set the alpha based on the pixel colour so it will still be anti-aliased against any background.

Also, it''s more flexible to do white text on a black background because then you can use that texture to modulate a colour for coloured text, or another texture for patterened text.

Here is a tutorial on manually editing textures to set an alpha value:
http://www.drunkenhyena.com/docs/d3d_tutorial.phtml#DHComputedAlpha

And here is a link to my font class (which uses a pre-made bitmap) which you can use for ideas (or just use entirely if you like, it''s free). There is also a link on that page to a utility that you can use to create font bitmaps.

http://www.drunkenhyena.com/docs/dhFastFont.phtml


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena
thanx guys,
white letters are a good idea. since i use alpha testing (and not alpha blending) i can also set the color to black.
just another question:
i have a 32bit TGA now and set (for testing) some alpha values to zero with a hex-editor. im loading the image with D3DFMT_A8R8G8B8, enable alpha-testing, setting the refernce value to 0x00000000 and the function to D3DCMP_GREATER. the alpha value must be greater than the reference value to be drawn right? but the pixels where i set alpha to zero are still drawn. do i have to set something with SetTextureStageState?
Make sure:
- You have alpha-testing enabled
- alphaFunc is supported by your device
- Your texture stages select the alpha from the texture (e.g. AlphaOp[0] = SelectArg1; AlphaArg1[0] = Texture; )
- The texture's loaded correctly with alpha.

Muhammad Haggag
[edit]A semicolon+bracket was converted to a smiley. Corrected that[/edit]

[edited by - Coder on January 16, 2004 12:25:20 AM]

To answer your original question, you can use D3DBLENDOP_MIN and get the effect you''re after.

SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_MIN)
SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)
SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE)
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE)

thanx, but do you mean that alpha-testing is not supported by all devices? if so i wont use it and i will go back to the blending stuff.
and thanx to don, yeah this works now!

[edited by - IStrikerI on January 17, 2004 7:15:52 AM]
quote:Original post by don
To answer your original question, you can use D3DBLENDOP_MIN and get the effect you''re after.

SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_MIN)
SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)
SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE)
SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE)

Smart!
Though it requires D3DBLENDOP support. Are there any half-decent cards out there that still don''t support D3DBLENDOP? (Only the Virge comes to mind, but that''s neither half-decent, nor out there )

quote:thanx, but do you mean that alpha-testing is not supported by all devices?

I think it''s safe to say it''s supported on all devices out there, simply because the S3 Virge had support for alpha-testing.

Muhammad Haggag

This topic is closed to new replies.

Advertisement