simple light shader

hi. i know this question belongs to the cg forums, but they are dead, no one looks at them, at least yet. so i post it here -
i wrote a simple diffuse lighting shader, but it doesnt work. here is the important part that doesnt work -

float4 T = float4(In.Pos.x, In.Pos.y, In.Pos.z, 1);
float4 N = float4(In.Normal.x, In.Normal.y, In.Normal.z, 1);
float4 l = float4(LightPos.x, LightPos.y, LightPos.z, 0);

N = normalize(mul(matWVIT, N).xyzz);
float4 V = mul(matWV, T);
float4 L = mul(matWV, l);
L = normalize(L-V);

float Diffuse = dot(N, L);

i guess the problem is here, but i dont know what it is. i pass the lightpos in world space and transform it to eye space along with the vertex pos to compute eye-space light direction. isnt it right?

just want to say that i’m as often in cgforum as here in. its not dead… still “bearing” or so…

Or try these forums;

sorry for the blatant plug.

To me it seems
normalize(mul(matWVIT, N).xyzz);

Looks dodge. Why dont you just use Float3’s? then you wouldn’t need that nasty mask.

I have a diffuse+specular + cubemapped Cg demo. Have a look at;


[This message has been edited by Nutty (edited 06-30-2002).]

[This message has been edited by Nutty (edited 06-30-2002).]

well, i must admit im using direct3d now, and im not sure the D3DMATRIX can be used for 3x3 matrices instead of 4x4 matrices.
thnx anyway, ill try what you said.

well, in your demo you have everything in vector4. i just dont think it is needed to send the extra float.

i updated the shader, it now looks like this, and i dont think it works.

v2f main(appdata In,
uniform float4x4 matWVP,
uniform float3x4 matWV,
uniform float3x3 matWVIT,
uniform float3 LightPos)

v2f Out;

//LightPos = float3(23,15,23);
float4 T = float4(In.Pos.x, In.Pos.y, In.Pos.z, 1);
float4 L = float4(LightPos.x, LightPos.y, LightPos.z,1);

float3 V = mul(matWV, T);
float3 l = mul(matWV, L);
float3 N = normalize(mul(matWVIT, In.Normal));

float3 light = normalize(l-V);

float Diffuse = dot(N,light);
float4 DColor = float4(0.4, 0.3, 0.8, 1);

Out.HPOS = mul(matWVP, T);
Out.COL0 = Diffuse*DColor;
return Out;


Nutty, I didn’t realise you worked for Climax…that’s in Brighton, isn’t it?
Did you do any work on MotoGP? Looks good, dot3 on the gravel etc.

Yes I work for Climax, but in another studio. Climax Solent. Just along the coast from Brighton.

Does look good.

okapota, send me a test app, and I’ll see what I can do.


[This message has been edited by Nutty (edited 06-30-2002).]

i have a few problems with my outlook express. but its D3D anyway. you think my shader is ok?