7

I tried to draw a line between two vertices with D3D11. I have some experiences in D3D9 and D3D11, but it seems to be a problem in D3D11 to draw a line, which starts in one given pixel and ends in an other.

What I did:

  1. I added 0.5f to the pixel coordinates of each vertex to fit the texel-/pixel coordinate system (I read the Microsoft pages to the differeces between 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 } };

  2. Generated a ortho projection matrix to fit the render target:

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

  3. Set RasterizerState, BlendState, Viewport, ...

  4. Draw Vertices as D3D11_PRIMITIVE_TOPOLOGY_LINELIST

Problem: The Line seems to be one pixel to short. It starts in the given pixel coordinate an fits it perfect. The direction of the line looks correct, but the pixel where I want the line to end is still not colored. It looks like the line is just one pixel to short...

Is the any tutorial explaining this problem or does anybody have the same problem? As I remember it wasn't as difficult in D3D9.

Please ask if you need further information.

Thanks, Stefan

EDIT: found the rasterization rules for d3d10 (should be the same for d3d11): http://msdn.microsoft.com/en-us/library/cc627092%28v=vs.85%29.aspx#Line_1

I hope this will help me understanding...

5
  • You no longer need to adjust the pixle coords in DX10+, just one of the many improvements (its no done at driver level iirc). this problem reminds me of an old OpenGL bug that existed with the line list drawing...
    – Necrolis
    May 5, 2011 at 13:53
  • when i don't add 0.5f to the coordinates, the line becomes 2px thick, which means for me, the it's exactly between 2 pixels in the render target
    – sandicz
    May 5, 2011 at 14:05
  • render target: 20x20px line vertices: (1,1) -> (18,1) on the render target the line fills the pixels from (1,1) to (17,1) with the added 0.5f adjustment
    – sandicz
    May 5, 2011 at 14:10
  • msdn.microsoft.com/en-us/library/bb205073(v=VS.85).aspx see "Mapping texels to pixels". are you use DX11 with a DX9 interface by any chance(aka D3D_FEATURE_LEVEL_9_x?
    – Necrolis
    May 5, 2011 at 15:17
  • i'm using D3D_FEATURE_LEVEL_11_0 with the hardware render device (nvidia geforce gtx 580). the adjustment is done before the vertex shader to hit the center of a pixel, so the coordinates of the vertices are (1.5,1.5) and (18.5,1.5). as there is no difference between texel and pixel (of the render target) there is no adjustment after this anymore
    – sandicz
    May 6, 2011 at 6:45

3 Answers 3

4

According to the rasterisation rules (link in the question above) I might have found a solution that should work:

  1. sort the vertices StartX < EndX and StartY < EndY
  2. add (0.5/0.5) to the start vertex (as i did before) to move the vertex to the center of the pixel
  3. add (1.0/1.0) to the end vertex to move the vertex to the lower right corner

This is needed to tell the rasterizer that the last pixel of the line should be drawn.

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 }
};

If you know a better solution, please let me know.

3
  • mistake: just sort StartX < EndX (as the y-Coordinates belong to them...) and test: if(fStartY > fEndY) fYEndOff = 0.0f;
    – sandicz
    May 6, 2011 at 13:57
  • Well - you can't just add 0.5,0.5 extra to the line or you're changing the slope. To do this you'd need to add 0.5 * slope (i.e. make the line 0.5 pixels longer)
    – Aaron
    May 6, 2011 at 16:00
  • you're right, my solution is not as precise as I want it to be, but on my tests (several lines on a 20x20px render target) the results were ok. I will give a try to your suggestion. Thanks :)
    – sandicz
    May 9, 2011 at 6:37
1

I don't know D3D11, but your problem sounds a lot like the D3DRS_LASTPIXEL render state from D3D9 - maybe there's an equal for D3D11 you need to look into.

1
  • right, sounds like this is what i need. but there are no renderstates in d3d11 anymore. i will look for something equal. thanks :)
    – sandicz
    May 6, 2011 at 6:39
0

I encountered the exact same issue, and i fixed thank to this discussion.

My vertices are stored into a D3D11_PRIMITIVE_TOPOLOGY_LINELIST vertex buffer.

Thank for this usefull post, you made me fix this bug today. It was REALLY trickier than i thought at start.

Here a few line of my code.

// 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;
}

I tested Several DrawLine2D calls, and it seems to work well.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.