Cg help - lighting and specular calculation

Recommended Videos

PiCroft

He who waits behind the wall
Mar 12, 2009
224
0
0
Not sure if this should be in advice, but it is kinda off-topic :p

I am trying to create a bump-map shader that uses the alpha channel of a normal map texture as a specular map. Here is my fragment shader:

Code:
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 float3 vLightVector : TEXCOORD2,             // The transformed light vector (in tangent space)

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

                uniform sampler2D baseTexture : TEXUNIT0,       // The texture map
                uniform sampler2D normalTexture : TEXUNIT1,     // The normal map
                uniform float3 lightcolor)              // The diffuse color of the light source
{
        // We must remember to normalize the light vector as it's linearly interpolated across the surface,
        // which in turn means the length of the vector will change as we interpolate
        vLightVector = normalize(vLightVector);

        // Since the normals in the normal map are in the (color) range [0, 1] we need to uncompress them
        // to "real" normal (vector) directions.
        // Decompress vector ([0, 1] -> [-1, 1])
        float3 vNormal = 2.0f * (tex2D(normalTexture, normalCoords).rgb - 0.5f);
        
        float4 normMap = tex2D(normalTexture, normalCoords);
        
        // Calculate the diffuse component and store it as the final color in 'colorOUT'
        // The diffuse component is defined as: I = Dl * Dm * clamp(L֎, 0, 1)
        // saturate() works just like clamp() except that it implies a clamping between [0;1]
        colorOUT.rgb = lightcolor * tex2D(baseTexture, texCoords).rgb * saturate(dot(vLightVector, vNormal));
}
The final colour calculation - colorOUT.rgb = lightcolor * tex2D(baseTexture, texCoords).rgb * saturate(dot(vLightVector, vNormal));


This is where I am falling down. I can sample the alpha of the normal map, grabbing the appropriate value, but when I try adding it to the calculation, it simply paints the texture on top. Multiplying has the obvious efffect of blanking all texture colour except for the alpha channel.

Could someone correct my maths and show me where I would need to use the alpha component to adjust the specular? If I'm not being clear, let me know and I'll try to explain further, I'm terrible at explaining things :D