我的 OpenGL 游戏引擎中有一个法线贴图,我意识到我的法线贴图着色器(以及视差贴图着色器)的光照有问题。不管光在哪里,光总是从右边来。我正在使用切线空间着色器,但我不确定我传入的内容是否正确。
这是我正在使用的法线贴图:
这是渲染:
灯光似乎来自坐标 3,0,3,但它的实际位置是 -3,0,3
有任何想法吗。
着色器:
[垂直]
attribute vec3 intan;
varying vec3 lightDir;
varying vec3 viewDir;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
vec3 vertexPos = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * intan);
vec3 b = cross(n, t) ;
mat3 tbnMatrix = mat3(t.x, b.x, n.x,
t.y, b.y, n.y,
t.z, b.z, n.z);
lightDir = (gl_LightSource[1].position.xyz - vertexPos) / 1000.;
lightDir = tbnMatrix * lightDir;
viewDir = -vertexPos;
//viewDir =
viewDir = tbnMatrix * viewDir;
}
[碎片]
uniform sampler2D tex;
uniform sampler2D nor;
varying vec3 lightDir;
varying vec3 viewDir;
const vec4 d = vec4(0.19225,0.19225,0.19225,1.0);
const vec4 a = vec4(0.50754,0.50754,0.50754,1.0);
const vec4 s = vec4(0.508273,0.508273,0.508273,1.0);
void main()
{
vec3 l = lightDir;
float atten = max(0.0, 1.0 - dot(l, l));
l = normalize(l);
vec3 n = normalize(texture2D(nor, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
vec3 v = normalize(viewDir);
vec3 h = normalize(l + v);
float nDotL = max(0.0, dot(n, l));
float nDotH = max(0.0, dot(n, h));
float power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);
vec4 ambient = a * atten;
vec4 diffuse = d * nDotL * atten;
vec4 specular = s * power * atten;
vec4 color = ambient + diffuse + specular;
gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st);
}