Jump to content
  • Advertisement
Sign in to follow this  
synthetix

OpenGL Access individual lines of texture in GLSL?

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!