DirectXTK和3D Ojbect
我有一个使用D3D11和DirectXMath显示3D对象的应用程序。我还想在左上角显示一个HUD,所以我想我应该使用DirectXTK sprintBatch/spriteFont来实现这一点。在添加以下代码之前,我的3D效果很好。 DirectXTK是否使用自己的着色器并更改某些状态 问题是我如何解决这个问题DirectXTK和3D Ojbect,directx,directxmath,Directx,Directxmath,我有一个使用D3D11和DirectXMath显示3D对象的应用程序。我还想在左上角显示一个HUD,所以我想我应该使用DirectXTK sprintBatch/spriteFont来实现这一点。在添加以下代码之前,我的3D效果很好。 DirectXTK是否使用自己的着色器并更改某些状态 问题是我如何解决这个问题 m_spriteBatch->Begin(); const wchar_t* output = L"Hello World"; m_font-&g
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();
这是我的着色器
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);
}
亲爱的,我成功了。哎呀。在交换链->礼物之后,我重置了所有这些
//*************************************************************************
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);
//*************************************************************************
是的,它确实会更改状态,包括着色器。您可以通过在draw调用之前恢复渲染对象所需的所有状态来修复它。这是一种暗中操作。spriteBatch->Begin()接受一系列状态。我应该把我的状态放在这里吗?在绘制字符串后spritebatch会恢复它们吗?嗯,渲染通常是这样做的。DirectX API是一个应用程序。在每次迭代的渲染循环中,应用状态集1、绘制对象1、应用状态集2、绘制对象2、…、应用状态集n、绘制对象n。DirectXTK不是魔术,它只是为您包装了其中一对。有一种方法有时可能有用。有关详细信息,请参阅一本计算机图形入门书籍。实际上,您应该在渲染对象之前执行此操作,而不是在呈现对象之后。感谢您的帮助。为了提高效率,DirectX工具包不“还原”状态。它会更改所使用的任何状态,然后进行渲染。调用方可以更改进一步绘制所需的状态。也就是说,在使用
SpriteBatch
之后,您不需要更改渲染目标。我在文档中添加了一个关于状态管理的部分。Chuck,您能帮我解决这个问题吗。