【问题标题】:Incorrect Lighting In Normal Maps法线贴图中不正确的光照
【发布时间】:2012-02-19 01:03:39
【问题描述】:

我的 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);
}

【问题讨论】:

  • 酷地图+1。但有什么想法吗?为什么不直接破解 vertexPos.x = -vertexPos.x
  • 我试过了,但我会遇到另一边的灯出现问题。

标签: opengl mapping glsl normals


【解决方案1】:
vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * intan);
vec3 b = cross(n, t) ;

我想intan 指定了法线贴图“右”指向的方向。那么这个叉积是错误的。如果 n 指向曲面外并且 intan 指向右侧,则 b(inormal) 指向下方。

请注意,如果先转换法线和切线,然后对结果进行叉积,或者先进行叉积并转换结果,则没有区别。

lightDir = (gl_LightSource[1].position.xyz - vertexPos) / 1000.;

你为什么不把它标准化呢?除以 1000 没有意义。

【讨论】:

  • 除以 1000 是我计算衰减的方式。如果您查看我的片段着色器,我将向量长度的平方减去 1 并得到介于 0.0 和 0.0 之间的最大值。 1000表示我的光可以达到1000光单位。我无法再深入解释了。我只知道它有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2013-08-09
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多