如何在2D模式下使用OpenGL渲染完美的wireframed矩形? [英] How to render perfect wireframed rectangle in 2D mode with OpenGL?

查看:1128
本文介绍了如何在2D模式下使用OpenGL渲染完美的wireframed矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:,这样你就知道了:我还没有完全解决这个问题,目前我使用0.5px偏移,它似乎工作,说,它不是适当的解决方案。所以我在寻找真正的交易,钻石退出规则解决方案根本没有工作。

just so you know: I have not solved this problem perfectly yet, currently I am using 0.5px offset, it seems to work, but as others have said, it is not the "proper" solution. So I am looking for the real deal, the diamond exit rule solution didn't work at all.

我相信这是一个在显卡可能,但如果

I believe it is a bug in the graphics card perhaps, but if so, then any professional programmer should have their bullet-proof solutions for this, right?

编辑:我已经购买了一个新的nvidia卡(有ATI卡之前),我仍然经历这个问题。我也在许多很多游戏中看到同样的错误。

I have now bought a new nvidia card (had ATI card before), and i still experience this problem. I also see the same bug in many, many games. So i guess it is impossible to fix in a clean way?

这是错误的图片:

如何克服这个问题问题?如果可能,最好是非着色器解决方案。我试图为第一行设置偏移量,当我自己绘制4个单独的线,而不是使用线框模式,但没有工作得很好:如果矩形大小更改,有时看起来完美的矩形,但有时甚至比我之前的修复。

How do you overcome this problem? Preferrably a non-shader solution, if possible. I tried to set offset for the first line when i drew 4 individual lines myself instead of using wireframe mode, but that didnt work out very well: if the rectangle size changed, it sometimes looked perfect rectangle, but sometimes even worse than before my fix.

这是我渲染的四边形:

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
    glVertex2f(...);
    glVertex2f(...);
    glVertex2f(...);
    glVertex2f(...);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

是的,我知道我可以使用顶点数组或VBO,但这不是重点。

Yes, I know I can use vertex arrays or VBO's, but that isn't the point here.

我也尝试过 GL_LINE_LOOP ,但没有修复错误。

I also tried GL_LINE_LOOP, but it didn't fix the bug.

编辑:一个解决方案是,到目前为止: Opengl pixel perfect 2D drawing by Lie Ryan:

One solution is at, which works so far: Opengl pixel perfect 2D drawing by Lie Ryan:


请注意,OpenGL坐标空间没有整数的概念,
一切都是一个浮动,OpenGL像素的中心真的在
0.5,0.5,而不是它的左上角。因此,如果你想要一个
1px宽行从0,0到10,10(包括),你真的不得不画一个
行从0.5,0.5到10.5,10.5。

如果您打开抗锯齿,如果您
具有抗锯齿,您尝试从50,0到50,100可能会看到
a模糊2px宽线,因为线在两个像素之间。

This will be especially apparent if you turn on anti-aliasing, if you have anti-aliasing and you try to draw from 50,0 to 50,100 you may see a blurry 2px wide line because the line fell in-between two pixels.


推荐答案

虽然你发现将点数移动0.5会使问题消失,这不是你想的原因。

Although you've discovered that shifting your points by 0.5 makes the problem go away it's not for the reason that you think.

答案确实在于钻石退出规则,这也是对 Opengl像素完美2D绘图的正确接受的核心。 a>。

The answer does indeed lie in the diamond exit rule which is also at the heart of the correctly accepted answer to Opengl pixel perfect 2D drawing.

下图显示了四个碎片/像素,其中每个都有一个菱形。四个色点表示四线/线循环的可能起点,即第一个顶点的窗口坐标。

The diagram below shows four fragments/pixels with a diamond inscribed within each. The four coloured spots represent possible starting points for your quad/line loop i.e. the window co-ordinates of the first vertex.

你没有说你画的是什么方式但没关系。我会假设,为了辩解,你是顺时针绘图。问题是所显示的四个片段的左上角是否将通过栅格化您的第一行或最后一行(它不能是两个)产生。

You didn't say which way you were drawing the quad but it doesn't matter. I'll assume, for argument's sake, that you are drawing it clockwise. The issue is whether the top left of the four fragments shown will be produced by rasterising either your first or last line (it cannot be both).


  1. 如果你从黄色顶点开始,那么第一行通过钻石,然后在水平向右移动时退出。

  1. If you start on the yellow vertex then the first line passes through the diamond and exits it as it passes horizontally to the right. The fragment will therefore be produced as a result of the first line's rasterisation.

如果您从绿色顶点开始,则第一行会退出片段,而不会通过钻石,因此永远不会退出钻石。然而,最后一行将垂直穿过它,并在它上升回到绿色顶点时退出。

If you start on the green vertex then the first line exits the fragment without passing through the diamond and hence never exits the diamond. However the last line will pass through it vertically and exit it as it ascends back to the green vertex. The fragment will therefore be produced as a result of the last line's rasterisation.

如果您从蓝色顶点开始,则第一行通过菱形并退出它水平向右通过。

If you start on the blue vertex then the first line passes through the diamond and exits it as it passes horizontally to the right. The fragment will therefore be produced as a result of the first line's rasterisation.

如果您从红色顶点开始,则第一行会退出片段,而不会通过钻石,因此永远不会退出钻石。最后一行也不会通过钻石,因此不会退出,因为它上升回到红色顶点。

If you start on the red vertex then the first line exits the fragment without passing through the diamond and hence never exits the diamond. The last line will also not pass through the diamond and therefore not exit it as it ascends back to the red vertex. The fragment will therefore not be produced as a result of either line's rasterisation.

注意:

这篇关于如何在2D模式下使用OpenGL渲染完美的wireframed矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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