Sign in to follow this  

Per Pixel Lighting Problem

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

If you intended to correct an error in the post then please contact us.

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]

Share this post


Link to post
Share on other sites
If you are not bound by instruction and texture read count (that is, using >= PS2.0), you could use 1-d floating point textures as light parameter arrays - storing, for example, one light's position per texel. Another same sized texture could store the light colors, and yet another one could store other lighting parameters (for example, spotlight cone angles or attenuation factors). In the shader, you then access the light parameters in a loop, accumulating their results and finally returning the accumulated color.

As for your current code, a hint: White - Red = Cyan [smile]

-Nik

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this