Having played about with the NVIDIA Glass shader and my own (old) attempts at glass I have come up with this post
Refraction is the light passing through an object but as they do so they bend. This is the effect we will get with this effect. Luckily HLSL has an intrinsic refraction function (refract) that we can use to get this effect. To get the refracted vector we need to calculate the direction of the viewer in relation to the surface and pass that with the surfaces normal to the refract function along with the degree of refraction we want.
So we need to pass in a world view projection and a world matrix as well as the camera position, oh and for the hell of it a tint for colour.
We also need to pass in the Environment Map we are going to use to make the refraction and include a function to make reading it a little simpler
So now we have the bits we need to do the job we need a vertex shader to calculate our position and refraction value. In the NVIDIA shader this was done in the pixel shader, but I figured this may give some performance boost, but doing this means that I have had to make sacrifices (not literal ones) later on.
Now all we need to do is get the correct colour from the environment map
And we get an effect like this
A bit hard to see, click the picture for a bigger version.
And so onto reflection, again HLSL has another function we can use, reflect, and again we need to pass a view direction (incident vector) and the surface normal.
The shaders parameters are the same as before, the only change is to the structures, and all that is changing is the name of the parameters
Again a small change in the vertex shader to populate the Reflect parameter with the result of the intrinsic reflect method
Then as before, we get the colour from the environment map
And we then get this effect
So if we now combine the two effects we get a glass effect, not as nice as the NVIDIA one I grant you, but an effect none the less. Again we have to alter the the output structure
We populate both the parameters as before in the vertex shader
And then in the pixel shader we get the colours from the environment map, I have taken the liberty of using the same method the NVIDIA shader did and used a lerp.
And we get this
Last Minute Addition
Now I kind of liked the prism effect you get with the NVIDIA Glass shader and thought I would add it here, so I have created a forth shader RC3DGlass shader.
So I added the RGB array and the refraction index for the RGB array (etas)
As I mentioned before the view direction and surface normals are calculated in the vertex shader in my version and so I need the vertex shader to output an array of refraction values.
Also I have replaced the in intrinsic refract method with the one in the NVIDIA shader so I can get the nice prism effect. So the vertex shader now looks like this
And finally the pixel shader
As you can see I have had to put a loop in the vertex and the pixel shader which may well take away any perf boot I get from doing the calcs in the vertex shader this also means that the Fresnel term could not be applied, well I tried and could not get a decent effect when calculating in the VS, guess that's why NVIDIA did it in the PS lol
And we finally get this effect
Solution for this post can be down loaded here.