DX11 在不使用无关库的情况下绘制简单的二维线
DX11 Draw a simple 2D line without the use of extraneous libraries
我正在寻找在 Present 调用的上下文中在两个屏幕坐标之间画一条线的最简单方法。对于 DX11,我是一个初学者,但令我震惊的是没有基本的 "simple" 画线方式。
重申一下,我正在寻找通过访问 IDXGISwapChain 和访问 DX 函数来绘制 2D 线的最简单方法:
HRESULT __stdcall D3D11Present(IDXGISwapChain* This, UINT SyncInterval, UINT Flags) {
// do anything here
}
使用 Direct3D 11 绘制单像素线的最简单方法是使用 DirectX Tool Kit 和 PrimitiveBatch
class 结合 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
我正在寻找在 Present 调用的上下文中在两个屏幕坐标之间画一条线的最简单方法。对于 DX11,我是一个初学者,但令我震惊的是没有基本的 "simple" 画线方式。
重申一下,我正在寻找通过访问 IDXGISwapChain 和访问 DX 函数来绘制 2D 线的最简单方法:
HRESULT __stdcall D3D11Present(IDXGISwapChain* This, UINT SyncInterval, UINT Flags) {
// do anything here
}
使用 Direct3D 11 绘制单像素线的最简单方法是使用 DirectX Tool Kit 和 PrimitiveBatch
class 结合 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