I have just started with cg and am trying to write a simple vertex lighting code for four light sources.
It should apply four lights at one pass and lights can be point lights or environment lights (only direction).
However, I can only see my model drawn as black.
Here’s my cg code:
struct vpin
{
float4 position : POSITION;
float4 normal : NORMAL;
float4 color : COLOR0;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};struct vpout
{
float4 position : POSITION;
float4 color : COLOR0;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};float Illuminate(float4 pos, float radius, float3 ipos, float3 n);
vpout main(vpin IN, uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewIT, uniform float4 LPos[4],
uniform float LRadius[4], uniform float3 LColor[4])
{vpout OUT;
float3 normal;
int i;/* get proper position and normal vectors */
OUT.position = mul(ModelViewProj, IN.position);
normal.xyz = normalize( mul(ModelViewIT, IN.normal).xyz );/* define diffuse color by light */
OUT.color.rgb=0.0f;
OUT.color.a = IN.color.a;
for(i=0; i<4; i++)
OUT.color.rgb += Illuminate(LPos[i], LRadius[i], OUT.position.xyz, normal)*LColor[i].rgb;OUT.color = clamp(OUT.color, 0.0f, 1.0f);
/* pass texture coordinates */
OUT.texcoord0 = IN.texcoord0;
OUT.texcoord1 = IN.texcoord1;return OUT;
}float Illuminate(float4 pos, float radius, float3 ipos, float3 n)
{float3 light;
float diffuse;
float ret;/* get light vector /
/ ---------------- /
/ 1.0 = pointlight /
/ 0.0 = env. light /
light.xyz = normalize(pos.xyz-(ipos.xyzpos.w));/* get diffuse component */
diffuse = dot(light, n);ret = (diffuse/(light.xlight.x+light.ylight.y+light.z*light.z))*radius;
if(pos.w<0.5f)
ret = diffuse;return ret;
}
I know it sucks but I would be grateful if someone could point me out what’s wrong.