Hello there! I am trying to implement Phong lighting. If my object is at the origin or the world (0,0,0) then it works. If I move the object around then i get wrong lighting... here is my shaders:
struct VS_INPUT
{
float4 Position : POSITION;
float4 Normal : NORMAL;
float2 Texture : TEXCOORD;
};
struct PS_INPUT
{
float4 Position : SV_POSITION;
float3 viewVec : ViewVec;
float2 Texture : TEXCOORD0;
float3 vecLight : TEXCOORD1;
float3 Normal : TEXCOORD2;
};
Vertex Shader:
cbuffer test1
{
matrix viewMatrix; /*View Matrix */
matrix projectionMatrix; /*Projection Matrix*/
matrix worldMatrix; /*World Matrix*/
float3 camPos; /*Camera position */
float3 lightPos; /*Light Position*/
};
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
// Calculate ViewProjection Matrix
matrix viewProjMatrix = mul( viewMatrix, projectionMatrix );
// Calculate WorldViewProjection
matrix worldViewProjMatrix = mul( worldMatrix, viewProjMatrix );
// Calculate Vertex position
output.Position = mul( input.Position, worldViewProjMatrix );
float3 posWorld = normalize( mul( input.Position, worldMatrix ) );
output.viewVec = normalize( camPos - posWorld );
output.Normal = normalize( mul( input.Normal, worldMatrix ) );
output.vecLight = lightPos;
output.Texture = input.Texture;
return output;
}
Pixel Shader:
float4 PS( PS_INPUT input) : SV_Target
{
// Set the output colours
float4 diffuse = float4( 1.0f, 0.0f, 0.0f,1.0f );
float4 ambient = float4( 0.5f,0.0f,0.0f,1.0f );
float3 Normal = normalize( input.Normal );
float3 Vert2Light = normalize( input.vecLight );
float3 ViewDir = normalize( input.viewVec );
float4 Diff = saturate( dot( Normal, Vert2Light ) );
float4 Reflect = normalize( 2 * Diff * Normal - Vert2Light );
float4 Specular = pow( saturate( dot( Reflect, ViewDir ) ), 256 );
return ambient + diffuse * Diff + Specular;
}
Should I do some transformation to the light position?
Thanks for your time!