Math 正常映射实现。。。。我是不是遗漏了什么?

Math 正常映射实现。。。。我是不是遗漏了什么?,math,graphics,3d,directx,hlsl,Math,Graphics,3d,Directx,Hlsl,所以,我在世界空间中有一个光的方向,我计算每个顶点的法线。。。然而,我对我的普通map实现有点困惑。现在我正在这样做 // 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

所以,我在世界空间中有一个光的方向,我计算每个顶点的法线。。。然而,我对我的普通map实现有点困惑。现在我正在这样做

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

我做错什么了吗?根据我的说法,我正在世界空间中执行法线贴图计算,一切都应该正常工作。。。我在这里遗漏了什么吗?

TBN
是一个将向量从世界空间转换到切线空间的矩阵。因此,应该在切线空间中进行照明计算


从法线贴图获取的法线已在切线空间中(假定)。因此,您需要将灯光方向和眼睛位置转换为切线空间,然后像往常一样继续计算。

Nico,您是对的。但我还有很多问题要解决

问题1:我没有正确计算法线。我使用的是逐顶点平均,但我甚至不知道有加权平均这样的技术。这是我所有照明的2000%改进

问题2:切线和双切线计算也没有正确完成。我可能仍然会在这方面有所改进,看看我是否也能对它们进行加权平均

问题#3:我没有正确地进行照明计算,在维基百科上呆了大约两天之后,我终于做对了,现在我完全清楚地理解了它

问题#4:我只是想快点去做,而不是百分之百地理解我在做什么(再也不会犯那种错误)