Original Post
I'm trying from a while to get a simple Normal Mapping shader to work without any success...somebody have some ideas? Here it is the shader (it works with instancing for drawing lots of meshes) : With this image as texture and map.
And this is the result :
If i change the row "texCol *= Diffuse;" to "texCol += Diffuse;" the mesh appear to be mapped, but without any colour.
What i'm doing wrong? Any clue? :( [Edited by - feal87 on April 10, 2009 12:07:20 PM]
matrix View;
matrix Projection;
Texture2D g_texture;
Texture2D g_bumpmap;
float AmbientIntensity = 1;
float4 AmbientColor : AMBIENT = float4(0,0,0,1);
float3 LightDirection : Direction = float3(0,1,1);
struct VS_INPUT
{
float3 Pos : POSITION;
float3 Norm : NORMAL;
float2 Tex : TEXCOORD0;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;
float3 InstancePosition : COLOR0;
float3 InstanceRotation : COLOR1;
float3 InstanceScale : COLOR2;
float4 color : COLOR3;
};
SamplerState samLinear
{
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD2;
};
matrix RotationXMatrix(float inp)
{
float coseno = cos(inp);
float seno = sin(inp);
matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, coseno, seno, 0.0f,
0.0f, -seno, coseno, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
return result;
}
matrix RotationZMatrix(float inp)
{
float coseno = cos(inp);
float seno = sin(inp);
matrix result = { coseno, seno, 0.0f, 0.0f,
-seno, coseno, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
return result;
}
matrix RotationYMatrix(float inp)
{
float coseno = cos(inp);
float seno = sin(inp);
matrix result = { coseno, 0.0f, -seno, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
seno, 0.0f, coseno, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
return result;
}
matrix ScaleMatrix(float3 inp)
{
matrix result = { inp.x, 0.0f, 0.0f, 0.0f,
0.0f, inp.y, 0.0f, 0.0f,
0.0f, 0.0f, inp.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
return result;
}
matrix TranslationMatrix(float3 inp)
{
matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
inp.x, inp.y, inp.z, 1.0f };
return result;
}
VS_OUTPUT VS( VS_INPUT input )
{
VS_OUTPUT output = (VS_OUTPUT)0;
matrix scale = ScaleMatrix(input.InstanceScale);
matrix transl = TranslationMatrix(input.InstancePosition);
matrix rotationx = RotationXMatrix(input.InstanceRotation.x);
matrix rotationy = RotationYMatrix(input.InstanceRotation.y);
matrix rotationz = RotationZMatrix(input.InstanceRotation.z);
matrix world = mul(mul(mul(mul( scale, transl ), rotationx), rotationy),rotationz);
output.Pos = mul( float4(input.Pos,1), world);
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Tex = input.Tex;
float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(input.Tangent,world);
worldToTangentSpace[1] = mul(input.Binormal,world);
worldToTangentSpace[2] = mul(input.Norm,world);
output.Light = mul(worldToTangentSpace,LightDirection);
return output;
}
float4 PS( VS_OUTPUT input ) : SV_Target
{
float3 Normal = (2 * (g_bumpmap.Sample(samLinear,input.Tex))) - 1.0;
float3 LightDir = normalize(input.Light);
float Diffuse = saturate(dot(LightDir,Normal));
float4 texCol = g_texture.Sample(samLinear,input.Tex);
float4 Ambient = AmbientIntensity * AmbientColor;
texCol *= Diffuse;
return Ambient + texCol;
}
technique Render
{
pass P0
{
SetVertexShader( CompileShader( vs_3_0, VS() ) );
SetPixelShader( CompileShader( ps_3_0, PS() ) );
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
AlphaBlendEnable = false;
CullMode = None;
}
}
And this is the result :
If i change the row "texCol *= Diffuse;" to "texCol += Diffuse;" the mesh appear to be mapped, but without any colour.
What i'm doing wrong? Any clue? :( [Edited by - feal87 on April 10, 2009 12:07:20 PM]