So im trying to learn HLSL. This is the result
What happend is that the light part on the box is also rotating together with the box. The light should be in fixed position.
This is my shader file
float4x4 world;
float4x4 view;
float4x4 projection;
float4 lightColor;
float3 lightDirection;
float4 ambientColor;
struct VertexShaderOutput
{
float4 Position : POSITION;
float4 Color : COLOR0;
};
struct PixelShaderInput
{
float4 Color: COLOR0;
};
VertexShaderOutput DiffuseLighting(
float3 position : POSITION,
float3 normal : NORMAL)
{
VertexShaderOutput output;
//generate the world-view-proj matrix
float4x4 wvp = mul(mul(world, view), projection);
//transform the input position to the output
output.Position = mul(view, world);
float3 worldNormal = mul(normal, world);
float diffuseIntensity = saturate(dot(-lightDirection, worldNormal));
float4 diffuseColor = lightColor * diffuseIntensity;
output.Color = diffuseColor + ambientColor;
diffuseColor.a = 1.0;
//return the output structure
return output;
}
float4 SimplePixelShader(PixelShaderInput input) : COLOR
{
return input.Color;
}
technique VertexLighting
{
pass P0
{
//set the VertexShader state to the vertex shader function
VertexShader = compile vs_2_0 DiffuseLighting();
//set the PixelShader state to the pixel shader function
PixelShader = compile ps_2_0 SimplePixelShader();
}
}
and this is just what I did on Draw method
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
part.Effect = effect;
effect.Parameters["world"].SetValue(world * mesh.ParentBone.Transform);
effect.Parameters["view"].SetValue(view);
effect.Parameters["projection"].SetValue(projection);
effect.Parameters["lightDirection"].SetValue(new Vector3(1, 0, 0));
effect.Parameters["lightColor"].SetValue(Color.Green.ToVector4());
effect.Parameters["ambientColor"].SetValue(Color.DarkSlateGray.ToVector4());
}
for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
{
effect.CurrentTechnique.Passes[i].Apply();
mesh.Draw();
}
}
Anyone ideas whats wrong?