如何确定 GLSL ES 中纹理的偶数/奇数线

Yi *_*ang 4 shader opengl-es glsl

我需要从纹理中删除所有奇数行 - 这是简单去隔行器的一部分。

在下面的代码示例中,我没有从纹理中获取 RGB,而是选择为奇数线输出白色,为偶数线输出红色 - 因此我可以直观地检查结果是否符合我的预期。

_texcoord 从顶点着色器传入,x 和 y 的范围均为 [0, 1]

uniform sampler2D sampler0; /* not used here because we directly output White or Red color */
varying highp vec2 _texcoord;
void main() {
    highp float height = 480.0; /* assume texture has height of 480 */
    highp float y = height * _texcoord.y;
    if (mod(y, 2.0) >= 1.0) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); 
    } else {
        gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); 
    }
}
Run Code Online (Sandbox Code Playgroud)

当渲染到屏幕时,输出不是预期的。垂直方向是 RRWWWRRWWW 但我真的很期待 RWRWRWRW(即在红色和白色之间交替)

我的代码在 iOS 上运行并以 GLES 2.0 为目标,因此在带有 GLES 2.0 的 Android 上应该没有什么不同

问题:我哪里做错了?

编辑

  1. 是的,纹理高度是正确的
  2. 我想我的问题是:给定 _texcoord.y,如何判断它是指纹理的奇数线还是偶数线。

dss*_*dss 7

void main(void)
{
    vec2 p= vec2(floor(gl_FragCoord.x), floor(gl_FragCoord.y));
    if (mod(p.y, 2.0)==0.0)
        gl_FragColor = vec4(texture2D(tex,uv).xyz ,1.0);
    else
        gl_FragColor = vec4(0.0,0.0,0.0 ,1.0);
}
Run Code Online (Sandbox Code Playgroud)