在OpenGL中用方形纹理映射梯形的纹理 [英] texture mapping a trapezoid with a square texture in OpenGL

查看:43
本文介绍了在OpenGL中用方形纹理映射梯形的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用方形纹理渲染 GL_QUAD(形状为梯形).我想尝试使用 OpenGL 来解决这个问题.现在纹理严重扭曲,真的很烦人.

通常,我会加载纹理计算单应性,但这意味着需要大量工作和额外的线性编程库/直接线性变换函数.我的印象是 OpenGL 可以为我简化这个过程.

我环顾了整个网络,看到了"Perspective- 正确的纹理、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, rvert.z, t, q*vert.z) 一样简单吗?还是我错过了一些步骤?喜欢搞乱 GL_TEXTURE glMatrixMode?

更新 #2:

成功了!请记住,这个问题遍布网络,没有任何简单的答案.大多数涉及使用原始形状和转换形状之间的单应性直接重新计算纹理......也就是大量的线性代数和外部 BLAS lib 依赖项.

解决方案

这里很好地解释了这个问题 &解决方案.

http://www.xyzw.us/~cass/qcoord/

工作链接:http://replay.web.archive.org/20080209130648/http://www.r3.nu/~cass/qcoord/

部分复制和改编自上述链接,由 Cass

创建<块引用>

纹理映射更有趣的一个方面是纹理坐标所在的空间.我们大多数人喜欢将纹理空间视为一个简单的 2D 仿射平面.在大多数情况下,这是完全可以接受的,而且非常直观,但有时会出现问题.

例如,假设您有一个空间坐标为梯形但纹理坐标为正方形的四边形.

OpenGL 会将四边形分成三角形并计算纹理坐标的斜率(ds/dx、ds/dy、dt/dx、dt/dy),并使用这些斜率在多边形内部插入纹理坐标.对于左下角三角形,dx = 1,ds = 1,但对于右上角三角形,dx <1 而 ds = 1.这使得右上方三角形的 ds/dx 大于下方三角形的 ds/dx.当纹理映射时,这会产生令人不快的图像.

纹理空间不仅仅是一个 2D 仿射平面,即使我们通常不理会 r=0 和 q=1 默认值.这真的是一个完整的投影空间(P3)!这很好,因为不是将上顶点的纹理坐标指定为 (0, 1) 和 (1, 1) 的 (s,t) 坐标,我们可以将它们指定为 (s,t,r,q) 坐标的(0,宽度,0,宽度)和(宽度,宽度,0,宽度)!这些坐标对应于纹理图像中的相同位置,但看看 ds/dx 发生了什么 - 现在两个三角形都相同!!它们都具有相同的 dq/dx 和 dq/dy.

注意它仍然在 z=0 平面上.将这种技术与透视相机投影一起使用时,可能会变得非常混乱,因为这会产生错误的深度感知".尽管如此,它可能比仅使用 (s,t) 更好.这由你决定.

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.

解决方案

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

One of the more interesting aspects of texture mapping is the space that texture coordinates live in. Most of us like to think of texture space as a simple 2D affine plane. In most cases this is perfectly acceptable, and very intuitive, but there are times when it becomes problematic.

For example, suppose you have a quad that is trapezoidal in its spatial coordinates but square in its texture coordinates.

OpenGL will divide the quad into triangles and compute the slopes of the texture coordinates (ds/dx, ds/dy, dt/dx, dt/dy) and use those to interpolate the texture coordinate over the interior of the polygon. For the lower left triangle, dx = 1 and ds = 1, but for the upper right triangle, dx < 1 while ds = 1. This makes ds/dx for the upper right triangle greater than ds/dx for the lower one. This produces an unpleasant image when texture mapped.

Texture space is not simply a 2D affine plane even though we generally leave the r=0 and q=1defaults alone. It's really a full-up projective space (P3)! This is good, because instead of specifying the texture coordinates for the upper vertices as (s,t) coordinates of (0, 1) and (1, 1), we can specify them as (s,t,r,q) coordinates of (0, width, 0, width) and (width, width, 0, width)! These coordinates correspond to the same location in the texture image, but LOOK at what happened to ds/dx - it's now the same for both triangles!! They both have the same dq/dx and dq/dy as well.

Note that it is still in the z=0 plane. It can become quite confusing when using this technique with a perspective camera projection because of the "false depth perception" that this produces. Still, it may be better than using only (s,t). That is for you to decide.

这篇关于在OpenGL中用方形纹理映射梯形的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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