I am porting my GLSL Directional Light Shader from OpenGL to DirectX11.
My LightDirection is (1,0,0) (from left to right in the pictures)
The Shader in GLSL looks like this:
#version 330 core
uniform sampler2D DiffuseMap;
uniform sampler2D NormalMap;
uniform sampler2D PositionMap;
uniform sampler2D DepthMap;
uniform vec3 LightDirection;
uniform vec3 LightColor;
uniform vec2 Resolution;
vec2 CalcTexCoord()
{
return gl_FragCoord.xy / Resolution;
}
void main()
{
vec4 norm = texture2D( NormalMap, CalcTexCoord());
float Factor = dot(norm.xyz,-LightDirection);
vec4 lights = vec4(LightColor*Factor,1);
gl_FragColor = lights;
}
This results in this:
The bricks are colored blue from a light which comes from the left side.
I ported the code to HLSL:
cbuffer MatrixBuffer
{
matrix Scale;
matrix Rotation;
matrix Translation;
matrix View;
matrix Projection;
float3 LightDirection;
float3 LightColor;
float2 Resolution;
};
tbuffer textureBuffer
{
Texture2D DiffuseMap;
Texture2D NormalMap;
Texture2D PositionMap;
Texture2D DepthMap;
};
SamplerState TextureSampler;
struct VertexShaderInput
{
float3 Position : POSITION0;
float2 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Normal : NORMAL;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float2 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Normal : NORMAL;
};
struct PixelShaderOutput
{
float4 color: SV_TARGET0;
};
float2 CalcTexCoord(float4 Position)
{
return Position.xy / Resolution;
}
VertexShaderOutput VS_MAIN(VertexShaderInput input)
{
VertexShaderOutput Output = (VertexShaderOutput)0;
float4 pos = float4(input.Position.xyz, 1);
float4x4 Model = mul(mul(Scale, Rotation), Translation);
float4x4 MVP = mul(mul(Model, View), Projection);
Output.Position = mul(pos, MVP);
Output.UV = input.UV;
Output.Color = input.Color;
Output.Normal = input.Normal;
return Output;
}
PixelShaderOutput PS_MAIN(VertexShaderOutput input)
{
PixelShaderOutput output;
float4 norm = NormalMap.Sample(TextureSampler, input.UV);
float Factor = dot(norm.xyz,-LightDirection);
float4 lights = float4(LightColor*Factor, 1);
output.color = lights;
return output;
}
technique10 Main
{
pass p0
{
SetVertexShader(CompileShader(vs_5_0, VS_MAIN()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_5_0, PS_MAIN()));
}
};
Which results in this:
I came to the conclusion, that the error is because I can't negate my normals.
If I draw my normal maps, they look both exactly the same
But drawing the normals negated results in this:
Now how do I fix the problem, so that my bricks are drawn correctly?