Jump to content
  • Advertisement
Sign in to follow this  
DickieChen

Is this per-pixel lighting CORRECT?!

This topic is 2852 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

Hi everyone,
I'm learing lighting and shader these days..and I wrote a per-pixel shader to render a house.I'm quite not sure whether my code is correct...

//per-pixellighting.fx
//generates diffuse and specular for each pixel
matrix mat;
vector lightPos;
vector eyePos;

texture tex;

sampler texSampler=
sampler_state
{
Texture=<tex>;
MipFilter=LINEAR;
MinFilter=LINEAR;
MagFilter=LINEAR;
};

struct VS_INPUT
{
vector vecPos:POSITION;
vector normal:NORMAL;
float2 tex:TEXCOORD;
};

struct VS_OUTPUT
{
vector vecPos:POSITION;
float2 tex:TEXCOORD;
float3 normal:TEXCOORD1;
float3 lightDir:TEXCOORD2;
float3 eyePos:TEXCOORD3;
};

VS_OUTPUT VS(VS_INPUT input)
{
VS_OUTPUT output;
output.vecPos=mul(input.vecPos,mat);
output.tex=input.tex;
output.normal=input.normal;

float lightDir;
lightDir=lightPos-input.vecPos;

output.lightDir=lightDir;

output.eyePos=normalize(eyePos-input.vecPos);

return output;
}
vector light_PointDiffuseSpecular(
float3 lightDir,
float3 eyePos,
float3 normal,
vector lightColor)
{
float diff=saturate(dot(normal,lightDir));
float spec=pow(saturate(dot(normal,eyePos)),4);

return lightColor*(diff+spec);
}

vector PS(float2 texCoord:TEXCOORD,
float3 normal:TEXCOORD1,
float3 lightDir:TEXCOORD2,
float3 eyePos:TEXCOORD3):COLOR
{
vector color={1.0f,1.0f,1.0f,1.0f};
return tex2D(texSampler,texCoord)*
light_PointDiffuseSpecular(lightDir,eyePos,normal,color);
}

technique perPixelLighting
{
pass Pass0
{
vertexShader=compile vs_1_1 VS();
pixelShader=compile ps_2_0 PS();
}
}


and here are the results..
free image hosting
free image hosting

In fact,I think that there have to be some errors in my code,some technical details such as normalize() a vector,etc.And I also think the results are not realistic..I found some pictures on the internet,which are some correct ones:
free image hosting
free image hosting
could anyone please tell me what the right per-pixel lighting shader code is?
thanks a lot...

Share this post


Link to post
Share on other sites
Advertisement
Quote:

float lightDir;
lightDir=lightPos-input.vecPos;


That should be float3 lightDir, not just float.

Also, you should normalize the light direction just as you do with the eye direction. For perfect results do it in the pixel shader and not in the vertex shader.

Share this post


Link to post
Share on other sites
Also, you may want to give your variables more meaningful names to avoid confusion.
For example, I thing this should not be an output.eyePos, but rather something like output.dirToEye

output.eyePos=normalize(eyePos-input.vecPos);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!