• 15
• 15
• 11
• 9
• 10

# Per Pixel Lighting Problem

This topic is 4964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been using RenderMonkey to experiment with Per-Pixel Lighting shaders. I created a point light shader and it works well, but now I'm trying to make it work with more then 1 light. I calculated everything for a second light but I'm getting some funny results. I have two lights on either side of the teapot. When both lights color are white it looks fine. But if I change one lights color to red, the other side of the teapot turns a cyan color, even though the color of the second light doesnt change. two white lights one white light and a red light Here is the code for my shader:
float4x4 view_proj_matrix;

float4 Light1_Position;
float4 Light1_Attenuation;
float4 Light2_Position;
float4 Light2_Attenuation;
struct VS_OUTPUT
{
float4 Pos:       POSITION;
float2 TexCoord:   TEXCOORD0;
float3 Normal:      TEXCOORD1;
float4 LightDir:   TEXCOORD2;
float4 LightDir2:  TEXCOORD3;
};
float4 Light_CalcDir(float4 LightPosition, float4 LightAttenuation,float4 inPos)
{

float4 LightDir;
LightDir.xyz = LightPosition - inPos;
float Dist = length(LightDir.xyz);
LightDir.xyz = LightDir.xyz / Dist;

LightDir.w = clamp(0,1, 1 / ( LightAttenuation.x +
LightAttenuation.y * Dist +
LightAttenuation.z * Dist * Dist ));
return LightDir;
}

VS_OUTPUT vs_main(float4 inPos: POSITION, float3 inNormal: NORMAL,
float2 inTxr: TEXCOORD0)
{
VS_OUTPUT Out;

Out.Pos = mul(view_proj_matrix, inPos);
Out.TexCoord = inTxr;

Out.Normal = inNormal;

Out.LightDir = Light_CalcDir(Light1_Position,Light1_Attenuation, inPos);
Out.LightDir2 = Light_CalcDir(Light2_Position,Light2_Attenuation, inPos);

return Out;
}

float4 Light1_Color;
float4 Light2_Color;
float4 Light_Ambient;
float4 Light_PointDiffuse(float4 LightDir,
float3 Normal,
float4 LightColor)
{

float AngleAttn = clamp(0, 1, dot(Normal, LightDir.xyz) );

return LightColor * LightDir.w * AngleAttn;
}

float4 ps_main(float3 inNormal:TEXCOORD1,
float4 inLightDir:TEXCOORD2, float4 inLightDir2:TEXCOORD3) : COLOR
{

float4 Color1;
float4 Color2;
Color1 = Light_PointDiffuse(inLightDir,inNormal,Light1_Color);
Color2 = Light_PointDiffuse(inLightDir2,inNormal,Light2_Color);
Color1.a = 1.0f;
Color2.a = 1.0f;

return Color1+Color2+Light_Ambient;
}


Any idea what the problem is? Also, I realize this approach is not practical for a lighting system that can handle many lights. If anyone has any advice on how to setup a per-pixel lighting system capable of many lights I'd welcome the help. thanks Edited by Coder: Using source tags is a good idea. Check GDNet Forums FAQ [Edited by - Coder on August 21, 2004 10:01:23 AM]