OpenGL:纹理不能被 2 整除时会扭曲
我有一个使用 OpenGL 进行显示的游戏引擎。 我为它编写了一个小菜单,然后在渲染文本后我注意到一些奇怪的事情。 http://img43.imageshack.us/i/garbagen.png/
作为你看,字体有点难以阅读,但下部(如“测试”)看起来符合预期。 屏幕上的位置似乎会影响可读性,因为边缘会被切割。 字体为 9x5,我将这个值除以 2 以获得宽度/高度并从中心渲染对象。 因此,对于 4.5x2.5 像素(我使用浮点数表示简单矩形的 x、y、宽度和高度),如果在 x.5 左右以外的其他位置渲染,纹理会变得混乱。然而,它目前只在两台计算机上这样做,但我不喜欢出现这个错误,因为它使文本不可读。我可以将其设为 4.55x2.55(通过在除以 2 时添加一点额外的大小),然后它在所有机器中充分渲染(或者至少在有问题的两台机器中不会经常发生),但我担心这个这是一个太粗暴的黑客行为,无法保留它,并且不能完全解决问题,并且它可能会放大文本,使字体看起来......“胖”。 所以我的问题是......有什么方法可以防止这种情况发生,而不是将这些值交换为整数? (我需要比较浮动提供的细微差别)。我能否找出哪些宽度/高度可以被二整除,哪些不能被二整除,并以不同的方式处理它们?如果确实是显卡问题,有办法解决吗? 抱歉,如果这个问题有什么不足的话,我不会经常在互联网上提问,而且我也没有编码研究。我很乐意提供可能需要的任何行或代码块。
I have a game engine that uses OpenGL for display.
I coded a small menu for it, and then I noticed something odd after rendering the text.
http://img43.imageshack.us/i/garbagen.png/
As you see, the font is somewhat unreadable, but the lower parts (like "Tests") look as intended.
Seems the position on the screen affects readability, as the edges get cut.
The font is 9x5, a value that I divide by 2 to obtain width/height and render the object from the center.
So, with 4.5x2.5 pixels (I use floats for x, y, width and height of simple rectangles), the texture is messed up if rendered somewhere other than x.5 or so. However, it only does so in two computers for now, but I would dislike this error to come out since it makes text unreadable. I can make it 4.55x2.55 (by adding a bit of extra size when dividing by 2), and then it renders adequately in all machines (or at least doesn't happen as often in the problematic two), but I fear this is a hack too gross to keep it and doesn't solve the issue entirely, and it might scale the text up making the font look..."fat".
So my question is...is there any way I can prevent this, and not exchanging those values to integers? (I need the slight differences floats offers in comparison). Can I find out which width/heights are divisible by two, and those that aren't, handle them differently? If it's indeed a video card issue, would it be possible to circumvent it?
Sorry if there's anything lacking for the question, I don't resort to questioning the internet often and I have no coding studies. I'll be happy to provide any line or chunk of code that might be required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果必须在非整数坐标处绘制文本,则应启用纹理过滤。使用 glTexParameterfv 设置
GL_TEXTURE_MIN_FILTER
和GL_TEXTURE_MAG_FILTER
到GL_LINEAR
。您的文本将变得模糊,但如果不诉诸像素完美(=整数)坐标,这是无法避免的。请注意,您的 0.05 解决方法除了更改有效坐标舍入为整数的方式外什么也不做。使用 GL_NEAREST 纹理过滤时,不存在半像素偏移之类的情况。即使您指定了这些坐标,纹理过滤器也会为您舍入它们。您只需将其推向正确的方向并附加 0.05。
If you have to draw your text at non-integer coordinates, you should enable texture filtering. Use glTexParameterfv to set
GL_TEXTURE_MIN_FILTER
andGL_TEXTURE_MAG_FILTER
toGL_LINEAR
. Your text will be blurred, but that cannot be avoided without resorting to pixel perfect (=integer) coordinates.Note that your 0.05 workaround does nothing but change the way the effective coordinates are rounded to integers. When using
GL_NEAREST
texture filtering, there's no such thing as a half pixel offset. Even if you specify these coordinates, the texture filter will round them for you. You just push it in the right direction with the additional 0.05.为了获得最佳可靠性,我会找到一种消除分数的方法。我对XNA和MDX只有一点经验,所以我不知道是否有充分的理由,但为什么你要从中心而不是角落走?
For best reliability I would find a way to eliminate the fractions. I only have a little experience with XNA and MDX, so I don't know if there is a good reason, but why are you going by the center rather than corner?
由于分辨率、纹理过滤等不同,在 OpenGL 中尝试做这样的像素完美的事情可能很困难。
您可以尝试以下一些操作:
Trying to do pixel-perfect stuff like this can be hard in OpenGL due to different resolutions, texture filtering etc.
Some things you could try: