Refraction in GLSL

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 :frowning:
What is the formula to calculate refraction?

A quick look in the spec reviels that there’s no refract function. I guess you’ll have to write your own.

http://www.3dlabs.com/support/developer/ogl2/downloads/ShaderSpecV1.051.pdf

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?

I hope so. It’s useful functionality.

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.

Actually, refract has been included in the newer version of GLSL:

http://oss.sgi.com/projects/ogl-sample/registry/ARB/GLSLangSpec.Full.1.10.59.pdf

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.