Opengl透视变形
我遇到了这个奇怪的问题,我希望有人能为我解决这个问题,以便我能够了解出了什么问题并采取相应的行动。在 OpenGL(固定功能)中,我在正交投影中渲染具有内表面的管。
下图显示了结果。它由 4 个顶点环组成,这些环使用左侧所示的索引模式形成三角形。为了您的方便,我对管子上的顶点进行了编号。右侧是正在使用的纹理:
如您所见,纹理严重扭曲。当我最初创建只有 2 个顶点环的管子时,我认为增加环的数量可以修复扭曲,但没有什么乐趣。另外 glHint 似乎不会影响这个特定问题。
纹理坐标似乎没问题。另外,棋盘格图案似乎渲染正确,但我猜扭曲在那个非常特定的图案中是不可见的。
请忽略交叉线,因为其中一条是不存在的边缘;我通过 GL_LINE_LOOP 渲染线框。
I'm having this weird issue and I'm hoping someone could clear this up for me so I can understand what's wrong and act accordingly. In OpenGL (fixed-function) I'm rendering a tube with inner faces in an orthographic projection.
The image below shows the result. It consists of 4 rings of vertices which are forming triangles using the index pattern shown at the left. I numbered the vertices on the tube for your convenience. At the right is the texture being used:
As you can see the texture is being heavily distorted. As I initially created the tube with only 2 rings of vertices, I thought raising the amount of rings would fix the distortion but no joy. Also glHint doesn't seem to affect this specific problem.
The texture coordinates seem to be alright. Also, the checker pattern seems to render correctly, but I guess the distortion is just not visible in that very specific pattern.
Please ignore the crossed lines as one of those is a non-existent edge; I rendered the wireframe through GL_LINE_LOOP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种特殊的效果是由纹理坐标在三角形中插值的方式引起的。发生的情况是,一个方向成为主要组成部分,而另一个方向则倾斜。你的标本恰好很容易出现这种情况。这也恰好是地板或墙壁上的透视投影和纹理的问题。您需要的是所谓的“透视正确纹理”PCT。有一个
glHint
可以实现这一点,但我想,您已经尝试过。坦率地说,避免这种情况的唯一方法是细分并应用透视校正。但幸运的是,这对于基于四边形的几何图形(如您的几何图形)来说很容易。细分边缘时,沿所有 4 条边缘在细分中心处插值纹理坐标,并使用其中 4 条边缘的平均值。仅沿着一个边缘插值纹理坐标正是您想要避免的。
如果您想保持几何数据不变,您可以在片段着色器中实现 PCT。
This particular effect is caused by the way texture coordinates are interpolated in a triangle. What happens is, that one direction becomes the major component, whereas the other is skewed. Your specimen happens to be very prone to this. This also happens to be a problem with perspective projections and textures on floors or walls. What you need is so called "perspective correct texturing" PCT. There's a
glHint
for this but I guess, you already tried that.Frankly the only way to avoid this is by subdividing and applying the perspective correction as well. But fortunately this is easy enough for quadrilateral based geoemtry (like yours). When subdividing the edges interpolate the texture coordinates at the subdivision centers along all 4 edges and use the mean value of a 4 of them. Interpolating the texture coordinate only along one edge is exactly what you want to avoid.
If you want to keep your geometry data untouched you can implement PCT in the fragment shader.
尝试一些细分:
我通常使用
Eigen::Vector2f
对于Vec
。Try some subdivision:
I usually use
Eigen::Vector2f
forVec
.为什么要使用正交投影?如果您使用透视投影,OpenGL 将为您纠正纹理映射。
Why are you using an orthographic projection for this? If you were using a perspective projection, OpenGL would be correcting the texture mapping for you.