Skip to main content
GameDev.net gamedev.net
🔒 Locked

Phong Lighting Artifact

Started by dave Jul 3, 2009 at 7:11 PM 10 replies 1.2k views
Original Post
dave
dave
So i've implemented Phong lighting a few times ages ago, quite easy. However i seem to be getting this strange artifact with the specular highlight now that im doing it with deferred rendering. I am pretty certain the fact im doing it deferred makes no difference. Screenie: Object Pass:

VS_OUTPUT vs_main( VS_INPUT In )
{
   VS_OUTPUT Out;

   Out.Pos           = mul(float4(In.Pos,1.0), matWorldViewProjection);
   Out.PosInWorld    = mul(float4(In.Pos,1.0), matWorld);
   Out.NormalInWorld = mul(In.Normal, matWorld);
   Out.Tex           = In.Tex;
   return Out;
}

PS_OUTPUT ps_main( PS_INPUT In )
{
   PS_OUTPUT Out;
   
   Out.PosInWorld    = float4(In.PosInWorld,1.0);
   Out.NormalInWorld = float4(In.NormalInWorld,1.0);
   Out.Albedo		 = float4(0,0,0,0);//tex2D(TextureMapSampler, In.Tex);
   
   return Out;
}
Shading Pass

PS_OUTPUT ps_main( PS_INPUT In )
{
	PS_OUTPUT Out;

	float4 Pos    = tex2D(PosMapSampler,In.TexCoord);   

	if (Pos.w==0.0) // If nothing is drawn at this pixel, just output clear color
	{
		Out.Color = ClearColor;
	} // End if
	else
	{
		if ( LightType == 2 )	//	Point
		{
			float3	Normal	= normalize( tex2D( NormalMapSampler, In.TexCoord ) );
			float3	ToLight = normalize( LightPosition-Pos );
			float3	Reflect = normalize( 2.0f * Normal * dot( Normal, ToLight ) - ToLight );
			float3	View	= normalize( CameraPosition - Pos );
		
			Out.Color = Phong(	LightAmbientColour,
								LightDiffuseColour,
								ToLight,
								Normal,
								LightSpecularColour,
								Reflect,
								View );
		}
	}
	return Out;
}
Phong function:
float4	Phong(	float4	AmbientColour,			//	Ia
				float4	DiffuseColour,			//	Id
				float3	PointToLightVector,		//	L
				float3	SurfaceNormal,			//	N
				float4	SpecularColour,			//	Is
				float3	ReflectVector,			//	R
				float3	PointToViewer )			//	V
{
	float4 Ambient	= AmbientColour;
	float4 Diffuse	= DiffuseColour * dot( SurfaceNormal, PointToLightVector );
	float4 Specular = SpecularColour * pow(saturate(dot( ReflectVector, PointToViewer )),4);
	
	return Ambient + Diffuse + Specular;
}
This specular isn't correct is it? If it isn't, can you see what is wrong?
dave
dave
Bump.
_Camus_
_Camus_
Maybe your source light are too close to the object, try to move some units Up.
rozz666
rozz666
I think you should clamp the dot products, since they can be negative.
float4 Diffuse	= DiffuseColour * max(0, dot( SurfaceNormal, PointToLightVector ));float4 Specular = SpecularColour * pow(saturate(max(0,dot( ReflectVector, PointToViewer ))),4);
programci_84
programci_84
Hi,

There's no artifact in the pic you posted. Also, your code seems correct. But, your pic says that "point light is positioned at (0, 0, 0)".

I think that you must check your light position.

Hope this helps.
Rohat.
There's no "hard", and "the impossible" takes just a little time.
dave
dave
Hmm yeah i wasn't sure there was a problem. Thanks for the ideas, i'll take another look.
dave
dave
Hmm just a thought, but the light cant be at 0,0,0 because otherwise the diffuse of the surroundings wouldn't be so well lit.
_Camus_
_Camus_
Try to debug moving the light position with the keyboard displaying the position with a font.
dave
dave
The light position is definately correct.
leet bix
leet bix
Hey, what type of light is this? point?
dave
dave
Yep.
RDragon1
RDragon1
What does the normal buffer look like? How far away from the surface is the point light? What does it look like when you move the light away from the surface a bit? What does it look like if you move the light so it's not directly over the center of the surface?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.