负轴仅法线贴图 "working"

Normalmapping only "working" in negative axis

所以基本上我在我的 3d 程序中实现了法线贴图,但我遇到了一个问题,法线贴图似乎只显示在负轴上,当我靠近它时它会偏移并且在位置不对。

我试过很多东西,包括使用不同的方法,乘以光线方向和相机位置而不是法线贴图,使用转置和逆 TBN 矩阵,检查纹理加载过程并关闭所有可能会干扰和更多的过程。

这是我在顶点着色器中的主要方法:

gl_Position = ProjectionMatrix * EyeMatrix * ModelMatrix * vec4(Position, 1.0);
texCoords = TexCoords;
position = (ModelMatrix * vec4(Position, 1.0)).xyz;

vec3 n = normalize((ModelMatrix * vec4(Normal, 0.0))).xyz;
vec3 t = normalize((ModelMatrix * vec4(Tangent, 0.0))).xyz;

t = normalize(t - dot(t, n) * n);

vec3 b = cross(t, n);

tbnMatrix = mat3(t, b, n);

以下是我计算光照的方法:

vec3 calculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, float energyConservation)
{
vec3 lightDir = normalize(light.position - fragPos);

float diff = max(dot(normal, normalize(lightDir + viewDir)), 0.0);

float spec = energyConservation * pow(max(dot(normal, normalize(lightDir + viewDir)), material.specularIntensity), material.specularDampening);

float distance    = length(light.position - fragPos);
float attenuation = 1.0 / (light.attenuation.constant + light.attenuation.linear * distance + 
             light.attenuation.quadratic * pow(distance, 2.2));    

vec3 ambient  = light.baseLight.ambient * vec3(texture(material.texture_diffuse, texCoords));
vec3 diffuse  = (light.baseLight.diffuse * light.baseLight.intensity) * diff * vec3(texture(material.texture_diffuse, texCoords));
vec3 specular = (light.baseLight.specular * light.baseLight.intensity) * spec * vec3(texture(material.texture_gloss, texCoords));

ambient  *= attenuation;
diffuse  *= attenuation;
specular *= attenuation;

return (ambient + diffuse + specular);
}

这是我的法线贴图的代码:

vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture(material.texture_normal, texCoords).xyz - 1));

这是切线的样子:

下面是应用 TBN 矩阵后的切线:

这里是加载的T组件(没有任何版本):
这是 B 组件(顶点着色器中的组件):
这是加载的 N 组件(没有任何版本):

这是我用法线贴图渲染场景时得到的结果:


这是没有法线贴图(启用了 mipmapping 和各向异性过滤)的样子:

(请记住我也有高光贴图)

几周后我发现了这个问题,这是一个非常简单的错误。在纹理加载过程中,我使用 GL_SRGB 而不是正常的 GL_RGB,并且 SRGB 是纹理的伽马校正版本而不是实际的纯图像。所以我必须选择一个选项来禁用和启用构造函数中的 SRGB 加载,以用于法线、光泽和置换贴图,基本上任何未在屏幕上显示的内容。