Open GL ES 2.0中的问题glTexGen [英] Problem glTexGen in Open GL ES 2.0

查看:169
本文介绍了Open GL ES 2.0中的问题glTexGen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此帖子类似的问题: OpenGL ES 2.0中的glTexGen

I have a problem somehow similar to this post : glTexGen in OpenGL ES 2.0

我在网上查找了其他两个网站,却没有找到解决问题的方法.

I've looked up on the web a couple of other websites without finding out how to solve my issue.

基本上,我想在设置正交投影后将纹理映射到2D正方形.我遇到的问题是纹理失真,正如您在这张图片上看到的那样,其中我使用了彩色图案而不是纹理:

Basically, I want to map a texture onto a 2D quad after having set up an orthographic projection. The problem I have is that the texture is distorted as you can see on this picture where I used a coloured pattern instead of a texture:

我希望纹理的线条在四边形上保持直线,但如您所见,它们会变形.我想我应该修改顶点着色器以使纹理坐标映射四边形,但是我不知道怎么做.

I would like the lines of the texture to stay straight on the quad, but as you can see, they are distorted. I guess that I should modify the vertex shader in order for the texture coordinates to map the quad but I can't figure out how.

非常感谢, 吉姆

推荐答案

您需要的是另一种插值(我相信这是二次插值),因为在这种情况下(空间中的边长与边不对应)长度),线性插值不会将其剪切.

what you need is a different kind of interpolation (I believe that would be quadratic interpolation), because in this case (edge lengths in space do not correspond with edge lengths in texture), linear interpolation won't cut it.

不幸的是,这是在OpenGL中完成的相当复杂的任务(尽管使用软件光栅化非常简单).您试图在屏幕空间中找到任意四边形到纹理空间中的单位正方形的变换.您可以在这里找到问题的完整解决方案: http://alumni.media.mit.edu /〜cwren/interpolator/这将为您提供一个矩阵,您需要通过该矩阵乘以屏幕空间坐标以获得正确的纹理坐标(在片段着色器中).

It is unfortunately rather complex task to do in OpenGL (very simple using software rasterization, though). You are trying to find a transform of an arbitrary quad in screenspace onto a unit square in texture space. You can find complete solution of your problem here: http://alumni.media.mit.edu/~cwren/interpolator/ This would get you a matrix by which you need to multiply screenspace coordinates to get correct texture coordinates (in fragment shader).

由于涉及相当讨厌的数学运算,因此我建议一个简单的解决方案,该解决方案实际上适用于简单的静态情况(需要手动调整).实际上,在顶点着色器中计算出的纹理坐标仅在中心边缘处处于关闭状态,其余部分是正确的.假设您的texcoords为(0,0),(1、0),(1、1)和(0,1),则校正因子为:

As that involves rather nasty math, i would suggest a simple solution which actually kind of works for simple static cases (requires manual tweaking). The texture coordinate calculated in vertex shader is actually only off on the center edge, the rest is correct. Assuming your texcoords are (0, 0), (1, 0), (1, 1) and (0, 1), The correction factor is:

u * v // for the first triangle
(1 - u) * (1 - v) // for the second triangle

您需要估计校正向量,然后将它们全部组合在一起.我使用一个简单的C ++ OpenGL应用程序进行了测试,并绘制了以下内容:

You need to estimate the correction vector, and then put it all together. I tested using a simple C++ OpenGL app, i draw the following:

Vector2f ta(0, 1), tb(1, 1), tc(1, 0), td(0, 0);
Vector2f a(-.1f, 1), b(1, .7f), c(1, .1f), d(0, 0);
// quad texcoords and vertices

Vector2f t_corr(-.01f, .5f);
// correction for the above quad

glBegin(GL_TRIANGLES);
glMultiTexCoord2f(GL_TEXTURE1, 1, -1); // first triangle
glTexCoord4f(ta.x, ta.y, t_corr.x, t_corr.y);
glVertex2f(a.x, a.y);
glTexCoord4f(tb.x, tb.y, t_corr.x, t_corr.y);
glVertex2f(b.x, b.y);
glTexCoord4f(tc.x, tc.y, t_corr.x, t_corr.y);
glVertex2f(c.x, c.y);

glMultiTexCoord2f(GL_TEXTURE1, 0, 1); // second triangle
glTexCoord4f(ta.x, ta.y, t_corr.x, t_corr.y);
glVertex2f(a.x, a.y);
glTexCoord4f(tc.x, tc.y, t_corr.x, t_corr.y);
glVertex2f(c.x, c.y);
glTexCoord4f(td.x, td.y, t_corr.x, t_corr.y);
glVertex2f(d.x, d.y);
glEnd();

顶点着色器看起来像这样:

And the vertex shader looks like this:

void main()
{
    gl_Position = ftransform();
    gl_FrontColor = gl_Color;
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_TexCoord[1] = gl_MultiTexCoord1; // just copy coords, nothing else to do
}

片段着色器:

uniform sampler2D sam;
void main()
{
    vec2 corr = vec2(gl_TexCoord[1].x + gl_TexCoord[1].y * gl_TexCoord[0].x,
                     gl_TexCoord[1].x + gl_TexCoord[1].y * gl_TexCoord[0].y);
    gl_FragColor = texture2D(sam, gl_TexCoord[0].xy + gl_TexCoord[0].zw * corr.x * corr.y);
}

这很简单,但是通过适当地调整校正向量,您可以做到:

This is very simple, but by adjusting the correction vector properly, you can make this into that: http://www.luki.webzdarma.cz/up/deskew.png

另一种选择是使用3D实心四边形执行此操作,然后将其手动旋转到看起来像2D四边形的位置.或为四边形设计深度"纹理坐标,在顶点着色器中计算1/深度,进行插值(u/深度,v/深度,1/深度),然后在片段着色器中按插值1/深度除以得到良好的值.尽管这看起来很简单,但请注意,很难得出好的深度值.

Another option is to do this with a real quad in 3D and manually rotate it to such position that it looks like your 2D quad. Or devise "depth" texture coordinates for your quad, calculate 1/depth in vertex shader, interpolate (u/depth, v/depth, 1/depth), and divide by interpolated 1/depth later on in fragment shader to get good values. While this might seem simple, note it is quite hard to come up with good depth values.

这篇关于Open GL ES 2.0中的问题glTexGen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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