1

EDIT: IT seems like the only problem left now is that the light comes from the opposite direction if i use the calculation with a normal map. If i only use:

n = normalize(Input.NorView);

it seems to be fine.

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:

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

I am not quite sure if this is what im looking to accomplish with this normal map: https://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.

And here is my shading code: Vertex Shader:

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;
}

Pixel Shader:

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;
}

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!

puelo
  • 5,464
  • 2
  • 34
  • 62
  • because i could only add 2 hyperlinks here is a screenshot without the usage of a normal map: http://i.stack.imgur.com/eJ8OZ.jpg – puelo Jun 23 '12 at 11:37

2 Answers2

0

A Normal map does not create shadowing. It, simply, allows you to re-evaluate the lighting per texel.

Goz
  • 61,365
  • 24
  • 124
  • 204
  • alright. Thanks for that. now i have rendered the whole thing without the diffuse , ambient or glow map, only the normal map applied to see if the lightning is correct. It seems like its lighting it according to the normal map, but the light seems to come from exactly the opposite direction as it was before. I already checked the matrices..they seem to be correct. – puelo Jun 23 '12 at 16:05
  • @Puelo: TBH if its exactly the wrong direction have you considered you are missing a negate? – Goz Jun 23 '12 at 16:21
  • I already fixed it myself. seems like i was setting the z value of the input normal wrong. Thanks anyways ;). Is it normal that there is no major noticeable difference between those two methods? i mean between using either just the normalized Input.NorView or the whole calculation with B N T? – puelo Jun 23 '12 at 16:33
  • i mean if i only render the lightning i see a major difference, but if i then render with diffuse, ambient and specular nothing big has changed to the image before. – puelo Jun 23 '12 at 16:36
0

Seems like i was calculating the normal incorrect and some textures also got the wrong normal.

puelo
  • 5,464
  • 2
  • 34
  • 62