I have a 3D object in my scene and a texture that is the same size of the screen (a render to texture). Is there a way to make the object act like a "mask" for the texture(using glsl), so the texture is aligned with the screen 2D space but only shows what is "inside" the mask object?

What I'm trying to achieve here is this: I have a 3D scene made of cubic tiles, some of these tiles are going to be water and I want to distort whatever is behind them. My idea was to pass the render to texture and then distort it to make a refraction effect. Is this going to work? Am I even suposed to do refraction like this?

  • \$\begingroup\$ Use a stencil buffer to create the mask, I'm thinking. \$\endgroup\$ – Patrick Hughes Feb 28 '12 at 15:55
  • \$\begingroup\$ Riemers has a good article on how to make water, it is in C# & XNA but the theory should be the same. riemers.net/eng/Tutorials/XNA/Csharp/Series4/… \$\endgroup\$ – Luis Estrada Feb 28 '12 at 17:16
  • \$\begingroup\$ The article is really good (I managed to make riples in the water), but I'm still not sure how to set the texture coordinates to the right position \$\endgroup\$ – Luke B. Feb 28 '12 at 23:22
  • \$\begingroup\$ How should I search for this? I can't find this information anywhere.. \$\endgroup\$ – Luke B. Feb 28 '12 at 23:52

This is what I did:

I used the fragment position and translated it to screen coordinates. After that I just had to pick the pixels on my render to texture from that coordinate.

uniform sampler2D myTexture;
uniform float time;

void main (void)
    vec2 pixelPos = (gl_FragCoord.xy / vec2(1024,768) ); //frag position in the screen
    gl_FragColor = texture2D(myTexture,pixelPos);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.