GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


A Paste by Anonymous
1
 
void main(    in float4 colorIN        : COLOR0,
            in float2 texCoords      : TEXCOORD0,            // The texture map's texcoords
            in float2 normalCoords   : TEXCOORD1,            // The normal map's texcoords
            in float2 specularCoords : TEXCOORD2,
            in float3 vLightVector   : TEXCOORD3,            // The transformed light vector (it is in tangent space)
            in float3 vEyeVectorOut     : TEXCOORD4,

            out float4 colorOUT : COLOR0,                // The final color of the current pixel

            uniform sampler2D baseTexture : TEXUNIT0,    // The whole rock texture map
            uniform sampler2D normalTexture : TEXUNIT1, // The whole normal map
            uniform sampler2D specularTexture : TEXUNIT2,
            uniform float3 fLightDiffuseColor,            // The diffuse color of the light source
            uniform float3 fLightSpecular,
            uniform float  fShininess,
            uniform float  fFalloff )            
{
    // Decompress vector ([0, 1] -> [-1, 1])
    float3 vNormal = 2.0f * (tex2D(normalTexture, normalCoords).rgb - 0.5f);
    
    // Calculate diffuse component 
    float diffuseLight = max(dot(vLightVector, vNormal), 0);
    float3 diffuseFinal = diffuseLight * tex2D(baseTexture, texCoords).rgb * fLightDiffuseColor;
    
    // Calculate specular component
    float3 specular = (pow(max(dot(vNormal, vEyeVectorOut), 0), fShininess)) * tex2D(specularTexture, specularCoords).rgb * fLightSpecular;
    
    if ( diffuseLight <= 0 )
        specular = 0;
    
    // Calculate attenuation
    float distanceToTexel = sqrt ( (vLightVector.x - vNormal.x) * (vLightVector.x - vNormal.x) + 
                                   (vLightVector.y - vNormal.y) * (vLightVector.y - vNormal.y) + 
                                   (vLightVector.z - vNormal.z) * (vLightVector.z - vNormal.z) );
    
    float attenuation = 1 - pow(( distanceToTexel/fFalloff ),2);
    
    // Output the final color
    colorOUT.rgb = attenuation * ( diffuseFinal + specular );
}