View Full Version : simple light shader

06-30-2002, 06:19 AM
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?

06-30-2002, 06:31 AM
just want to say that i'm as often in cgforum as here in. its not dead.. http://www.opengl.org/discussion_boards/ubb/smile.gif still "bearing" or so.. http://www.opengl.org/discussion_boards/ubb/smile.gif

06-30-2002, 06:45 AM
Or try these forums; http://forum.opengl.nutty.org/ http://www.opengl.org/discussion_boards/ubb/smile.gif

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).]

06-30-2002, 10:44 AM
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.

06-30-2002, 11:03 AM
well, in your demo you have everything in vector4. i just dont think it is needed to send the extra float.

06-30-2002, 11:30 AM
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;

06-30-2002, 12:53 PM
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.

06-30-2002, 02:19 PM
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).]

06-30-2002, 11:48 PM
i have a few problems with my outlook express. but its D3D anyway. you think my shader is ok?