简体   繁体   中英

Alpha Blending problems in DirectX9

I am quite desperate to achieve desired alpha blending results in DirectX. I am try to draw texture, and on it a color triangle strip.

Despite all my attempts it seems that color of the strip is affected by the color of the texture drawn before. Even if I set D3DRS_ALPHABLENDENABLE to FALSE, I still see the color affected.

Here is how the image suppose to looks like (Rendered with openGL)

在此处输入图片说明

And here is what I get with directX rendering:

在此处输入图片说明

As you can see, the second image purple strip has yellow shade without any specific reason :( (The yellow rectangle is a texture, not a polygon).

Here is the setting I use in Engine initialization:

m_pDirect3D_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pDirect3D_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pDirect3D_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_BLENDDIFFUSEALPHA);
m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

Those settings are the only settings that gave me correct Alpha Blending of textures, but I can in no way achieve correct blending of polygons. Any Help?

I figured it out eventually. In addition to Alpha Blending there is a Color Blending. So DirectX merges color of the last texture with the last primitive. In order to solve it we need to set:

m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTSS_COLORARG1);
m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);

before drawing the primitives - meaning only take color from primitive (Diffuse), and:

m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTSS_COLORARG1);
m_pDirect3D_Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

before drawing the texture - meaning only use texture colors.

Old topic, but useful one!

I noticed a small detail, just to avoid some confusion: I think that the parameter to SetTextureStageState should be D3DTOP_SELECTARG1 instead of D3DTSS_COLORARG1 (same value anyway, so no big deal!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM