I’d like to implement anisotropic lighting using a Cg shader. I’m using some existing code from here:
codesampler.com/oglsrc/oglsrc_11.htm
but I seem to be missing something somewhere. I get a sphere which is half black and half blue, with crazy flickering going on even when there’s no movement.
Here’s my shader code - anything jump out that’s obviously wrong here … ?
//Cg
float3 transformVec( const float4x4 matrix, const float3 vec )
{
return( float3( dot(vec, matrix._11_12_13),
dot(vec, matrix._21_22_23),
dot(vec, matrix._31_32_33) ) );
}
void vshader(
uniform float4x4 mat_modelproj,
uniform float4x4 mat_modelview,
uniform float4x4 itp_modelview,
uniform float4x4 trans_model_to_world,
uniform float4 wspos_light,
uniform float4 wspos_camera,
in float4 vtx_position : POSITION,
in float4 vtx_normal : NORMAL,
in float4 vtx_color : COLOR,
in float2 vtx_texcoord0: TEXCOORD0,
out float2 l_texcoord0 : TEXCOORD0,
out float4 l_position : POSITION)
{
l_position = mul(mat_modelproj, vtx_position);
// worldNormal : normal, in worldspace coordinates
float4 worldNormal;
worldNormal.xyz=transformVec(itp_modelview,vtx_normal.xyz);
worldNormal = normalize(worldNormal);
float4 worldPosition = mul(trans_model_to_world,vtx_position);
float4 vertToEye = normalize (wspos_camera-worldPosition);
float4 lightVec = normalize (wspos_light-worldPosition);
float4 halfAngle = normalize (vertToEye + lightVec);
l_texcoord0 = vtx_texcoord0;
l_texcoord0.x=max (dot(lightVec,worldNormal),0.0);
l_texcoord0.y=max (dot(halfAngle,worldNormal),0.5);
}
void fshader(
in float2 l_texcoord0 : texcoord0,
in float4 l_position : POSITION,
uniform sampler2D tex_0,
out float4 o_color : COLOR)
{
o_color = tex2D (tex_0, l_texcoord0);
}