Domanda

So, I have a light direction in World Space, and I calculate my normals per-vertex... I am however a little confused about my normal map implementation. Right now I'm doing this.

// Normal Map
const float3 normalmap = (2.0f*gTextures1024.Sample(gLinearSam, float3(_in.Tex, gNormalMapIndex)).rgb) - 1.0f;
const float3 NormalW = _in.Norm;
const float3 TangentW = normalize(_in.TangentW.xyz - dot(_in.TangentW.xyz, _in.Norm)* _in.Norm);
const float3 BitangentW = cross(NormalW, TangentW) * _in.TangentW.w;

const float3x3 TBN = float3x3(TangentW, BitangentW, NormalW);

float3 normal = normalize(mul(TBN, normalmap));
// Lighting Calculations
//float4 normal = normalize(float4(_in.Norm, 0.0f));
float3 hvector = normalize(mul(-gDirLight.Direction.xyz, TBN) + gEyePos).xyz;
//hvector = mul(hvector.xyz, TBN);
float4 ambient  = gDirLight.Ambient * gMaterial.Ambient;
float4 diffuse  = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 texColor = float4(1.0f, 1.0f, 1.0f, 1.0f);

[branch]
if(gUseTextures)
    texColor = gTextures1024.Sample(gLinearSam, float3(_in.Tex, gDiffuseMapIndex));

// diffuse factor
float diffuseFactor = saturate(dot(normal, -gDirLight.Direction.xyz));
[branch]
if(diffuseFactor > 0.0f)
{
    diffuse = diffuseFactor * gDirLight.Diffuse * gMaterial.Diffuse;
    // Specular facttor & color
    float HdotN = saturate(dot(hvector, normal));
    specular = gDirLight.Specular * pow(HdotN, gMaterial.Specular.w);
}

// Modulate with late add
return (texColor * (ambient + diffuse)) + specular;

Am I doing something wrong here? According to me I am implementing the normal maps calculation in world space, and everything should be working just fine... Am I missing something here?

È stato utile?

Soluzione

TBN is a matrix that transforms vectors from world space to tangent space. Therefore, you should do lighting calculations in tangent space.

The normal that you acquire from the normal map is already in tangent space (assumably). So you need to transform light direction and eye position to tangent space and continue the calculation as usual.

Altri suggerimenti

Nico, you were right. But I had MANY MANY more issues that were creeping up on me.

Issue #1: I wasn't calculating my normals properly. I was using the per vertex average but wasn't even aware that there was such a technique as weighted average. This was a 2000% improvement on all my lighting.

Issue #2: Tangent and Bitangent calculation were not being done correctly either. I might still improve on that area to see if I can also do a weighted average of them.

Issue #3: I wasn't doing my lighting calculations correctly and after being on Wikipedia for about 2 days, I finally did it right, and actually understand it now with complete clarity.

Issue #4: I just wanted to hurry up and do it without understanding 100% what I was doing(never making that mistake AGAIN).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top