繁体   English   中英

DX11 在不使用无关库的情况下绘制简单的 2D 线

[英]DX11 Draw a simple 2D line without the use of extraneous libraries

我正在寻找在 Present 调用上下文中在两个屏幕坐标之间绘制一条线的最简单方法。 谈到 DX11,我是一个初学者,但我很震惊,没有基本的“简单”方式来画线。

重申一遍,我正在寻找最简单的方法来绘制一条 2D 线,并可以访问 IDXGISwapChain 并访问 DX 函数:

HRESULT __stdcall D3D11Present(IDXGISwapChain* This, UINT SyncInterval, UINT Flags) {
   // do anything here
}

使用 Direct3D 11 绘制单像素线的最简单方法是将DirectX 工具包PrimitiveBatch类与BasicEffect结合使用:

std::unique_ptr<DirectX::CommonStates> m_states;
std::unique_ptr<DirectX::BasicEffect> m_effect;
std::unique_ptr<DirectX::PrimitiveBatch<DirectX::VertexPositionColor>> m_batch;
Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;

m_states = std::make_unique<CommonStates>(m_d3dDevice.Get());

m_effect = std::make_unique<BasicEffect>(m_d3dDevice.Get());
m_effect->SetVertexColorEnabled(true);

void const* shaderByteCode;
size_t byteCodeLength;

m_effect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);

DX::ThrowIfFailed(
    m_d3dDevice->CreateInputLayout(VertexPositionColor::InputElements,
        VertexPositionColor::InputElementCount,
        shaderByteCode, byteCodeLength,
        m_inputLayout.ReleaseAndGetAddressOf()));

m_batch = std::make_unique<PrimitiveBatch<VertexPositionColor>>(m_d3dContext.Get());

m_d3dContext->OMSetBlendState( m_states->Opaque(), nullptr, 0xFFFFFFFF );
m_d3dContext->OMSetDepthStencilState( m_states->DepthNone(), 0 );
m_d3dContext->RSSetState( m_states->CullNone() );

m_effect->Apply(m_d3dContext.Get());

m_d3dContext->IASetInputLayout(m_inputLayout.Get());

m_batch->Begin();

VertexPositionColor v1(Vector3(-1.f, -1.0f, 0.5f), Colors::Yellow);
VertexPositionColor v2(Vector3(1.0f, 1.0f, 0.5f), Colors::Yellow);

m_batch->DrawLine(v1, v2);

m_batch->End();

Direct3D 可以本机绘制单像素“纹理线”,但通常如果您需要宽线等任何奇特的东西,请使用Direct2D进行绘制,因为它是一个完整的基于矢量的渲染器。

如果要使用 DirectX 12,请参阅DirectX 12 的DirectX 工具包

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM