cuberefractshinymapnormal.shader

00001 
00002 varying vec3 vEye;
00003 varying vec3 mT;
00004 varying vec3 mB;
00005 varying vec3 mN;
00006 
00007 attribute vec3 tangent;
00008 
00009 void main() 
00010 {
00011     // Get the normal and normalize
00012     mN = normalize( gl_NormalMatrix * gl_Normal );
00013 
00014     // Get this fragment's position
00015     vec4 vPos = gl_ModelViewMatrix * gl_Vertex;
00016 
00017     // Get the eye vector
00018     vEye = -vPos.xyz;
00019 
00020     //Use the first set of texture coordinates in the fragment shader 
00021     gl_TexCoord[0] = gl_MultiTexCoord0;
00022 
00023 ///// Normal mapping specific:
00024 
00025     // Get the tangent and normalise
00026     mT = normalize( gl_NormalMatrix * tangent );
00027 
00028     // Get the binormal
00029     mB = cross( mN, mT );
00030 
00031     // Transform the position to screen space
00032     gl_Position = ftransform(); 
00033 }
00034 
00035 ~~~~~
00036 
00037 varying vec3 vEye;
00038 varying vec3 mT;
00039 varying vec3 mB;
00040 varying vec3 mN;
00041 
00042 uniform float reflectivity;
00043 uniform float refractiveindex;
00044 
00045 uniform sampler2D Texture0; // Diffuse map
00046 uniform sampler2D Texture1; // Normal map
00047 uniform samplerCube Texture2; // Cube map
00048 // uniform sampler2D Texture3; // Shininess map
00049 // uniform sampler2D Texture4; // Emissive map
00050 
00051 void main() 
00052 {
00053     // Get the bump vector
00054     vec3 vTmp = normalize( texture2D( Texture1, gl_TexCoord[0].xy ).xyz * 2.0 - 1.0 );
00055     vTmp.y = -vTmp.y;
00056     
00057     // Get the tbn matrix
00058     vec3 vT = normalize( mT );
00059     vec3 vB = normalize( mB );
00060     vec3 vN = normalize( mN );
00061 
00062     // Put through the inverse of the TBN matrix
00063     vec3 vBump;
00064     vBump.x = vTmp.x * vT.x + vTmp.y * vB.x + vTmp.z * vN.x;
00065     vBump.y = vTmp.x * vT.y + vTmp.y * vB.y + vTmp.z * vN.y;
00066     vBump.z = vTmp.x * vT.z + vTmp.y * vB.z + vTmp.z * vN.z;
00067     
00068     // Get the eye vector
00069     vec3 vE = normalize( vEye );
00070 
00071     // Get the reflection vector
00072     vec3 vReflect = reflect( vE, vBump );
00073 
00074     // Get the refraction vector
00075     vec3 vRefract = refract( vE, -vBump, 1.0 / refractiveindex );
00076     
00077     // Put the reflection vector through the matrix
00078     vReflect = mat3( gl_TextureMatrix[0] ) * vReflect;
00079     vReflect.x = -vReflect.x;
00080 
00081     // Put the refraction vector through the matrix
00082     vRefract = mat3( gl_TextureMatrix[0] ) * vRefract;
00083     vRefract.x = -vRefract.x;
00084 
00085     // Get the value from the cubemap for the reflection
00086     vec4 vReflectColour = textureCube( Texture2, vReflect.xyz );
00087 
00088     // Get the refraction
00089     vec4 vRefractColour = textureCube( Texture2, vRefract.xyz );
00090 
00091     // Mix the two colours based on the dot product of the eye and normal vector
00092     float interpolant = dot( vE, vN );
00093     if ( interpolant < 0.0 ) interpolant = -interpolant;
00094 
00095     // Reverse the interpolant and raise it to a few powers
00096     interpolant = pow( 1.0 - interpolant, 2.0 );
00097 
00098     // Set the colour
00099     gl_FragColor.rgb = vReflectColour.rgb * interpolant + vRefractColour.rgb * ( 1.0 - interpolant );
00100     
00101     // Set the alpha
00102     gl_FragColor.a = reflectivity;
00103 }

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