That was exactly the direction I meant, thanks . Got it working now!
//Cg
//Cg profile arbvp1 arbfp1
void vshader(
in float2 vtx_texcoord0 : TEXCOORD0,
in float4 vtx_position : POSITION,
in float4 vtx_normal : NORMAL,
in float4 vtx_tangent1,
in float4 vtx_binormal1,
uniform float4x4 mat_modelproj,
uniform float4x4 trans_world_to_model,
in uniform float3 k_dlightvec,
out float2 l_texcoord0 : TEXCOORD0,
out float4 l_position : POSITION,
out float3 l_normal,
out float3 l_tangent,
out float3 l_binormal,
out float3 l_dlightvec
) {
l_position=mul(mat_modelproj,vtx_position);
l_texcoord0=vtx_texcoord0;
l_normal = vtx_normal.xyz;
l_tangent = vtx_tangent1.xyz;
l_binormal = -vtx_binormal1.xyz;
l_dlightvec = normalize(mul((float3x3)trans_world_to_model, -k_dlightvec));
}
void fshader(
uniform sampler2D tex_0 : TEXUNIT0,
uniform sampler2D tex_1 : TEXUNIT1,
in float2 l_texcoord0 : TEXCOORD0,
in float4 l_position : POSITION,
in float3 l_normal,
in float3 l_tangent,
in float3 l_binormal,
in float3 l_dlightvec,
in uniform float4 k_dlightcolor,
in uniform float4 k_alightcolor,
out float4 o_color : COLOR
) {
// Getting the correct normals
float3 TN = tex2D( tex_1, l_texcoord0 ) * 2.0 - 1.0 ; //Texture normal
float3 CN = l_normal; //Combined normal
CN *= TN.z;
CN += l_tangent * TN.x;
CN += l_binormal * TN.y;
CN = normalize(CN);
// Getting the correct light for this fragment
float4 lightcolor = k_alightcolor;
lightcolor += k_dlightcolor*saturate(dot(CN,l_dlightvec));
lightcolor = saturate(lightcolor);
// Final fragment color
o_color = tex2D( tex_0, l_texcoord0 );
o_color *= lightcolor;
}
Even though the shader is working I still have some questions:
- Am I correct to think that the tangent is a vector in the direction of where the x-direction of the texture is going on the model? (So the direction (u,v)->(u+.0001,v) mapped on the model) and the binormal the y-direction ((u,v)->(u,v+.0001)
- Why does the binormal have to be multiplied by -1 (i think because the y direction on a uv map is opposite to the y-direction in model space)
- why does the light vector have to be multiplied by -1
I am already very pleased with the results but it is much appreciated if someone could confirm my first two ideas!