在 OpenGL 中使用正方形纹理映射梯形的纹理
我一直在尝试用方形纹理渲染 GL_QUAD (形状为梯形)。我想尝试使用 OpenGL 来实现这一点。现在纹理变得严重扭曲,这真的很烦人。
通常,我会加载纹理计算单应性,但这意味着大量的工作和额外的线性编程库/直接线性变换函数。我的印象是 OpenGL 可以为我简化这个过程。
我浏览了网络,看到了“透视正确纹理” 、Q 坐标和 GLSL" 和 “OpenGL 中的倾斜/剪切纹理映射”。
这些似乎都假设您将进行某种类型的单应性计算或使用我不知道的 OpenGL 的某些部分......有什么建议吗?
更新:
我一直在阅读"使用图像空间简化和变形导航静态环境” [PDF] - 第 9 页附录 A。
看起来他们通过将 (s,t,r,q) 纹理坐标与模型的世界空间 z 分量的顶点相乘来禁用透视校正。
因此,对于梯形四边形的给定纹理坐标 (s, r, t, q),其中 4 个分量是:
(0.0f, 0.0f, 0.0f, 1.0f),
(0.0f, 1.0f, 0.0f, 1.0f),
(1.0f, 1.0f, 0.0f, 1.0f),
(1.0f, 0.0f, 0.0f, 1.0f)
这与 glTexCoord4f (svert.z, r垂直 z、t、q*垂直 z)?或者我错过了一些步骤?喜欢搞乱 GL_TEXTURE glMatrixMode 吗?
更新 #2:
成功了!请记住,伙计们,这个问题遍布整个网络,并且没有任何简单的答案。大多数涉及使用原始形状和变换后的形状之间的单应性直接重新计算纹理......又名大量线性代数和外部 BLAS lib 依赖项。
I've been trying to render a GL_QUAD (which is shaped as a trapezoid) with a square texture. I'd like to try and use OpenGL only to pull this off. Right now the texture is getting heavily distorted and it's really annoying.
Normally, I would load the texture compute a homography but that means a lot of work and an additional linear programming library/direct linear transform function. I'm under the impression OpenGL can simplify this process for me.
I've looked around the web and have seen "Perspective-Correct Texturing, Q Coordinates, and GLSL" and "Skewed/Sheared Texture Mapping in OpenGL".
These all seem to assume you'll do some type of homography computation or use some parts of OpenGL I'm ignorant of ... any advice?
Update:
I've been reading "Navigating Static Environments Using Image-Space Simplification and Morphing" [PDF] - page 9 appendix A.
It looks like they disable perspective correction by multiplying the (s,t,r,q) texture coordinate with the vertex of a model's world space z component.
so for a given texture coordinate (s, r, t, q) for a quad that's shaped as a trapezoid, where the 4 components are:
(0.0f, 0.0f, 0.0f, 1.0f),
(0.0f, 1.0f, 0.0f, 1.0f),
(1.0f, 1.0f, 0.0f, 1.0f),
(1.0f, 0.0f, 0.0f, 1.0f)
This is as easy as glTexCoord4f (svert.z, rvert.z, t, q*vert.z)? Or am I missing some step? like messing with the GL_TEXTURE glMatrixMode?
Update #2:
That did the trick! Keep it in mind folks, this problem is all over the web and there weren't any easy answers. Most involved directly recalculating the texture with a homography between the original shape and the transformed shape...aka lots of linear algebra and an external BLAS lib dependency.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是对该问题的一个很好的解释&解决方案。
http://www.xyzw.us/~cass/qcoord/
工作链接: http://replay.web.archive.org/20080209130648/http://www.r3.nu/~cass/qcoord/
部分复制并改编自上述链接,由 Cass 创建
Here is a good explanation of the issue & solution.
http://www.xyzw.us/~cass/qcoord/
working link: http://replay.web.archive.org/20080209130648/http://www.r3.nu/~cass/qcoord/
Partly copied and adapted from above link, created by Cass
我猜想大多数想要在梯形上安装矩形纹理的人都会考虑以下两个结果之一:
这里的大多数解决方案都属于第一组,而我最近发现自己属于第二组。
我发现实现效果 2. 的最简单方法是将梯形分成矩形和直角三角形。就我而言,梯形是规则的,所以一个四边形和两个三角形解决了问题。
I would guess that most people wanting to fit a rectangular texture on a trapezoid are thinking of one of two results:
Most solutions here on SO fall into the first group, whereas I recently found myself in the second.
The easiest way I found to achieve effect 2. was to split the trapezoid into a rectangle and right triangles. In my case the trapezoid was regular, so a quad and two triangles solved the problem.
希望这可以帮助:
引自论文:
”
在每个像素处,使用 (s=w; t=w; r=w; q=w) 的内插值执行除法,产生 (s=q; t=q),其中
是最终的纹理坐标。要禁用此效果,这不是
可以直接在 OpenGL 中实现。 “
在 GLSL 中,(至少现在)这是可能的。您可以添加:
noperspective out vec4 v_TexCoord;
有一个解释:
https://www.geeks3d.com/20130514/opengl-interpolation -限定符-glsl-tutorial/
Hope this can help:
Quoted from the paper:
"
At each pixel, a division is performed using the interpolated values of (s=w; t=w; r=w; q=w), yielding (s=q; t=q), which
are the final texture coordinates. To disable this effect, which is not
possible in OpenGL directly. "
In GLSL, (now at least) this is possible. You can add:
noperspective out vec4 v_TexCoord;
there's an explanation:
https://www.geeks3d.com/20130514/opengl-interpolation-qualifiers-glsl-tutorial/