Question

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:

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.

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!

Was it helpful?

Solution 2

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top