# Looking for an example of glass with XNA or DirectX 9

Any good pointers on how to get the refractive look of glass? I'm looking to create a think piece of glass type of look, sort of the type that you'd have on bathroom shower doors.

I am a little confused about what you are asking. I think you might be asking how to create water droplets in an indirect way. I will answer both. =)

If you want to create water droplets you can use a very simple distortion shader to offset the current pixel coordinates. However, there are many ways to do water droplets. Here is one which works well: http://blogs.msdn.com/b/shawnhar/archive/2009/04/13/motogp-raindrops-keep-falling-on-my-lens.aspx

If you want to create the refractive look of glass then you are going to have to create an environment map. This essentially is your scene rendered from the 6 sides of a cube, if you choose a cube map. I suggest using XNA 4 because it now comes with a built in EnvironmentMapEffect shader.

EnvironmentMapEffect — An environment map effect uses a diffuse texture to color or detail an object, and a cube map texture containing an environment map to shade the object due to the environment. Because the object reflects the scene, this makes the object look more like it is in a real scene. Also, you can use the fresnel parameter to tune the amount of light reflected (shininess) based on the geometry of the surface.

Here are some articles which describe the process in more detail (keep in mind the new EnvironmentMapEffect for XNA 4.0):

http://digierr.spaces.live.com/blog/cns!2B7007E9EC2AE37B!585.entry?wa=wsignin1.0&sa=736960100

http://xna-uk.net/blogs/randomchaos/archive/2009/01/06/basic-hlsl-lighting-techniques-episode-6-environment-maps-reflection-amp-refraction.aspx

• Thanks, it was the environment mapping that I was after, thanks for the links, I did try to find stuff like this but I think I had the wrong taxonomy. – Rhubarb Oct 23 '10 at 20:28

If you want refractions, you will need to copy your back buffer to use as a texture for a distortion pass. There are a couple of ways to do this pass, but the most common is to create a distortion buffer some where early in your rendering pipeline after your Z has been written where you blend in your distortion directions.

You can then do a pass where you find it suitable (after opaque and before transparent for example) where you copy screen to a texture and distort back onto the screen using your distortion map. It will not be correct but it will be quite fast.

An alternative is to copy screen for each glass that is supposed to distort, this will give you a more correct look but it will cost a lot.