Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine even/odd line of a texture in GLSL ES

I need to remove all odd lines from a texture - this is part of a simple deinterlacer.

In the following code sample, instead of getting the RGB from texture, I choose to output white colour for odd line and red colour for even line - so I can visually check if the result is what I expected.

_texcoord is passed in from vertex shader and has a range of [0, 1] for both x and y

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); 
    }
}

When render to screen, the output isn't expected. Vertically it's RRWWWRRWWW But I am really expecting RWRWRWRW (i.e. alternate between red and white color)

My code run on iOS and target to GLES 2.0 so it should be no different on Android with GLES 2.0

Question: Where did I do wrong?

EDIT

  1. Yes the texture height is correct
  2. I guess my question is: given a _texcoord.y, how to tell if it's referring to odd or even line of the texture.
like image 200
Yi Wang Avatar asked Feb 14 '23 21:02

Yi Wang


1 Answers

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);
}
like image 95
dss Avatar answered Feb 17 '23 20:02

dss