Alpha Blending problems in DirectX9

爷,独闯天下 提交于 2021-02-07 10:51:41

问题


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)

enter image description here

And here is what I get with directX rendering:

enter image description here

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?


回答1:


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.




回答2:


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!).



来源:https://stackoverflow.com/questions/15107121/alpha-blending-problems-in-directx9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!