anisotropic lighting

I’d like to implement anisotropic lighting using a Cg shader. I’m using some existing code from here:

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 … ?


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 = 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);

If you get crazy flickering, that may be an indication that one of the inputs is incorrect. Are you sure the surface has a first texture assigned?

I got it working. There were two problems. First, my fragment shader was missing a crucial step, which I overlooked because in the example code I was working from that step was done with opengl calls in the app. The lookup into the texture does not just directly give you the final fragment color, which is what I was doing. The idea is actually to read the specularity out of the alpha channel and use that to modulate the diffuse component from the r,g,b channels.

The second problem is that panda was losing the alpha channel from the .tga texture. I redid it using separate color and transparency textures, and now it works a lot better. :slight_smile: