C++ DirectX 11 Blending

39 views Asked by At

I have a question about c++ in connection with DirectX11. I'm drawing on an overlay (WS_EX_LAYERED) and want to draw my things there in 3D space. First a circle. I do it like this: PixelShader:

#pragma once

static inline const char* g_circlePixelShader = R"(
    cbuffer circleData : register(b0)
    {
        float3 center;
        float radius;
        float thickness;
    }

    struct PS_Input
    {
        float3 worldPosition : POS;
        float4 screenPosition : SV_POSITION;
        float4 color : COL;
    };

    float4 ps_main(PS_Input input) : SV_TARGET
    {
        float distanceToCenter = distance(input.worldPosition, center);
        float distanceToBorder = abs(distanceToCenter - radius);

        float alpha = saturate(distanceToBorder / thickness);

        return input.color * (1 - alpha);
    }
)";

BlendState:

bool DxRenderer::createBlendState()
{
    D3D11_BLEND_DESC blendDesc;
    ZeroMemory(&blendDesc, sizeof(blendDesc));
    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_SRC_ALPHA;
    blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_MAX;
    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
    CHECK_FAILED(m_device->CreateBlendState(&blendDesc, &m_blendState));
    
    return true;
}

DrawCircle function:

void DxRenderer::drawCircle(const DirectX::XMFLOAT3& center, float radius, float thickness, const DirectX::XMFLOAT4& color)
{
    struct ConstantBufferCircle
    {
        DirectX::XMFLOAT3 center;
        float radius;
        float thickness;
    };

    ConstantBufferCircle constantBuffer;
    constantBuffer.center = center;
    constantBuffer.radius = radius;
    constantBuffer.thickness = thickness;

    D3D11_MAPPED_SUBRESOURCE mappedSubresource;
    m_deviceContext->Map(m_constantBufferPixel, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubresource);
    memcpy(mappedSubresource.pData, &constantBuffer, sizeof(ConstantBufferCircle));
    m_deviceContext->Unmap(m_constantBufferPixel, 0);

    float realRadius = radius + thickness;

    Vertex3D vertexData[4] =
    {
        { { center.x - realRadius, center.y, center.z - realRadius }, color },
        { { center.x + realRadius, center.y, center.z - realRadius }, color },
        { { center.x + realRadius, center.y, center.z + realRadius }, color },
        { { center.x - realRadius, center.y, center.z + realRadius }, color }
    };

    unsigned int indexData[6] =
    {
        0, 1, 2,
        0, 2, 3
    };

    D3D11_MAPPED_SUBRESOURCE mappedSubresourceVertex;
    m_deviceContext->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubresourceVertex);
    memcpy(mappedSubresourceVertex.pData, vertexData, sizeof(vertexData));
    m_deviceContext->Unmap(m_vertexBuffer, 0);

    D3D11_MAPPED_SUBRESOURCE mappedSubresourceIndex;
    m_deviceContext->Map(m_indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubresourceIndex);
    memcpy(mappedSubresourceIndex.pData, indexData, sizeof(indexData));
    m_deviceContext->Unmap(m_indexBuffer, 0);


    m_deviceContext->PSSetShader(m_circlePixelShader, nullptr, 0);


    m_deviceContext->DrawIndexed(6, 0, 0);
}

Example image

The problem is as soon as I want to draw circles on top of each other it doesn't work. The red triangle appears

0

There are 0 answers