2012-12-24 76 views
1

我正在使用DirectX 11 SDK并将某些内容呈现给纹理(它的工作原理)。比使用D3DX11SaveTextureToFile将输出保存到PNG纹理。一切正常,但我不透明。渲染为纹理,但如何使用DirectX以透明度保存它?

我想要背景(没有任何元素已渲染的空间)透明。取而代之,我得到一些背景。

我试图改变float ClearColor[4]ClearRenderTargetView功能的{ 0.0f, 0.0f, 0.0f, 1.0f },因为我觉得最后一个是α和1.0F用给我的透明度,但并没有为我工作(同样为0.0 f)。

这里是我的代码,可能是有用的部分:

混合状态(我相信是好的,它们适用于在屏幕上呈现透明元素 - 监视器的进程画面总是有一些“背景”, PNG文件没有):

D3D11_BLEND_DESC blendDesc; 
ZeroMemory(&blendDesc, sizeof(D3D11_BLEND_DESC)); 
blendDesc.AlphaToCoverageEnable = false; 
blendDesc.IndependentBlendEnable = false;   
blendDesc.RenderTarget[0].BlendEnable = true; 
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; 
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; 
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; 

blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; 
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; 
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; 
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ; 

ID3D11BlendState * blendState; 

if(FAILED(device->CreateBlendState(&blendDesc, &blendState))){ 
     //... 
} 

g_pImmediateContext->OMSetBlendState(blendState,NULL,0xffffffff); 

和:

// Setup the render target texture description. 
textureDesc.Width = textureWidth; 
textureDesc.Height = textureHeight; 
textureDesc.MipLevels = 1; 
textureDesc.ArraySize = 1; 
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 
textureDesc.SampleDesc.Count = 1; 
textureDesc.Usage = D3D11_USAGE_DEFAULT; 
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; 
textureDesc.CPUAccessFlags = 0; 
textureDesc.MiscFlags = 0; 
textureDesc.SampleDesc.Quality = 0; 
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;  
... 
//Create the render target texture. 
result = device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture); 
... 
result = device->CreateShaderResourceView(renderTargetTexture, 
    &shaderResourceViewDesc, &shaderResourceView); 
... 
g_pImmediateContext->OMSetRenderTargets(1, &renderTargetView, 
    g_pDepthStencilView); 
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; //red, green, blue, alpha 
g_pImmediateContext->ClearRenderTargetView(renderTargetView, ClearColor); 

//clear the depth buffer to 1.0 (max depth) 
g_pImmediateContext->ClearDepthStencilView(g_pDepthStencilView, 
    D3D11_CLEAR_DEPTH, 1.0f, 0); 
... 
//render here 
... 
D3DX11SaveTextureToFile(g_pImmediateContext, renderTargetTexture, 
    D3DX11_IFF_BMP, CommonFunctions::s2ws(newTextureName).c_str()); 
+2

该alpha的作品相反。 0.0f表示完全透明,1.0f完全可见。 (alpha-value = opacity) – Gnietschow

回答

2

如果你想保存PNG文件,使用D3DX11_IFF_PNG代替D3DX11_IFF_BMP


由于PolGraphic在他的评论中提到的,可能有必要从D3D11_BLEND_SRC_ALPHA和D3D11_BLEND_INV_SRC_ALPHA混合状态更改为D3D11_BLEND_ONE该协会致力于PNG。但是,在大多数情况下,默认设置SRC_ALPHA/INV_SRC_ALPHA应该工作。

+0

我已经尝试过了,但是根本没有得到我的图像。只有一半的图像和透明背景上的黑色矩形。更奇怪的是,矩形出现在应该有透明背景的地方(没有元素),透明的地方有对象。 – PolGraphic

+1

你确定你有正确的混合状态吗?并确保清除(0,0,0,0)。 –

+0

感谢关于混合状态。从D3D11_BLEND_SRC_ALPHA和D3D11_BLEND_INV_SRC_ALPHA更改为D3D11_BLEND_ONE适用于PNG!但是在场景中,我获得了半透明的所有对象。不知道为什么,但我必须使用不同的混合状态来渲染PNG和场景。你可以编辑你的anserw并在那里放置关于混合状态的信息吗?读者会更清楚,因为最后的anserw现在部分在评论中;-) – PolGraphic