在OpenGL中将纹理映射为具有正方形纹理的梯形 [英] texture mapping a trapezoid with a square texture in OpenGL

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

问题描述

我一直在尝试渲染具有正方形纹理的GL_QUAD(形状为梯形).我想尝试并仅使用OpenGL来实现此目的.现在,纹理变得严重变形,这确实很烦人.

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.

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

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.

我环顾了网上,看到了透视图-正确的纹理,Q坐标和GLSL" "OpenGL中的倾斜/剪切纹理映射" .

I've looked around the web and have seen "Perspective-Correct Texturing, Q Coordinates, and GLSL" and "Skewed/Sheared Texture Mapping in OpenGL".

所有这些似乎都假设您将进行某种形式的单应性计算或使用我不知道的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?

更新:

我一直在阅读使用图像导航静态环境-空间简化和变形" [

I've been reading "Navigating Static Environments Using Image-Space Simplification and Morphing" [PDF] - page 9 appendix A.

似乎他们通过将(s,t,r,q)纹理坐标与模型的世界空间z分量的顶点相乘来禁用透视校正.

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.

所以对于给定的纹理坐标(s,r,t,q),形状为梯形的四边形,其中4个分量是:

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) 

这和glTexCoord4f(s vert.z,r vert.z,t,q * vert.z)一样容易吗?还是我错过了一些步骤?像搞砸了GL_TEXTURE glMatrixMode一样?

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?

更新#2:

成功了!谨记大家,这个问题遍布整个网络,而且没有任何简单的答案.大多数涉及直接用原始形状和变换形状之间的单应性来重新计算纹理……又是很多线性代数和外部BLAS lib依赖性.

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/

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

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

Partly copied and adapted from above link, created by Cass

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

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将四边形划分为三角形,并计算纹理坐标的斜率(ds/dx,ds/dy,dt/dx,dt/dy),并使用其在多边形内部插值纹理坐标.对于左下三角形,dx = 1并且ds = 1,但是对于右上三角形,dx <1.当ds = 1时为1.这使得右上三角形的ds/dx大于下右三角形的ds/dx.当纹理映射时,这会产生令人不愉快的图像.

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.

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

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.

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

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天全站免登陆