Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 27 February 2013 - 02:29 PM

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, 27 February 2013 - 02:43 PM.


Sponsor:

#2 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 27 February 2013 - 03:16 PM

Are you applying all your shader constants for the pixel shader?


Perception is when one imagination clashes with another

#3 Adam_42   Crossbones+   -  Reputation: 2505

Like
0Likes
Like

Posted 27 February 2013 - 03:20 PM

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



#4 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 27 February 2013 - 03:33 PM

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.



#5 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 27 February 2013 - 03:51 PM

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, 27 February 2013 - 04:02 PM.


#6 CC Ricers   Members   -  Reputation: 623

Like
0Likes
Like

Posted 28 February 2013 - 06:16 PM

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.


My development blog: Electronic Meteor

#7 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 01 March 2013 - 12:12 PM

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 :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS