Miguel
Miguel

Reputation: 924

Implementing Normal Mapping HLSL

So, ok. I'm trying to implement normal mapping in my small game engine and I just cannot get it to work.

When I do the lighting with only per-vertex normals everything is fine, but if I try to do it with normal map then everything falls apart.

I know I have the right UVs because the texture looks good, but I don't know what I'm doing wrong when it comes to Normal Map texture.

This is some of my code in my pixel shader(HLSL)....

    float3 NormalSample(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
{
    //Uncompress each component from [0,1] to [-1,1]
    float3 normalT = 2.0f*normalMapSample - 1.0f;
    // Build orthonormal basis.
    float3 N = unitNormalW;
    float3 T = normalize(tangentW - dot(tangentW, N)*N);
    float3 B = cross(N, T);

    float3x3 TBN = float3x3(T, B, N);

    // Transform from tangent space to world space.
    float3 bumpedNormalW = mul(normalT, TBN);

    return bumpedNormalW;
}

Any ideas on what I can be doing wrong?

Upvotes: 2

Views: 4547

Answers (1)

Miguel
Miguel

Reputation: 924

Ok, I fixed it. I was just doing the normal calculations in the wrong coordinate space. To make it easier I decided to do everything in model-space. It looks great. Too bad I'm not allowed to post screen shots.

Thanks again Miloszmaki....

Upvotes: 3

Related Questions