简体   繁体   中英

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

I'm looking for the simplest way to draw a line between two screen coordinates within the context of a Present call. I'm quite a beginner when it comes to DX11, but I was shocked that there was no rudimentary "simple" way to draw a line.

To re-iterate, I'm looking for the easiest way to draw a 2D line with access to the IDXGISwapChain and access to DX functions:

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

The easiest way to draw a single-pixel line with Direct3D 11 is to use DirectX Tool Kit and the PrimitiveBatch class in combination with 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 can natively draw single-pixel 'textured-lines', but typically if you need anything fancy like wide-lines, etc. use Direct2D to do the drawing since it's a full vector-based renderer.

If you want to use DirectX 12, see DirectX Tool Kit for DirectX 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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