2016-06-20 13 views
0

透明な背景を持つpngファイルがあります。DirectX 11透明度の問題

私が画像を描くとき、​​透明度が働きます。

enter image description here

限り以下見られるように、それの前に他の透明画像が存在しないように。この問題を解決するために

enter image description here

どのような変更私は私の透明コードに作成する必要がありますか?

深度ステンシルコード:

// Initialize the description of the depth buffer. 
ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc)); 

// Set up the description of the depth buffer. 
depthBufferDesc.Width = screenWidth; 
depthBufferDesc.Height = screenHeight; 
depthBufferDesc.MipLevels = 1; 
depthBufferDesc.ArraySize = 1; 
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; 
depthBufferDesc.SampleDesc.Count = 1; 
depthBufferDesc.SampleDesc.Quality = 0; 
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT; 
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; 
depthBufferDesc.CPUAccessFlags = 0; 
depthBufferDesc.MiscFlags = 0; 

// Create the texture for the depth buffer using the filled out description. 
result = m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); 
Error::ErrorCheck(result, TEXT("m_device->CreateTexture2D()")); 

// Initialize the description of the stencil state. 
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc)); 

// Set up the description of the stencil state. 
depthStencilDesc.DepthEnable = true; 
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; 
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; 

depthStencilDesc.StencilEnable = true; 
depthStencilDesc.StencilReadMask = 0xFF; 
depthStencilDesc.StencilWriteMask = 0xFF; 

// Stencil operations if pixel is front-facing. 
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; 
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 

// Stencil operations if pixel is back-facing. 
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; 
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 

// Create the depth stencil state. 
result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState); 
Error::ErrorCheck(result, TEXT("m_device->CreateDepthStencilState()")); 

// Set the depth stencil state. 
m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 0); 

// Initailze the depth stencil view. 
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); 

// Set up the depth stencil view description. 
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; 
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; 
depthStencilViewDesc.Texture2D.MipSlice = 0; 

// Create the depth stencil view. 
result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView); 
Error::ErrorCheck(result, TEXT("m_device->CreateDepthStencilView()")); 

// Bind the render target view and depth stencil buffer to the output render pipeline. 
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); 

透明コード:

//Setup Blend State for transperency 
D3D11_BLEND_DESC BlendStateDescription; 
ZeroMemory(&BlendStateDescription, sizeof(D3D11_BLEND_DESC)); 

ID3D11BlendState* blend; 

BlendStateDescription.RenderTarget[0].BlendEnable = TRUE; 
BlendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; 
BlendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; 

BlendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; 
BlendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA; 
BlendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; 
BlendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; 
BlendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; 

m_device->CreateBlendState(&BlendStateDescription, &blend); 
float blendFactor[] = { 0, 0, 0, 0 }; 
UINT sampleMask = 0xffffffff; 

m_deviceContext->OMSetBlendState(blend, blendFactor, sampleMask); 
+0

あなたは透明度が最初のケースで動作しますか? PNGの透明部分があなたの澄んだ色と一致する場合があります(両方とも白です)。 – Columbo

+0

私は、背景を変更すると画像はまだ透明です:) – Oblivion

+0

場合によっては深度バッファがありますか?それに書き込み、深度ステンシルの状態をテストしますか? – galop1n

答えて

0

は、問題を発見しました。深度バッファーに入っていた。 depthStencilDesc.DepthFuncをD3D11_COMPARISON_LESSからD3D11_COMPARISON_LESS_EQUALに変更するだけです。ここで答えを見つけ

、より詳細な説明については、リンクをたどる: Detailed answer

+2

問題は深度バッファの状態ではありません。問題は、透明度をレンダリングするときに深度バッファを変更していることです。 'LESS_EQUAL'に切り替えるとすぐに問題は解決するかもしれませんが、帯域幅を無駄にしてしまい、同じ面でフラットな画像をレンダリングするよりも興味深いことをしようとすると、さらなる問題に遭遇するでしょう。あなたが推薦するようにリンクした答えとして、おそらく深度を完全にオフにし、透明なオブジェクトを前面にレンダリングする必要があります。 – MooseBoys

+0

ご清聴ありがとうございます。なぜ深さバッファを有効にしてはいけないのかが分かります。しかし、100%は、あなたが "透明なオブジェクトを前面にレンダリングする"と言ったときに何を意味するのかを確かめないでください。私は理解できるようにこれをさらに詳しく説明できますか? – Oblivion

+0

透明オブジェクトを単一のレンダリングターゲットに正しくレンダリングする唯一の方法は、カメラに対して最も遠い位置から最も近い位置から(ビューポートに対して前後に)それらを描画することです。順序に依存しない透明性などの高度な手法がありますが、通常は追加の中間バッファが必要です。 – MooseBoys

関連する問題