Skip to main content
gamedev.net
🔒 Locked

Phong Shader

Started by Magos Sep 16, 2005 at 9:26 AM 4 replies 500+ views
Original Post
Magos
Magos
This is my phong shader:

PS_OUT main(PS_IN In)
{
  PS_OUT Out;

  float3 Light = normalize(In.Light);
  float3 Normal = normalize(In.Normal);
  float3 View = normalize(In.View);
  float DiffuseFactor = saturate(dot(Light, Normal));
  float3 Reflect = normalize(2 * DiffuseFactor * Normal - Light);
  float4 SpecularFactor = pow(saturate(dot(Reflect, View)), SpecularPower);
  float4 Color = AmbientColor + (DiffuseColor * DiffuseFactor) + (SpecularColor * SpecularFactor);
  Color.a = 1.0f;

  Out.Color = Color * tex2D(Texture, In.TexturePosition.xy);

  return Out;
}

Though it's generally ok, it doesn't seem exactly correct. The specular lighting feels so faint... somehow. I've found tons of phong lighting docs on google, but none that takes textures into account. Does the above code look correct to you?
----------------------------------------MagosX.com
centipede
centipede
You are calculating reflection vector incorrectly. It should be calculated like this:

float3 Reflect = 2 * dot(Normal, Light) * Normal - Light;

Alternatively, you can use Blinn specular. If you choose that, you need half-angle vector:

float3 halfAngle = (Light + View) / 2;

Then you calculate specular factor:

float4 SpecularFactor = pow(max(dot(Normal, halfAngle), 0), SpecularPower);


Note that this is written from top of my head, so there might be some errors. I hope this helps anyway. [smile]
Magos
Magos
Well, isn't that what I'm doing? Check out my DiffuseFactor variable. Perhaps I'm doing some extra (unneccessary?) saturate/normalize operation, but otherwise it's exactly like you described...
----------------------------------------MagosX.com
centipede
centipede
Oh yes, sorry, I didn't notice that. Well, it happens. [grin]

Now that I look again, I think I can see your problem. The problem is, that you have added specular to Color and then multiplied Color with texture color. Specular should be added after all other lighting, so you should change these lines

float4 Color = AmbientColor + (DiffuseColor * DiffuseFactor) + (SpecularColor * SpecularFactor);Out.Color = Color * tex2D(Texture, In.TexturePosition.xy);


to this

float4 Color = AmbientColor + (DiffuseColor * DiffuseFactor);Out.Color = Color * tex2D(Texture, In.TexturePosition.xy) + (SpecularColor * SpecularFactor);


Try that and see if it helps. [smile]
Magos
Magos
Ah, yes, now it looks so much better. Thanks a million!
----------------------------------------MagosX.com
Magos
Magos
I seem to have encountered another problem. When my mesh is animated the (specular) lighting stays in the same spot even when the mesh rotates. My guess is that the normals that I use are in the mesh's original position. Would it work to simply transform the camera position/targetwith the inverse world-view-projection matrix and use these new coordinates in the calculation?
----------------------------------------MagosX.com

Topic Locked

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

Sign in to reply to this topic.