cuberefract.shader

00001 varying vec3 vEye;
00002 varying vec3 vNormal;
00003 
00004 void main() 
00005 {
00006     // Get the normal and normalize
00007     vNormal = gl_NormalMatrix * gl_Normal;
00008     
00009     // Get the world space position
00010     vec4 vPos = gl_ModelViewMatrix * gl_Vertex;
00011 
00012     // Get the eye vector
00013     vEye = -vPos.xyz;
00014     
00015     //Use the first set of texture coordinates in the fragment shader 
00016     gl_TexCoord[0] = gl_MultiTexCoord0;
00017 
00018     // Transform the position to screen space
00019     gl_Position = ftransform(); 
00020 }
00021 
00022 ~~~~~
00023 
00024 varying vec3 vEye;
00025 varying vec3 vNormal;
00026 
00027 uniform float reflectivity;
00028 uniform float refractiveindex;
00029 
00030 // uniform sampler2D Texture0; // Diffuse map
00031 // uniform sampler2D Texture1; // Normal map
00032 uniform samplerCube Texture2; // Cube map
00033 // uniform sampler2D Texture3; // Shininess map
00034 // uniform sampler2D Texture4; // Emissive map
00035 
00036 void main() 
00037 {
00038     // Get the bump vector
00039     vec3 vN = normalize( vNormal );
00040 
00041     // Get the eye vector
00042     vec3 vE = normalize( vEye );
00043     
00044     // Get the reflection vector
00045     vec3 vReflect = reflect( vE, vN );
00046 
00047     // Get the refraction vector
00048     vec3 vRefract = refract( vE, -vN, 1.0 / refractiveindex );
00049     
00050     // Put the reflection vector through the matrix
00051     vReflect = mat3( gl_TextureMatrix[0] ) * vReflect;
00052     vReflect.x = -vReflect.x;
00053 
00054     // Put the refraction vector through the matrix
00055     vRefract = mat3( gl_TextureMatrix[0] ) * vRefract;
00056     vRefract.x = -vRefract.x;
00057 
00058     // Get the value from the cubemap for the reflection
00059     vec4 vReflectColour = textureCube( Texture2, vReflect.xyz );
00060 
00061     // Get the refraction
00062     vec4 vRefractColour = textureCube( Texture2, vRefract.xyz );
00063 
00064     // Mix the two colours based on the dot product of the eye and normal vector
00065     float interpolant = dot( vE, vN );
00066     if ( interpolant < 0.0 ) interpolant = -interpolant;
00067 
00068     // Reverse the interpolant and raise it to a few powers
00069     interpolant = pow( 1.0 - interpolant, 2.0 );
00070 
00071     // Set the colour
00072     gl_FragColor.rgb = vReflectColour.rgb * interpolant + vRefractColour.rgb * ( 1.0 - interpolant );
00073     
00074     // Set the alpha
00075     gl_FragColor.a = reflectivity;
00076 }

Generated on Fri Mar 23 12:55:03 2007 for glsldemo by  doxygen 1.5.1-p1