2013-07-30 142 views
3

我正在使用的DirectX 9遊戲這裏就是我想要做的事:的DirectX Alpha遮罩

場景渲染後,在它的上面我想渲染幾個精靈:一個黑色封面在整個場景和幾個精靈,這是掩模顯示蓋應該有洞。到目前爲止,我嘗試混合模式,但沒有運氣。我的代碼設置它看起來像這樣:

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

我想最好的辦法是乘每個精靈α,但根據http://msdn.microsoft.com/en-us/library/windows/desktop/bb172508%28v=vs.85%29.aspx不支持任何這樣的模式。是否有另一種方法來做到這一點?

edit 

繼尼科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(); 

可惜的是,恢復舊的渲染目標,當應用程序崩潰。有什麼建議?

+0

你會得到什麼錯誤?順便說一句,你當然應該使用帶有alpha通道的紋理格式。 –

+0

訪問衝突讀取位置0xcdcdcdcd。 pBackBuffer指針似乎是正確的,直到我渲染精靈的位置,然後在D3DD-> SetRenderTarget(0,pBackBuffer)中,它的值不同(始終爲0xcdcdcdcd)。 –

+0

另外我更改了紋理格式爲D3DFMT_A8R8G8B8 –

回答

1

首先,你應該首先在獨立的紋理中創建遮罩。然後您可以根據需要添加孔。最後,在屏幕上畫出面具:

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 

它應該總是黑色的。

和α通道由下式計算:

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

那麼,該孔精靈是不透明的,混合的α變爲0哪裏它是透明的,混合alpha是以前的值。兩者之間的值被混合。