【问题标题】:Computing texture coordinates in fragment shader (iOS/OpenGL ES 2.0)在片段着色器中计算纹理坐标 (iOS/OpenGL ES 2.0)
【发布时间】:2012-07-26 15:33:16
【问题描述】:

我发现在我的片段着色器中,这两个语句给出了相同的输出:

// #1
// pos is set from gl_Position in vertex shader   
highp vec2 texc = ((pos.xy / pos.w) + 1.0) / 2.0; 

// #2 - equivalent?
highp vec2 texc2 = gl_FragCoord.xy/uWinDims.xy;

如果这是正确的,你能解释一下数学吗?我理解#2,这是我想出的,但在一篇论文中看到了#1。这是 NDC(标准化设备坐标)计算吗?

上下文是我正在使用与视口大小相同的 FBO 纹理坐标。一切正常,但我想了解数学。

顶点着色器的相关部分:

attribute vec4 position;
uniform mat4 modelViewProjectionMatrix;
varying lowp vec4 vColor;
// transformed position
varying highp vec4 pos;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;

    // for fragment shader
    pos = gl_Position;
    vColor = aColor;
}

片段着色器的相关部分:

// transformed position - from vsh
varying highp vec4 pos;
// viewport dimensions
uniform highp vec2 uWinDims;

void main()
{
    highp vec2 texc = ((pos.xy / pos.w) + 1.0) / 2.0; 

    // equivalent?
    highp vec2 texc2 = gl_FragCoord.xy/uWinDims.xy;

...

}

【问题讨论】:

    标签: ios opengl-es


    【解决方案1】:

    (pos.xy / pos.w) 是标准化设备坐标 (NDC) 中的坐标值。这个值在每个维度上的范围是 -1 到 1。

    (NDC + 1.0)/2.0 将范围从 (-1 to 1) 更改为 (0 to 1)(屏幕左侧为 0,右侧为 1,顶部/底部类似)。

    或者,gl_FragCoord 以像素为单位给出坐标,因此它的范围为(0 to width)(0 to height)

    将此值除以宽度和高度 (uWinDims),再次给出从屏幕左侧的 0 到右侧的 1 的位置。

    所以是的,它们看起来是等价的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2011-11-23
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      相关资源
      最近更新 更多