正常映射仅在负轴上“工作”

问题描述 投票:1回答:1

所以基本上我在我的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和各向异性过滤):

(请记住我也有镜面贴图)

java opengl 3d glsl lwjgl
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.