Sign in to follow this  
mux

Glass Shader

Recommended Posts

Basically you need to do a lookup in an environment cube map, using the refracted eye vector.

I'm pretty certain you can find several examples of glass shaders lying around. To begin with I guess you'll need to understand Snell's law and, perhaps also, the Fresnel equations. Here are some articles that helped me while implementing it, anyway:

Snell's law
Fresnel equations

Well, that's the physics part of it, anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by James Trotter
I'm pretty certain you can find several examples of glass shaders lying around. To begin with I guess you'll need to understand Snell's law and, perhaps also, the Fresnel equations. Here are some articles that helped me while implementing it, anyway:

Snell's law
Fresnel equations

Well, that's the physics part of it, anyway.


It's almost impossible to get glass to look right without the fresnel equations. Effects that we're used to seeing when we look out the window just don't happen when you model glass with constant transparency.

Share this post


Link to post
Share on other sites
Well it depends on the type of glass you want. For basic glassy look here is a Cg shader I have that does the trick:


// fresnel approximation
float my_fresnel( float3 I, float3 N, float power, float scale, float bias )
{
return bias + ( pow( min( 0.0, 1.0 - dot( I, N ) ), power ) * scale );
}

float4 glass_object_main( uniform float3 incident : TEXCOORD0,
uniform float3 normal : TEXCOORD1,

uniform float indRefract, // Index of refraction

uniform samplerCUBE envMap : TEXUNIT0

) : COLOR
{
float3 I = normalize( incident );
float3 N = normalize( normal );

float3 R = reflect( I, N );
float3 T = refract( I, N, indRefract );

float fresnel = my_fresnel( -I, N, 5.0, 0.98, 0.02 );

// reflection in env map
float3 _reflect = texCUBE( envMap, R ).rgb;

// refraction in env map
float3 _refract = texCUBE( envMap, T ).rgb;

float3 final = lerp( _refract, _reflect, fresnel );

return float4( final, 1.0 );
}





If you want a frosted glass look you can get a nice effect of this by using splines. ATI's Ashli viewer has shaders (in GLSL, GLSL, and RenderMan) that does this. It's quite cool.


-SirKnight

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this