【问题标题】:DirectXTK and 3D OjbectDirectXTK 和 3D 对象
【发布时间】:2015-10-14 13:08:10
【问题描述】:

我有一个使用 D3D11 和 DirectXMath 显示 3D 对象的应用程序。我还想在左上角显示一个 HUD,所以我想我会使用 DirectXTK sprintBatch/spriteFont 来做到这一点。在我添加以下代码之前,我的 3D 效果很好。 DirectXTK 是否使用它自己的着色器并更改某些状态?

问题是我该如何解决这个问题?

m_spriteBatch->Begin();
        const wchar_t* output = L"Hello World";
        m_font->DrawString(m_spriteBatch.get(), output, DirectX::XMFLOAT2{ 10, 10 }, Colors::Yellow);
        m_spriteBatch->End();

Without spritefont->DrawString

With spritefont->DrawString

这是我的着色器。

Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register(s0);
cbuffer WorldViewProjectionType : register(b0)
{
    matrix World;
    matrix View;
    matrix Projection;
};
cbuffer TransparentBuffer
{
    float4 blendAmount;
};
struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};
struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;
    output.Pos.w = 1.0f;
    output.Pos = mul(input.Pos, World);
    output.Pos = mul(output.Pos, View);
    output.Pos = mul(output.Pos, Projection);
    output.Tex = input.Tex;
    return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
    float4 color = txDiffuse.Sample(samLinear, input.Tex);
    color.a = blendAmount.a;
    return color;
}
float4 PSGray(PS_INPUT input) : SV_Target
{
    float4 color = txDiffuse.Sample(samLinear, input.Tex);
    float fLuminance = 0.299f * color.r + 0.587f * color.g + 0.114f *          color.b;
    return float4(fLuminance, fLuminance, fLuminance, blendAmount.a);
}

【问题讨论】:

  • 是的,它确实会改变状态,包括着色器。您可以通过在绘制调用之前恢复渲染对象所需的所有状态来修复它。
  • 这是在黑暗中刺伤。 spriteBatch->Begin() 需要一堆状态。我应该把我的状态放在这里,并在绘制字符串后 spritebatch 恢复它们吗?
  • 嗯,这就是通常的渲染方式。 DirectX API 是一个state machine。在每次迭代的渲染循环中,您应用状态集 1,绘制对象 1,应用状态集 2,绘制对象 2,...,应用状态集 n,绘制对象 n。 DirectXTK 并不神奇,它只是为您包装了其中一对。有一个 ID3D11DeviceContext::ClearState 方法有时会很有用。有关详细信息,请查看一本计算机图形入门书籍。

标签: directx directxmath


【解决方案1】:

亲爱的,我搞定了。嬉皮士。在交换链之后->现在我确实重置了所有这些。

            //*************************************************************************
        m_pImmediateContext->IASetInputLayout(m_pVertexLayout);
        m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
        TurnOffAlphaBlending(m_pImmediateContext);
        // Set the depth stencil state.
        m_pImmediateContext->OMSetDepthStencilState(m_pdepthStencilState, 1);
        m_pImmediateContext->OMSetRenderTargets(1, &m_pdesignerRenderTargetView, m_pdesignerDepthStencilView);
        m_pImmediateContext->RSSetState(m_prasterState);
        //*************************************************************************

【讨论】:

  • 实际上您应该在渲染对象之前执行此操作,而不是在呈现之后。
  • 感谢您的帮助 Drop。
  • 为了提高效率,DirectX Tool Kit 不会“恢复”状态。它改变它使用的任何状态,然后渲染。调用者可以更改他们需要进一步绘制的状态。也就是说,在使用 SpriteBatch 之后,您不需要更改渲染目标。
  • 我在SpriteBatch 文档中添加了一个关于状态管理的部分。
  • Chuck,你能帮我解决这个问题吗?Spritefont in 3d space
猜你喜欢
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2012-11-16
  • 2017-12-14
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多