Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Use a stencil buffer to create the mask, I'm thinking. – Patrick Hughes Feb 28 '12 at 15:55
Riemers has a good article on how to make water, it is in C# & XNA but the theory should be the same.… – Luis Estrada Feb 28 '12 at 17:16
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 – Luke B. Feb 28 '12 at 23:22
How should I search for this? I can't find this information anywhere.. – Luke B. Feb 28 '12 at 23:52
up vote 2 down vote accepted

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);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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