2
\$\begingroup\$

I have a Direct3D9 application that is rendering ID3DXSprites. The problem I am experiencing is best described in this situation:

I have a texture that is totally opaque. On top of it I draw a rectangle filled with solid color and alpha of 128. On top of the rectangle I have a text that is totally opaque. I draw all of this and get the resulting image through GetRenderTarget call. The problem is that on the resulting image, on the area where the transparent rectangle is, I have semi transparent pixels. It is not a problem that the rectangle is transparent, the problem is that the resulting image is.

The question is how to setup the blending so in this situation I don't get the transparent pixels in the resulting image?

I use the sprite with D3DXSPRITE_ALPHABLEND which sets the device state to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA. I tried couple of combinations of SetRenderState, like D3DBLEND_SRCALPHA, D3DBLEND_DESTALPHA etc., but couldn't make it work.

Thanks.

\$\endgroup\$
4
  • \$\begingroup\$ Why do you paint a transparent rectangle when you want no transparent pixels? \$\endgroup\$
    – vines
    Jun 24, 2011 at 12:59
  • \$\begingroup\$ @vines I want a transparent rectangle over the first texture. But the resultant image shouldn't contain transparent pixels, since the first texture is totally opaque. \$\endgroup\$
    – ambrozija
    Jun 24, 2011 at 13:15
  • \$\begingroup\$ Then it would be nice of you to post the code related to alpha-blending, so that we could check :) \$\endgroup\$
    – vines
    Jun 24, 2011 at 13:58
  • \$\begingroup\$ There's really nothing to it: m_pSprite->Begin(D3DXSPRITE_ALPHABLEND); m_pSprite->Draw(m_vecTextures[0].pTexture, NULL, NULL, NULL, 0xffffffff); m_pSprite->End(); \$\endgroup\$
    – ambrozija
    Jun 24, 2011 at 14:08

2 Answers 2

2
\$\begingroup\$

You can use D3DRS_SEPARATEALPHABLENDENABLE render state to control the blend outcome in the alpha channel of the destination.

SetRenderState (D3DRS_SEPARATEALPHABLENDENABLE, TRUE); 
SetRenderState (D3DRS_SRCBLENDALPHA, D3DBLEND_ZERO);
SetRenderState (D3DRS_DESTBLENDALPHA, D3DBLEND_ONE);

Provided that your texture was filled with a fully opaque color to begin with; this should leave the alpha channel alone.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @Aaldert thanks, but that didn't quite work for me. What did work was: SetRenderState (D3DRS_SEPARATEALPHABLENDENABLE, TRUE); SetRenderState (D3DRS_SRCBLENDALPHA, D3DBLEND_ONE); SetRenderState (D3DRS_DESTBLENDALPHA, D3DBLEND_ONE); \$\endgroup\$
    – ambrozija
    Jun 27, 2011 at 8:57
1
\$\begingroup\$

for future reference, you should check out Pix in the directx SDK, it's a great tool for seeing exactly what is being rendered at each point, and working out how the final color fo a specific pixel was arrived at.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .