Access individual lines of texture in GLSL?

Started by
1 comment, last by synthetix 12 years, 8 months ago
Hello, I've referenced this site many times, but this is my first post. I'm new to OpenGL and GLSL, and was wondering if there's way to split a texture up using a GLSL fragment shader. Basically, I want to take a texture and modify it on a per-line basis (kind of like a TV lines effect).

Here's an example using a single-plane input texture of format GL_LUMINANCE:


uniform sampler2DRect texture;
void main(){
float tex = texture2DRect(texture,gl_TexCoord[0].st).r;
//add texture effect
gl_FragColor = vec4(tex,tex,tex,0.0);
}


After linking to the texture via my sampler2DRect, I'd then like to add an effect to it on a per-line basis. Would this require a loop? I assume since "rect" textures are accessed via their true dimensions and not normalized values, I would have to set up a loop based on the texture height. How do I retrieve the texture height, though? Do I have to pass a uniform int that has this information or can I grab Y dimensions from gl_TexCoord[0].t? Or are .st (x/y coords) normalized values?

Any help would be appreciated!
Advertisement
You can't modify a texture via a fragment shader. To achieve your effect, you can do one of two things:

1) Read the texture, apply the filter, and draw the result. This isn't really modifying a texture, you're just drawing the result you get after modifying it. Its not clear if this is what you want to achieve. I'm not sure how this would work anyway if your texture was minified or magnified, as then you won't be sampling exactly on row boundaries. If you are drawing a pixel-perfect texture, then you can use a algorithm similar to what I listed for #2, but just skip the creation of the second texture (see below).

2) Read the texture, apply the filter, and write the result into a second texture. You can do this via Render-to-Texture, which you may have to research more. In this case, what you do is to make a render target with the same dimensions as your texture, and just draw a quad on it. For each pixel in this quad, you sample the equivalent pixel of the original texture, and then you can do a test with gl_FragCoord for your 'per-line' operation:

if (gl_FragCoord.y % 2 == 0) {
//add effect
} else {
//don't add effect
}

In neither case will you want to use a loop. Essentially you just have to think of a fragment shader as drawing only one pixel at a time. You don't create lines or squares or rows out of a fragment shader, each execution is only responsible for a single pixel.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

if (gl_FragCoord.y % 2 == 0) {
//add effect
} else {
//don't add effect
}

Thanks! I got it working. I made the tweaks and then rendered the result to an FBO.

By the way, I had to use the mod() function instead of the modulus operator (%). The shader wouldn't run otherwise.

This topic is closed to new replies.

Advertisement