# HLSL Phong Lightning (/w normal map)

This topic is 2033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am starting to learn some HLSL Shading with DirectX10 and I have tried to use a normal map to calculate my phong lightning.
First off here is an example of how far I have come:

http://i.stack.imgur.com/IFAo4.jpg

I am not quite sure if this is what im looking to accomplish with this normal map:
http://i.stack.imgur.com/moQvf.jpg

I dont know..shouldn't this look more 3Dish? Maybe I have just an false understanding of the usage of a normal map, but in my mindset a normal map is used to make a model more detailed by adding shadows based on the normal map, so its looks more 3D ish.
Here is a screenshot without using the normal map:

[url="http://i.stack.imgur.com/eJ8OZ.jpg"]i.stack.imgur.com/eJ8OZ.jpg[/url]

And here is my shading code:

[CODE]
T3dVertexPSIn MeshVS(T3dVertexVSIn Input) {
T3dVertexPSIn output = (T3dVertexPSIn) 0;
float4 tempVar;

output.Pos = mul(float4(Input.Pos.xyz, 1).xyzw, g_WorldViewProjection);

output.Tex = Input.Tex;

tempVar = mul(float4(Input.Pos.xyz, 1).xyzw, g_WorldView);
output.PosView = tempVar.xyz;

tempVar = mul(float4(Input.Nor.xyz, 0).xyzw, g_WorldViewNormals);
output.NorView = tempVar.xyz;
output.NorView = normalize(output.NorView);

tempVar = mul(float4(Input.Tan.xyz, 0).xyzw, g_WorldViewNormals);
output.TanView = tempVar.xyz;
output.TanView = normalize(output.TanView);

return output;
}[/CODE]

[CODE]
float4 MeshPS(T3dVertexPSIn Input) : SV_Target0 {
float4 output = (float4)0; //output color

float3 N = normalize(Input.NorView);

float3 T = Input.TanView;

T = normalize(T - N * dot(N,T));
float3 B = cross(T,N);

column_major float3x3 mTBN= {T, B, N};
float4 matDiffuse = g_Diffuse.Sample(samAnisotropic, Input.Tex);
float4 matSpecular = g_Specular.Sample(samAnisotropic, Input.Tex);
float4 matGlow = g_Glow.Sample(samAnisotropic, Input.Tex);
float4 colLight = float4(1,1,1,1);
float4 colLightAmbient = float4(1,1,1,1);
float3 n = mul(g_Normal.Sample(samAnisotropic, Input.Tex).rgb * 2.0 - 1.0, mTBN);
//float3 n = mul(float3x3(T,B,N), nT);
//float3 n = normalize(Input.NorView);
float4 I = g_LightDirView;
float3 r = reflect(-I.xyz, n);
float3 v = normalize(-Input.PosView);

float cd = 0.5f, cs = 0.3f, ca = 0.1f, cg = 0.3f;

output = cd*matDiffuse*saturate(dot(n,I.xyz))*colLight
+cs*matSpecular*pow(saturate(dot(r.xyz,v)),10)*colLight
+ca*matDiffuse*colLightAmbient
+cg*matGlow;

return output;
}[/CODE]

I also have the feeling that the lightning is changing direction when im using the normal map.but I am not sure.
Maybe someone can explain this matter to me a little bit.
Thanks in advance for any help!

##### Share on other sites
I've accomplished to implement the normal mapping in my engine [font="arial, sans-serif"][size="2"]right this week, i suggest you to read this: [/size][/font]
[url="http://www.rastertek.com/dx10tut20.html"]http://www.rastertek.com/dx10tut20.html[/url]
and this:
[url="http://www.bencloward.com/tutorials_normal_maps1.shtml"]http://www.bencloward.com/tutorials_normal_maps1.shtml[/url]