Http://blog.csdn.net/a3070173/archive/2008/11/13/3290091.aspx
The effect of using shader to implement freell and color dispersion is very simple. In the CG tutorial and OpenGL Shader Language, there are
For a detailed introduction, I personally think it is important to note that the reflection vector and the refraction vector should be calculated in the world space,
Calculation in model space and camera space results in incorrect results.
Specific shader code:
Vertex shader:
Const float g_feta = 0.66; // refractive ratio of air to glass
Const float g_fetar = 0.65;
Const float g_fetag= 0.67;
Const float g_fetab= 0.69;
Const float g_ffresnelpower = 0.8;
Const float F = (1.0-g_feta) * (1.0-g_feta)/(1.0 + g_feta) * (1.0 + g_feta ));
Uniform vec3 g_vec3camerepositioninworld;
Uniform int g_irendermode;
Varying vec3 g_vec3reflect;
Varying vec3 g_vec3refract;
Varying vec3 g_vec3refract_r;
Varying vec3 g_vec3refract_g;
Varying vec3 g_vec3refract_ B;
Varying float g_ffresnelratio;
Void main ()
{
Vec3 NV = normalize (gl_vertex.xyz-g_vec3camerepositioninworld );
Vec3 n = gl_normal;
// Calculate the Fresnel Ratio
G_ffresnelratio = F + (1.0-f) * POW (1.0-dot (-NV, n), g_ffresnelpower );
// Calculate the reflection and refraction light
If (g_irendermode = 0)
{
G_vec3reflect = reflect (NV, N );
G_vec3reflect.y =-g_vec3reflect.y;
}
Else if (g_irendermode = 1)
{
G_vec3refract = refract (NV, N, g_feta );
G_vec3refract.y =-g_vec3refract.y;
}
Else if (g_irendermode = 2)
{
G_vec3reflect = reflect (NV, N );
G_vec3reflect.y =-g_vec3reflect.y;
G_vec3refract = refract (NV, N, g_feta );
G_vec3refract.y =-g_vec3refract.y;
}
Else
{
G_vec3reflect = reflect (NV, N );
G_vec3reflect.y =-g_vec3reflect.y;
G_vec3refract_r = refract (NV, N, g_fetar );
G_vec3refract_r.y =-g_vec3refract_r.y;
G_vec3refract_g = refract (NV, N, g_fetag );
G_vec3refract_g.y =-g_vec3refract_g.y;
G_vec3refract_ B = refract (NV, N, g_fetab );
G_vec3refract_ B .y =-g_vec3refract_ B .y;
}
Gl_position = ftransform ();
}
}
Fragment coloring tool:
Varying vec3 g_vec3reflect;
Varying vec3 g_vec3refract;
Varying vec3 g_vec3refract_r;
Varying vec3 g_vec3refract_g;
Varying vec3 g_vec3refract_ B;
Varying float g_ffresnelratio;
Uniform samplercube g_cubemap;
Uniform int g_irendermode;
Void main ()
{
Vec3 vec3finalcolor = vec3 (0.0 );
If (g_irendermode = 0)
{
Vec3finalcolor = vec3 (texturecube (g_cubemap, g_vec3reflect ));
}
Else if (g_irendermode = 1)
{
Vec3finalcolor = vec3 (texturecube (g_cubemap, g_vec3refract ));
}
Else if (g_irendermode = 2)
{
Vec3 vec3reflectcolor = vec3 (texturecube (g_cubemap, g_vec3reflect ));
Vec3 vec3refractcolor = vec3 (texturecube (g_cubemap, g_vec3refract ));
Vec3finalcolor = mix (vec3refractcolor, vec3reflectcolor, g_ffresnelratio );
}
Else
{
Vec3 vec3reflectcolor = vec3 (texturecube (g_cubemap, g_vec3reflect ));
Vec3 vec3refractcolor = vec3 (0.0 );
Vec3refractcolor. r = vec3 (texturecube (g_cubemap, g_vec3refract_r). R;
Vec3refractcolor. G = vec3 (texturecube (g_cubemap, g_vec3refract_g). G;
Vec3refractcolor. B = vec3 (texturecube (g_cubemap, g_vec3refract_ B). B;
Vec3finalcolor = mix (vec3refractcolor, vec3reflectcolor, g_ffresnelratio );
}
Gl_fragcolor = vec4 (vec3finalcolor, 1.0 );
}
Demo:
EXE file: http://www.fileupyours.com/view/219112/GLSL/Fresnel%20And%20Chromatic%20aberration%20Demo.rar