我正在使用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());
该alpha的作品相反。 0.0f表示完全透明,1.0f完全可见。 (alpha-value = opacity) – Gnietschow