View Full Version : Refraction in GLSL

04-22-2004, 05:59 AM
I ran GLSL validate on my shaders that were tested on NV & found a lot of incompatibilities. After chanfing all the 1 to 1.0 and removing stuff like asigning to variables that are initializet as varying vec's in the program there is still one thing that I miss...
That is refraction, in NV GLSL implementation it works just fine, but validator keeps swearing :(
What is the formula to calculate refraction?

04-22-2004, 08:09 AM
A quick look in the spec reviels that there's no refract function. I guess you'll have to write your own.


04-22-2004, 08:31 AM
Here's a sample implementation of the refract routine in Cg ...

float3 refract(float3 I, float3 N, float etaRatio)
float cosI = dot(-I, N);
float cosT2 = 1.0f - etaRatio * etaRatio * (1.0f - cosI * cosI);
float3 T = etaRatio * I + ((etaRatio * cosI - sqrt(abs)(cosT2))) * N);
return T * (float3)(cosT2 > 0);

I = incident ray direction
N = surface normal
etaRatio = relative index of refraction
return = refraction vector

Will GLSL eventually add this?

04-23-2004, 05:04 PM
I hope so. It's useful functionality.

04-26-2004, 06:15 AM
using the compositionality of fragment programs, you can have one main() and several sub-shaders you can call from your main, can't you just write it once and use it wherever you want? Let the time do its job and I bet we'll soon see some cool GLSLANG routines packages.

08-23-2005, 07:59 AM
Actually, refract has been included in the newer version of GLSL: