D3D11: How to draw a line with aligned pixels?

I tried to draw a line between two peaks with D3D11. I have some impressions of D3D9 and D3D11, but it seems that the problem with D3D11 is to draw a line that starts at one given pixel and ends at another.

What I've done:

  • I added 0.5f to the pixel coordinates of each vertex to fit the texel / pixel coordinate system (I read Microsoft pages for the differences between the D3D9 and D3D11 coordinate systems):

    f32 fOff = 0.5f; ColoredVertex newVertices [2] = {{D3DXVECTOR3 (fStartX + fOff, fStartY + fOff, 0), vecColorRGB}, {D3DXVECTOR3 (fEndX + fOff, fEndY + fOff, 0), vecColorRGB}};

  • An orthoprojection matrix is ​​generated to match the rendering target:

    D3DXMatrixOrthoOffCenterLH (& MatrixOrthoProj, 0.0f, (F32) uRTWidth, 0.0f, (F32) uRTHeight, 0.0f, 1.0f); D3DXMatrixTranspose (& cbConstant.m_matOrthoProjection, & MatrixOrthoProj);

  • Install RasterizerState, BlendState, Viewport, ...

  • Draw vertices as D3D11_PRIMITIVE_TOPOLOGY_LINELIST

Problem: It seems that the line has one pixel. It starts with a given pixel coordinate, which is ideal. The direction of the line looks right, but the pixel in which I want the line to end is still not colored. It looks like the line is just one pixel per short ...

Is any tutorial an explanation for this problem or does anyone have the same problem? As far as I remember, in D3D9 it was not so difficult.

Ask if you need more information.

Thanks Stefan

EDIT: d3d10 ( d3d11): http://msdn.microsoft.com/en-us/library/cc627092%28v=vs.85%29.aspx#Line_1

, ...

+5
3

( ) , :

  • StartX < EndX StartY < Endy
  • (0.5/0.5) ( ),
  • (1.0/1.0) , .

, , .

f32 fXStartOff = 0.5f;
f32 fYStartOff = 0.5f;
f32 fXEndOff = 1.0f;
f32 fYEndOff = 1.0f;

ColoredVertex newVertices[2] = 
{
    { D3DXVECTOR3((f32)fStartX + fXStartOff, (f32)fStartY + fYStartOff,0), vecColorRGB },
    { D3DXVECTOR3((f32)fEndX + fXEndOff , (f32)fEndY + fYEndOff,0), vecColorRGB }
};

, .

+3

D3D11, D3DRS_LASTPIXEL D3D9 - , D3D11 .

+1

, .

D3D11_PRIMITIVE_TOPOLOGY_LINELIST.

, . , .

.

// projection matrix code
float width = 1024.0f;
float height = 768.0f;
DirectX::XMMATRIX offsetedProj = DirectX::XMMatrixOrthographicRH(width, height, 0.0f, 10.0f);
DirectX::XMMATRIX proj = DirectX::XMMatrixMultiply(DirectX::XMMatrixTranslation(- width / 2, height / 2, 0), offsetedProj);

// view matrix code
// screen top left pixel is 0,0 and bottom right is 1023,767
DirectX::XMMATRIX viewMirrored = DirectX::XMMatrixLookAtRH(eye, at, up);
DirectX::XMMATRIX mirrorYZ = DirectX::XMMatrixScaling(1.0f, -1.0f, -1.0f);
DirectX::XMMATRIX view = DirectX::XMMatrixMultiply(mirrorYZ, viewMirrored);

// draw line code in my visual debug tool.
void TVisualDebug::DrawLine2D(int2 const& parStart,
                              int2 const& parEnd,
                              TColor parColorStart,
                              TColor parColorEnd,
                              float parDepth)

{
    FLine2DsDirty = true;

    // D3D11_PRIMITIVE_TOPOLOGY_LINELIST
    float2 const startFloat(parStart.x() + 0.5f, parStart.y() + 0.5f);
    float2 const endFloat(parEnd.x() + 0.5f, parEnd.y() + 0.5f);
    float2 const diff = endFloat - startFloat;
    // return normalized difference or float2(1.0f, 1.0f) if distance between the points is null. Then multiplies the result by something a little bigger than 0.5f, 0.5f is not enough.
    float2 const diffNormalized =  diff.normalized_replace_if_null(float2(1.0f, 1.0f)) * 0.501f;

    size_t const currentIndex = FLine2Ds.size();
    FLine2Ds.resize(currentIndex + 2);
    render::vertex::TVertexColor* baseAddress = FLine2Ds.data() + currentIndex;
    render::vertex::TVertexColor& v0 = baseAddress[0];
    render::vertex::TVertexColor& v1 = baseAddress[1];
    v0.FPosition = float3(startFloat.x(), startFloat.y(), parDepth);
    v0.FColor = parColorStart;
    v1.FPosition = float3(endFloat.x() + diffNormalized.x(), endFloat.y() + diffNormalized.y(), parDepth);
    v1.FColor = parColorEnd;
}

DrawLine2D, , , .

0

All Articles