3

我正在实现法线映射,通过 ASSIMP 库计算切线向量。

法线映射似乎在模型矩阵接近单位矩阵的对象上工作得很好。只要我开始平移和缩放,我的灯光似乎就关闭了。正如您在图片中看到的,法线贴图在容器立方体上完美运行,但在大地板上照明失败(镜面反射光的方向应该朝向玩家,而不是朝向容器)。

光照在法线贴图上不起作用

只要我开始更改模型矩阵(通过翻译/缩放)。我发布了所有相关的代码,希望你们能以某种方式看到我丢失的东西,因为我已经盯着我的代码好几天了。

顶点着色器

#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 tangent;
layout(location = 3) in vec3 color;
layout(location = 4) in vec2 texCoord;

// fragment pass through
out vec3 Position;
out vec3 Normal;
out vec3 Tangent;
out vec3 Color;
out vec2 TexCoord;

out vec3 TangentSurface2Light;
out vec3 TangentSurface2View;

uniform vec3 lightPos;

// vertex transformation
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    mat3 normalMatrix = transpose(mat3(inverse(view * model))); 
    Position = vec3((view * model) * vec4(position, 1.0)); 
    Normal = normalMatrix * normal;
    Tangent = tangent;
    Color = color;
    TexCoord = texCoord;

    gl_Position = projection * view * model * vec4(position, 1.0);

    vec3 light = vec3(view * vec4(lightPos, 1.0));
    vec3 n = normalize(normalMatrix * normal);
    vec3 t = normalize(normalMatrix * tangent);
    vec3 b = cross(n, t);
    mat3 mat = mat3(t.x, b.x ,n.x, t.y, b.y ,n.y, t.z, b.z ,n.z);
    vec3 vector = normalize(light - Position);
    TangentSurface2Light = mat * vector;
    vector = normalize(-Position);
    TangentSurface2View = mat * vector;
}

片段着色器

#version 330

in vec3 Position;
in vec3 Normal;
in vec3 Tangent;
in vec3 Color;
in vec2 TexCoord;

in vec3 TangentSurface2Light;
in vec3 TangentSurface2View;

out vec4 outColor;

uniform vec3 lightPos;
uniform mat4 view;
uniform sampler2D texture0;
uniform sampler2D texture_normal; // normal

uniform float repeatFactor = 1;

void main()
{   
    vec4 texColor = texture(texture0, TexCoord * repeatFactor);
    vec3 light = vec3(view * vec4(lightPos, 1.0));
    float dist = length(light - Position);
    float att = 1.0 / (1.0 + 0.01 * dist + 0.001 * dist * dist);
    // Ambient
    vec4 ambient = vec4(0.2);
    // Diffuse
    vec3 surface2light = normalize(TangentSurface2Light);
    vec3 norm = normalize(texture(texture_normal, TexCoord * repeatFactor).xyz * 2.0 - 1.0); 
    float contribution = max(dot(norm, surface2light), 0.0);
    vec4 diffuse = contribution * vec4(0.8);
    // Specular
    vec3 surf2view = normalize(TangentSurface2View);
    vec3 reflection = reflect(-surface2light, norm); // reflection vector
    float specContribution = pow(max(dot(surf2view, reflection), 0.0), 32);
    vec4 specular = vec4(0.6) * specContribution;

    outColor = (ambient + (diffuse * att)+ (specular * pow(att, 3))) * texColor;
}

OpenGL绘图代码

void Render()
{
    ...

    glm::mat4 view, projection; // Model will be done via MatrixStack
    view = glm::lookAt(position, position + direction, up); // cam pos, look at (eye pos), up vec
    projection = glm::perspective(45.0f, (float)width/(float)height, 0.1f, 1000.0f);
    glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    // Lighting
    lightPos.x = 0.0 + sin(time / 125) * 10;

    glUniform3f(glGetUniformLocation(basicShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);

    // Objects  (use bump mapping on this cube)
    bumpShader.Use();
    glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    glUniform3f(glGetUniformLocation(bumpShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
    MatrixStack::LoadIdentity();
    MatrixStack::Scale(2);
    MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container"));
    glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "img"), 0);
    glActiveTexture(GL_TEXTURE1); // Normal map
    glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container_normal"));
    glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "normalMap"), 1);

    glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 1);
    cubeNormal.Draw();

    MatrixStack::LoadIdentity();
    MatrixStack::Translate(glm::vec3(0.0f, -22.0f, 0.0f));
    MatrixStack::Scale(glm::vec3(200.0f, 20.0f, 200.0f));
    MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor"));
    glActiveTexture(GL_TEXTURE1); // Normal map
    glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor_normal"));
    glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 100);
    cubeNormal.Draw();

    MatrixStack::LoadIdentity();
    glActiveTexture(GL_TEXTURE0);

    ...
}

