How to implement render steps for a glass shader?

First, let me explain what I’d like to accomplish. I’d like for some objects in the scene to refract (fake refraction is fine) what’s behind them. I’ve seen some refraction shaders that use environment maps but that’s no good for nearby geometry. Now, I’m no expert in shaders but I imagine one way to accomplish that would be the following:

  1. Render the scene without the glass or otherwise fake-refractive objects into a texture.
  2. Render this texture into the final scene but preserving the original depth buffer. (this might be unnecessary, I’m not sure… maybe a parasite buffer?)
  3. Render the glass objects from the same perspective with depth testing, with a pixel shader that takes the texture from step 1. as a sampler2D.

I can’t seem to figure out how to do this kind of stuff in Panda. I’ve seen the samples that employ shaders and I understand those but none of them seems to be doing something similar to this.

I’m also open to suggestions on alternate ways of accomplishing this kind of effect.

Approximated (fake) refraction is actually quite easy when using shaders. There are as you mentioned several ways to do this, it all depends what your doing.

For flatter objects (stained glass, pools of water, waterfalls, etc.), essentially you just use projective texture mapping to map a off screen texture of the scene onto the object with perturbed texture coordinates according to a normal map.

For more curved surfaces (like teapots, characters), its better (and slower) to use a dynamic cube map for generating dynamic cube maps quite easily and also if you model your object to take up a unit cube in model space, you can do some tricks for more accurate refractions on the shader side.

Just for reference when using cube maps all you have to do is make the cube map available to the shader NodePath.setShaderInput as well as NodePath.setTexture will work then reference it by as a samplerCUBE and then call texCUBE(tex_0, cameraVector). Perturb the camera vector with a normal map if you wish for bumpy reflections.

If you would like code to do projective texture mapping i have that too.

Thanks, I figured out how to do what I was thinking of by looking at the deferred shading example in more depth. You were right though, it only looks ok for flatter objects. For curvier objects I get blue pixels in some places because the texcoords are sampling outside of the background texture. I guess dynamic cubemaps are the way to go but I assume they’re really expensive.

I suppose I’ll have to experiment further to get the effect I’m looking for. At least now I figured out how to work with Panda with this kind of thing.