c++ - DirectX 中两个矩形的透明度,一个在另一个后面 - 我看到窗口的背景而不是第二个纹理

标签 c++ directx shader directx-11 pixel-shader

我有一个 DirectX 11 C++ 应用程序,它显示两个带有纹理和一些文本的矩形。 两种纹理均取自 TGA 资源(添加了 alpha channel )。

当我运行程序时,我得到了结果:

enter image description here

怎么了?仔细看看:

enter image description here

矩形的角是透明的(它们应该是)。其余纹理设置为 30% 不透明度(效果也很好)。

但是,当一个纹理(我们称它为 texture1)在另一个纹理(texture2)之上时:

The corners of texture1 are transparent. But behind them I see the background of window, instead of texture2.

换句话说,纹理的透明度与窗口背景相互作用,而不是与其背后的纹理相互作用

我做错了什么?我的程序的哪一部分可以对此负责? 混合选项、渲染状态、着色器代码...?

在我的着色器中,我设置:

technique10 RENDER{
    pass P0{
        SetVertexShader(CompileShader( vs_4_0, VS()));      
        SetPixelShader(CompileShader( ps_4_0, PS()));
        SetBlendState(SrcAlphaBlendingAdd, float4(0.0f, 0.0f, 0.0f, 0.0f), 
            0xFFFFFFFF);
    }    
}

附言 当然,当我将窗口背景从蓝色更改为另一种颜色时,元素仍然具有透明度(角落不是蓝色)。


编辑:

根据 @ComicSansMS(+ 代表 nick,无论如何 ;p ),我尝试更改渲染元素的顺序(我也稍微移动了较小的纹理,以检查是否错误仍然存​​在):

enter image description here

较小的纹理现在位于较大的纹理后面。但是角的问题仍然存在(现在它出现在第二个纹理上)。 我几乎可以肯定,在渲染上面的矩形之前,我在后面绘制了矩形(我看到了代码的行顺序)。

我的深度模板:

    //initialize the description of the stencil state
    ZeroMemory(depthStencilsDescs, sizeof(*depthStencilsDescs));

    //set up the description of the stencil state
    depthStencilsDescs->DepthEnable = true;
    depthStencilsDescs->DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    depthStencilsDescs->DepthFunc = D3D11_COMPARISON_LESS;

    depthStencilsDescs->StencilEnable = true;
    depthStencilsDescs->StencilReadMask = 0xFF;
    depthStencilsDescs->StencilWriteMask = 0xFF;

    //stencil operations if pixel is front-facing
    depthStencilsDescs->FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
    depthStencilsDescs->FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

    //stencil operations if pixel is back-facing
    depthStencilsDescs->BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
    depthStencilsDescs->BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

    //create the depth stencil state
    result = device->CreateDepthStencilState(depthStencilsDescs, depthStencilState2D);

渲染函数:

...
//clear the back buffer
context->ClearRenderTargetView(myRenderTargetView, backgroundColor); //backgroundColor

//clear the depth buffer to 1.0 (max depth)
context->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
context->OMSetDepthStencilState(depthStencilState2D, 1);
context->VSSetShader(getVertexShader(), NULL, 0);
context->PSSetShader(getPixelShader(), NULL, 0);
for(...){
    rectangles[i]->render();
}

混合状态:

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_ONE;      
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;     
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))){
}

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

最佳答案

您的绘制顺序可能有误。

混合不与其背后对象的像素交互,它与当前在帧缓冲区中的像素交互。

因此,如果您在绘制后面的矩形之前绘制前面的矩形,则其混合操作将与此时帧缓冲区中的内容(即背景)交互。

解决方案显然是根据对象在 View 空间中的深度对对象进行排序,并从后向前绘制,尽管这有时说起来容易做起来难(尤其是在允许任意重叠时)。

另一个问题似乎是您以相同的深度值绘制了两个矩形。您的深度测试设置为 D3D11_COMPARISON_LESS,因此一旦在一个像素上绘制了一个三角形,另一个三角形将无法通过该像素的深度测试并且根本不会被绘制。这解释了交换绘图顺序时得到的结果。

请注意,如果您从后向前绘制对象,则根本不需要执行深度测试,因此在这种情况下您可能只想将其关闭。或者,您可以尝试通过为对象提供不同的 Z 值来沿深度轴排列对象,或者只切换到 D3D11_COMPARISON_LESS_EQUAL 进行深度测试。

关于c++ - DirectX 中两个矩形的透明度,一个在另一个后面 - 我看到窗口的背景而不是第二个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24864948/

相关文章:

c++ - 绘制带纹理的四边形看起来变形了

c++ - Matrix Hell - 将 3D 纹理中的点转换为世界空间

调用实例方法时出现 C++ 访问冲突

c++ - OpenGL - 让着色器编译时出现问题

c++ - 在 C++ 中切换到循环

c++ - 迁移 C-->C++ 指向函数的指针会引发编译器错误

c++ - 在 Word 中迭代段落

c++ - 在结构中存储 COM 指针

Android:将 SurfaceTexture 附加到 FrameBuffer

java - 使用 glVertexAttribPointer 和 glDrawElements 从打包的顶点缓冲区中绘制