编辑 我现在使用启用了“aiProcess_CalcTangentSpace”标志的 ASSIMP 库加载我的对象,并相应地更改了我的着色器以适应新的更改。由于 ASSIMP 现在会自动计算正确的切向量,因此我应该有有效的切向量并且我的问题应该得到解决(正如 Nicol Bolas 所指出的),但我仍然遇到同样的问题,镜面照明表现得很奇怪,并且漫反射照明没有真正出现. 我想还有其他东西不能正常工作。我将您的答案取消标记为正确答案 Nicol Bolas(目前)并相应地更新了我的代码,因为我仍然缺少一些东西。

应该和翻译有关吧。一旦我向模型矩阵添加一个平移(y 方向上的 -22.0f),它就会对奇怪的光照做出反应。只要地板(实际上是一个立方体)没有平移,照明看起来就很好。

4

2 回答 2

9

计算顶点着色器中的切向量

嗯,有你的问题。这对于任意表面是不可能的。

切线和双切线不是相互垂直的任意向量。它们是指向纹理坐标方向的模型空间方向向量。切线指向 S 纹理坐标方向,双切线指向 T 纹理坐标方向(如果您愿意,也可以使用 U 和 V 表示 tex 坐标)。

这有效地计算了纹理相对于表面上每个顶点的方向。你需要这个方向,因为当你想要理解切线空间向量时,纹理映射到表面的方式很重要。

请记住:切线空间是垂直于表面的空间。但是,例如,您需要知道该表面是如何映射到对象的,以便知道“向上”在哪里。取一个正方形表面。您可以映射纹理,使正方形的 +Y 部分沿纹理​​的 +T 方向定向。或者它可以沿着正方形的+X。您甚至可以映射它,使纹理扭曲或以任意角度旋转。

切线和双切线向量旨在校正此映射。它们指向模型空间中的 S 和 T 方向。因此,结合法线,它们形成一个变换矩阵,从切线空间变换到 3 个向量所在的任何空间(您通常将 NBT 变换到相机空间或在使用它们之前用于照明的任何空间)。

不能仅通过取法线并将其与某个任意向量交叉来计算它们。这会产生垂直法线,但不会产生正确的法线。

为了正确计算切线/副切线,您需要访问多个顶点。您需要能够看到纹理坐标如何在网格表面上发生变化,这就是您计算相对于网格的 S 和 T 方向的方式。

顶点着色器不能访问多个顶点。几何着色器(通常)也无法访问足够的顶点来执行此操作。在 CPU 上离线计算切线/副切线。

于 2013-08-08T08:02:50.457 回答
0
 mat3 mat = mat3(t.x, b.x ,n.x, t.y, b.y ,n.y, t.z, b.z ,n.z);

是错的。为了正确使用 tbn 矩阵,您必须转置它,如下所示:

 mat3 mat = transpose(mat3(t.x, b.x ,n.x, t.y, b.y ,n.y, t.z, b.z ,n.z));

然后用它把你的光和视图向量转换成切线空间。或者(效率较低),将未转置的 tbn 矩阵传递给片段着色器,并使用它将采样的法线转换为视图空间。这是一件容易错过的事情,但非常重要。有关更多信息,请参阅http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/

附带说明一下,您可以对顶点着色器进行一个小的优化,即计算 cpu 上的每个网格的法线矩阵,因为它对于网格上的所有顶点都是相同的,因此可以减少不必要的计算。

于 2015-11-10T04:49:47.907 回答