【问题标题】:Why does this OpenGL shader use texture coordinates beyond 1.0?为什么这个 OpenGL 着色器使用超过 1.0 的纹理坐标?
【发布时间】:2011-07-18 15:49:44
【问题描述】:

我正在尝试熟悉 opengl 中的着色器。这是我找到的一些示例代码(使用 openframeworks)。该代码只是分两次模糊图像,首先是水平方向,然后是垂直方向。这是来自水平着色器的代码。我唯一的困惑是纹理坐标。它们超过 1。

void main( void )
{
    vec2 st = gl_TexCoord[0].st;
    //horizontal blur
    //from http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/

    vec4 color;

    color += 1.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * -4.0, 0));
    color += 2.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * -3.0, 0));
    color += 3.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * -2.0, 0));
    color += 4.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * -1.0, 0));

    color += 5.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt , 0));

    color += 4.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * 1.0, 0));
    color += 3.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * 2.0, 0));
    color += 2.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * 3.0, 0));
    color += 1.0 * texture2DRect(src_tex_unit0, st + vec2(blurAmnt * 4.0, 0));

    color /= 5.0;
    gl_FragColor = color;
}

我无法从这段代码中找出正面或反面。纹理坐标应该在 0 和 1 之间,我已经阅读了一些关于当它们大于 1 时会发生什么的信息,但这不是我看到的行为(或者我没有看到连接)。 blurAmnt 在 0.0 和 6.4 之间变化,因此 s 可以从 0 变为 25.6。图像只是根据值或多或少地模糊,我没有看到任何重复的图案。

我的问题归结为:当调用 texture2DRect 中的纹理坐标参数超过 1 时究竟发生了什么?为什么尽管如此,模糊行为仍能完美运行?

【问题讨论】:

  • 您的代码与您链接到的页面不匹配。 texture2DRect的定义是什么?
  • 这是一个 opengl 着色器。在给定 st 坐标的情况下,texture2DRect 只是一个从底层纹理访问像素信息的函数。我链接到的页面实际上是描述模糊算法的代码中的注释,与我的问题没有直接关系,即“为什么尽管纹理坐标大于 1,模糊算法仍然有效?”
  • 我了解它应该是什么,或多或少,但它似乎不是内置的 GLSL(至少我的 OpenGL 4.1 参考卡不包括它)。所以我想也许它是一个用户定义的包装器,并且包装器内部可能会有一些缩放。
  • 显然,我发现它是opengl的扩展stackoverflow.com/questions/6736531/…

标签: opengl glsl shader


【解决方案1】:

[0, 1] 纹理坐标范围仅适用于 GL_TEXTURE_2D 纹理目标。由于该代码使用 texture2DRect(和 samplerRect),它使用 GL_TEXTURE_RECTANGLE_ARB 纹理目标,该目标使用 未标准化的纹理坐标,范围为 [0, width]x[0, height]。

这就是为什么你有“奇怪”的纹理坐标。别担心,它们可以很好地处理这个纹理目标。

【讨论】:

【解决方案2】:

取决于主机代码。如果你看到类似

glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);

然后越界的维度将为零,IIRC。 t 类似。

【讨论】:

  • 随着 s 维度从 1 一直到 6.4,图像变得越来越模糊,如果它只是归零,这不是我所期望的行为。有什么想法吗?
猜你喜欢
  • 2016-01-10
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
相关资源
最近更新 更多