我遇到了完全相同的問題,我解決了這個問題。
我的頂點存儲在D3D11_PRIMITIVE_TOPOLOGY_LINELIST頂點緩衝區中。
謝謝你這個有用的文章,你讓我今天修復這個bug。 這真的比我想象的更復雜。
這裏有幾行我的代碼。
// 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調用,它似乎工作得很好。
來源
2015-09-25 21:56:41
Pcp
您不再需要調整DX10 +中的像素座標,只是衆多改進中的一個(在驅動程序級iirc中沒有完成)。這個問題讓我想起了一箇舊的OpenGL bug列表繪圖... – Necrolis 2011-05-05 13:53:29
當我不添加0.5f的座標,行變成2px厚,這意味着對我來說,它恰好在2像素之間渲染目標 – sandicz 2011-05-05 14:05:23
渲染目標:20x20px 渲染目標上的線頂點:(1,1) - >(18,1) 該行將填充從(1,1)到(17,1)的像素並添加0.5 f調整 – sandicz 2011-05-05 14:10:00