gpt4 book ai didi

c++ - DirectX alpha 掩蔽

转载 作者:太空狗 更新时间:2023-10-29 23:05:47 25 4
gpt4 key购买 nike

我正在使用 DirectX 9 开发游戏。这是我正在尝试做的事情:

渲染场景后,我想在它上面渲染一些 Sprite :整个场景上的黑色覆盖物和一些 Sprite ,它们是显示覆盖物应该有孔的蒙版。到目前为止,我尝试搞乱混合模式但没有运气。我的设置代码如下所示:

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

我想最好的方法是将每个 Sprite 的 alpha 相乘,但根据 http://msdn.microsoft.com/en-us/library/windows/desktop/bb172508%28v=vs.85%29.aspx不支持这种模式。还有其他方法吗?

edit

根据 Nico Schertler 的回答,这是我想出的代码:

LPDIRECT3DTEXTURE9      pRenderTexture;
LPDIRECT3DSURFACE9 pRenderSurface,
pBackBuffer;

// create texture
D3DD->CreateTexture(1024,
1024,
1,
D3DUSAGE_RENDERTARGET,
D3DFMT_R5G6B5,
D3DPOOL_DEFAULT,
&pRenderTexture,
NULL);

pRenderTexture->GetSurfaceLevel(0,&pRenderSurface);

// store old render target - back buffer
D3DD->GetRenderTarget(0,&pBackBuffer);

// set new render target - texture
D3DD->SetRenderTarget(0,pRenderSurface);

//clear texture to opaque black
D3DD->Clear(0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0),
32.0f,
0);

// set blending
D3DD->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
D3DD->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
D3DD->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ZERO);
D3DD->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_SRCALPHA);
D3DD->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);

//// now I render hole sprites the usual way

// restore back buffe as render target
D3DD->SetRenderTarget(0,pBackBuffer);

// restore blending
D3DD->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
D3DD->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
D3DD->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_SRCALPHA);
D3DD->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA);
D3DD->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);

ulong color = ulong(-1);
Vertex2D v[4];

v[0] = Vertex2D(0, 0, 0);
v[1] = Vertex2D(1023, 0, 0);
v[3] = Vertex2D(1023, 1023, 0);
v[2] = Vertex2D(0, 1023, 0);

D3DD->SetTexture(0, pRenderTexture);
D3DD->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
D3DD->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, sizeof(Vertex2D));

D3DD->SetTexture(0, NULL);

// release used resources
pRenderTexture->Release();
pRenderSurface->Release();
pBackBuffer->Release();

不幸的是,应用程序在恢复旧的渲染目标时崩溃了。有什么建议吗?

最佳答案

首先,您应该先在单独的纹理中创建 mask 。然后您可以根据需要添加孔。最后,在屏幕上绘制 mask :

Initialize the texture
Clear it to opaque black
Using the following blend states:
D3DRS_SRCBLEND -> D3DBLEND_ZERO (hole's color does not matter)
D3DRS_DESTBLEND -> D3DBLEND_ONE (preserve the black color)
D3DRS_SRCBLENDALPHA -> D3DBLEND_ZERO
D3DRS_DESTBLENDALPHA -> D3DBLEND_SRCALPHA
D3DRS_SEPARATEALPHABLENDENABLE -> TRUE
Draw each hole sprite
Restore default blending (src_alpha / inv_src_alpha)
Render the texture as a sprite to the back buffer

上面的混合状态假定孔洞在应该有孔洞的地方是不透明的。然后,颜色计算如下:

blended color = 0 * hole sprite color + 1 * background color

它应该总是黑色的。

Alpha channel 的计算方式是:

blended alpha = 0 * hole sprite alpha + (1 - hole sprite alpha) * background alpha

因此,在孔 Sprite 不透明的地方,混合 alpha 变为 0。在透明的地方,混合 alpha 为之前的值。介于两者之间的值是混合的。

关于c++ - DirectX alpha 掩蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17950565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com