Sign in to follow this  
cozzie

"Convert" multiple lighting shader to 'per vertex lighting'

Recommended Posts

cozzie    5029

Hi,

I've managed to create working effect/ shader which has a flexible number of directional and point lights, based on the given scene and available shader model.

 

Next thing I want to do is go to per pixel lighting instead of per vertex.

At the moment I have the normal of the vertex available in the pixel shader, but I don't use it..

 

Can someone give me some pointers on how to go to work to achieve per pixel lighting in my effect?

 

The code:

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
	Out.Pos = mul(worldPosition, ViewProj);

	float4 normal = mul(input.Normal, WorldInvTransp);

	Out.Normal = normal;
	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition;

//	DIRECTIONAL LIGHT
	float dirIntensity[MaxDirectionalLights];
	float dirTotal = 0.0f;

	for(int i=0;i<MaxDirectionalLights;i++)
	{	
		dirIntensity[i] 	= dot(normal, DirLightDir[i]);
		dirTotal 		+= saturate(DirLightCol[i] * DirLightInt[i] * dirIntensity[i]);
	}

	Out.Color = dirTotal;
	return Out;
}

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);
	float4 amb = AmbientColor * AmbientIntensity * MatAmb;
	float4 diff = input.Color * MatDiff;

	float distt[MaxPointLights];
	float att[MaxPointLights];

	float4 att_total = 0.0f;
	float4 attcolored;

	for(int i=0;i<MaxPointLights;i++)
	{
		distt[i] 	= distance(PointLightPos[i], input.wPos);
		att[i]	= 1 - saturate((distt[i] - PointLightFPRange[i]) / PointLightRange[i]);
		att[i]	= (pow(att[i], 2)) * PointLightInt[i];
		
		attcolored	= att[i] * PointLightCol[i];
		att_total	+= attcolored;
	}

	return saturate((diff + amb + att_total) * textureColor);
}

I've read several tutorials on per pixel lighting, but up till now can't manage to implement it in my existing effect/shader.

Here are two screenshots of the current result, which by the way looks ok even on low poly (the ground):

 

pointlight.jpg

 

pointlight-wireframe.jpg

Edited by cozzie

Share this post


Link to post
Share on other sites
Adam_42    3629

If you look closely at that image it appears that the right hand end of that wall is facing away from the light and should therefore not be lit by it.

 

The standard way to handle that is to multiply the light intensity by N dot L: saturate(dot(normalize(light_position - pixel_position), normalize(normal)))

 

If you want a specular highlight too take a look at: http://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model

Share this post


Link to post
Share on other sites
cozzie    5029

I checked if I pass all the shader constants, which is the case

(ambientcolor, ambientintensity, matamb, matdiff).

 

The right wall is lit up by another lightsource, in this case directional.

I've disabled it, here's the result of only the point light (with a little ambient light to see the effect):

 

pointlight2.jpg

 

pointlight2-wireframe.jpg

 

@Adam; thanks, I'll play around with multiplying light intensity by N dot L and see what it does.

Share this post


Link to post
Share on other sites
cozzie    5029

It works and shows a clear difference, clearly visible on the corners where normals are/ aren't applied.

Not sure though which looks better or more realistic.

 

pointwithoutnormal.jpg

 

pointwithnormal.jpg

 

First impression is that with normals is more realistic since the corners of the building 'break' the light.

What do you think?

 

I actually only changed the pixel shader for the point light calculations, not the directional lighting calculation in the vertex shader:

 

	float4 perpixel;

	for(int i=0;i<MaxPointLights;i++)
	{
		distt[i] 	= distance(PointLightPos[i], input.wPos);
		att[i]	= 1 - saturate((distt[i] - PointLightFPRange[i]) / PointLightRange[i]);
		att[i]	= (pow(att[i], 2)) * PointLightInt[i];
		
		attcolored	= att[i] * PointLightCol[i];

		perpixel	= saturate(dot(normalize(PointLightPos[i] - input.wPos), normalize(input.Normal)));
		att_total	+= (attcolored * perpixel);
	}

Edited by cozzie

Share this post


Link to post
Share on other sites
CC Ricers    1491

That appears correct. The lighted area of the point light is barely grazing the side wall of the building, and with the light attenuation together with the wall normals, the wall is receiving very little light. The normals reduce it a lot here because its resulting dot product is very low.

Share this post


Link to post
Share on other sites
cozzie    5029

thanks, on the 'non-fact' side it also just looks better.

 

by accident I did notice though, when rendering wireframes, I have some culling improvement possibilities :)

Share this post


Link to post
Share on other sites

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