normalpoint.shader

00001 
00002 varying vec4 vColour;
00003 varying vec3 vEye;
00004 varying vec3 vLightDir;
00005 varying float vDist;
00006 varying vec4 vProjected;
00007 
00008 attribute vec3 tangent;
00009 
00010 void main() 
00011 {
00012     //Put the color in a varying variable
00013     vColour = gl_Color * gl_FrontMaterial.diffuse;
00014 
00015     //Use the first set of texture coordinates in the fragment shader 
00016     gl_TexCoord[0] = gl_MultiTexCoord0;
00017 
00018     // Get the normal and normalize
00019     vec3 vN = normalize( gl_NormalMatrix * gl_Normal );
00020 
00021     // Get this fragment's position
00022     vec4 vPos = gl_ModelViewMatrix * gl_Vertex;
00023 
00024     // Get the light's direction
00025     vec3 vTmp = gl_LightSource[0].position.xyz - vPos.xyz;
00026 
00027     // Get the distance
00028     vDist = length( vTmp );
00029 
00030 ///// Normal mapping specific:
00031 
00032     // Get the tangent and normalise
00033     vec3 vT = normalize( gl_NormalMatrix * tangent );
00034 
00035     // Get the binormal
00036     vec3 vB = cross( vN, vT );
00037 
00038     // Get the light's direction
00039     vLightDir.x = dot( vTmp, vT );
00040     vLightDir.y = dot( vTmp, vB );
00041     vLightDir.z = dot( vTmp, vN );
00042 
00043     // Get the eye vector
00044     vTmp = -vPos.xyz;
00045     vEye.x = dot( vTmp, vT );
00046     vEye.y = dot( vTmp, vB );
00047     vEye.z = dot( vTmp, vN );
00048 
00049 ///// Shadow specific:
00050 
00051     // Get the projected position
00052     vProjected = vPos;
00053 
00054     // Transform the position to screen space
00055     gl_Position = ftransform(); 
00056 }
00057 
00058 ~~~~~
00059 
00060 varying vec4 vColour;
00061 varying vec3 vEye;
00062 varying vec3 vLightDir;
00063 varying float vDist;
00064 
00065 uniform sampler2D Texture0;
00066 uniform sampler2DShadow Texture1;
00067 uniform sampler2DShadow Texture2;
00068 uniform sampler2DShadow Texture3;
00069 uniform sampler2DShadow Texture4;
00070 uniform sampler2DShadow Texture5;
00071 uniform sampler2DShadow Texture6;
00072 
00073 uniform int vShadowCount;
00074 varying vec4 vProjected;
00075 
00076 void main() 
00077 {
00078     // Get the ambient colour as a base
00079     vec4 vBase = vec4( 0, 0, 0, 0 ); 
00080     vec4 vLight = vec4( 0, 0, 0, 0 );
00081     float att = 0.0;
00082 
00083     // If we have a shadow...
00084     if ( vShadowCount > 0 )
00085     {
00086         // Transform the projected vector
00087         vec4 tmp[6];
00088         tmp[0] = gl_TextureMatrix[1] * vProjected;
00089         tmp[1] = gl_TextureMatrix[2] * vProjected;
00090         tmp[2] = gl_TextureMatrix[3] * vProjected;
00091         tmp[3] = gl_TextureMatrix[4] * vProjected;
00092         tmp[4] = gl_TextureMatrix[5] * vProjected;
00093         tmp[5] = gl_TextureMatrix[6] * vProjected;
00094     
00095         // Get the shadow
00096         vBase += shadow2D( Texture1, tmp[0].xyz / tmp[0].w );
00097         /*
00098         vBase += shadow2D( Texture2, tmp[1].xyz / tmp[1].w );
00099         vBase += shadow2D( Texture3, tmp[2].xyz / tmp[2].w );
00100         vBase += shadow2D( Texture4, tmp[3].xyz / tmp[3].w );
00101         vBase += shadow2D( Texture5, tmp[4].xyz / tmp[4].w );
00102         vBase += shadow2D( Texture6, tmp[5].xyz / tmp[5].w );
00103         */
00104     }
00105 
00106     // Get the bump vector
00107     vec3 vN = normalize( texture2D( Texture0, gl_TexCoord[0].xy ).xyz * 2.0 - 1.0 );
00108     vN.y = -vN.y;
00109 
00110     // Get the light vector
00111     vec3 vL = normalize( vLightDir );
00112 
00113     // Get NdotL and make sure it's +ve before getting the specular
00114     float NdotL = dot( vN, vL );
00115 
00116     // Do we do specular?
00117     if ( NdotL > 0.0 )
00118     {
00119         // Get the attenuation
00120         att = 1.0 / ( gl_LightSource[0].constantAttenuation +
00121                 gl_LightSource[0].linearAttenuation * vDist +
00122                 gl_LightSource[0].quadraticAttenuation * vDist * vDist );
00123 
00124         // Add the diffuse component of the light
00125         vLight = NdotL * gl_LightSource[0].diffuse;
00126 
00127         // Get the eye vector
00128         vec3 vE = normalize( vEye );
00129 
00130         // Get the reflection vector
00131         vec3 R = reflect( -vL,  vN );
00132 
00133         // Dot it with the eye
00134         NdotL = max( dot( R, vE ), 0.0 ); 
00135 
00136         // Now add the specular component
00137         vLight +=   pow( NdotL, gl_FrontMaterial.shininess ) *      // Power
00138                     gl_FrontMaterial.specular *                     // Material Specular
00139                     gl_LightSource[0].specular;                     // Light Specular
00140     }
00141 
00142     // Output the fragment colour
00143     gl_FragColor = vBase * att * vLight;
00144 }

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