D3D11:如何绘制一条简单的像素对齐线? [英] D3D11: How to draw a simple pixel aligned line?

查看:246
本文介绍了D3D11:如何绘制一条简单的像素对齐线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用D3D11在两个顶点之间画一条线。我在D3D9和D3D11中有一些经验,但是在D3D11中画一条线似乎是一个问题,该线从一个给定的像素开始,到另一像素结束。

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.

我做了什么:


  1. 我向每个顶点的像素坐标添加了0.5f以适合texel- /像素坐标系(我阅读了有关D3D9和D3D11坐标系之间差异的Microsoft页面):

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

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

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

设置RasterizerState,BlendState,视口,...

Set RasterizerState, BlendState, Viewport, ...

问题:
该线似乎是一个像素太短。它从给定的像素坐标开始,非常适合它。线的方向看起来正确,但是我要线结束的像素仍未着色。看起来这条线只是短到一个像素...

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...

是解释这个问题的任何教程还是有人有同样的问题?我记得在D3D9中并不是那么困难。

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.

谢谢,Stefan

编辑:找到d3d10的光栅化规则(对于d3d11应该是相同的):
http://msdn.microsoft.com/en-us/library/cc627092%28v= vs.85%29.aspx#Line_1

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...

推荐答案

根据栅格化规则(上述问题的链接),我可能已经找到了可行的解决方案:

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


  1. 对顶点进行排序StartX< EndX和StartY< EndY

  2. 将(0.5 / 0.5)添加到起始顶点(如我之前所做的那样)以将顶点移动到像素中心

  3. 添加(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 }
};

如果您知道更好的解决方案,请告诉我。

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

这篇关于D3D11:如何绘制一条简单的像素对齐线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