D3D11:如何绘制简单的像素对齐线?
我尝试用 D3D11 在两个顶点之间画一条线。我在 D3D9 和 D3D11 中有一些经验,但在 D3D11 中绘制一条线(从一个给定像素开始并以另一个像素结束)似乎是一个问题。
我做了什么:
我向每个顶点的像素坐标添加了 0.5f 以适应纹素/像素坐标系(我阅读了 Microsoft 页面以了解 D3D9 和 D3D11 坐标系之间的差异):
f32 fOff = 0.5f; ColoredVertex newVertices[2] = { { D3DXVECTOR3(fStartX + fOff, fStartY + fOff,0), vecColorRGB }, { D3DXVECTOR3(fEndX + fOff, fEndY + fOff,0), vecColorRGB } ;
生成一个正交投影矩阵以适合渲染目标:
D3DXMatrixOrthoOffCenterLH(&MatrixOrthoProj,0.0f,(f32)uRTWidth,0.0f,(f32)uRTHeight,0.0f,1.0f); D3DXMatrixTranspose(&cbConstant.m_matOrthoProjection,&MatrixOrthoProj);
设置 RasterizerState、BlendState、Viewport、...
- 将顶点绘制为 D3D11_PRIMITIVE_TOPOLOGY_LINELIST
问题: 该线似乎短了一个像素。它从给定的像素坐标开始并完美契合。线条的方向看起来是正确的,但我想要线条结束的像素仍然没有着色。看起来这条线只是短了一个像素...
是否有教程解释这个问题或者有人有同样的问题吗?我记得在 D3D9 中这并没有那么困难。
请询问您是否需要更多信息。
谢谢,Stefan
编辑:找到 d3d10 的光栅化规则(应该与 d3d11 相同): http://msdn.microsoft.com /en-us/library/cc627092%28v=vs.85%29.aspx#Line_1
我希望这能帮助我理解......
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:
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 }
};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);Set RasterizerState, BlendState, Viewport, ...
- 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据光栅化规则(上面问题中的链接),我可能找到了一个可行的解决方案:
这是需要的告诉光栅化器应该绘制线条的最后一个像素。
如果您知道更好的解决方案,请告诉我。
According to the rasterisation rules (link in the question above) I might have found a solution that should work:
This is needed to tell the rasterizer that the last pixel of the line should be drawn.
If you know a better solution, please let me know.
我不知道 D3D11,但你的问题听起来很像 D3D9 中的 D3DRS_LASTPIXEL 渲染状态 - 也许你需要研究一下 D3D11 的同等情况。
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.
我遇到了完全相同的问题,感谢这次讨论,我解决了。
我的顶点存储在 D3D11_PRIMITIVE_TOPOLOGY_LINELIST 顶点缓冲区中。
感谢这篇有用的帖子,你今天让我修复了这个错误。
这真的比我一开始想象的要棘手。
这是我的几行代码。
我测试了几个 DrawLine2D 调用,它似乎工作得很好。
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.
I tested Several DrawLine2D calls, and it seems to work well.