法线贴图和灯光出错,无法正确显示

Joe*_*ewd 3 c++ opengl lighting glsl bump-mapping

我正在研究法线贴图的实现,通过ASSIMP库计算切线向量.

法线贴图似乎在具有接近单位矩阵的模型矩阵的对象上完美地起作用.只要我开始翻译和缩放,我的照明就好了.正如您在图片中看到的那样,法线贴图在容器立方体上完美地工作,但是在大地板上的照明失败(镜面光的方向应朝向播放器,而不是朝向容器).

照明无法正常映射

只要我开始更改模型矩阵(通过翻译),我感觉它与光的位置(目前从x = -10到x = 10,随着时间的推移)在某种程度上没有正确地包含在计算中/缩放).我发布了所有相关的代码,并希望你们能够以某种方式看到我失踪的东西,因为我已经盯着我的代码好几天了.

顶点着色器

#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;
}
Run Code Online (Sandbox Code Playgroud)

片段着色器

#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;
}
Run Code Online (Sandbox Code Playgroud)

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);

    ...
}
Run Code Online (Sandbox Code Playgroud)

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

它可能与翻译有关.一旦我将一个平移(y方向为-22.0f)添加到模型矩阵,它就会对奇怪的光照做出反应.只要地板(实际上是一个立方体)没有平移,照明就会很好.

Nic*_*las 8

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

那是你的问题.对于任意表面来说,这是不可能的.

切线和切线不是彼此垂直的任意矢量.它们是指向纹理坐标方向的模型空间方向向量.S纹理坐标方向上的切点,以及T纹理坐标方向上的切点(如果您愿意,则为tex坐标的U和V).

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

请记住:切线空间是垂直于曲面的空间.但是,您需要知道该表面如何映射到对象,以便知道例如"向上"的位置.取一个正方形表面.您可以映射纹理,使正方形的+ Y部分沿纹理​​的+ T方向定向.或者它可能沿着广场的+ X. 您甚至可以对其进行映射,以使纹理失真或以任意角度旋转.

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

不能通过采用法线并将其与任意向量交叉来计算它们.这会产生垂直法线,但不是正确法线.

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

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

  • @Jessy:赶上?它们属于*OpenGL规范*.它是GLSL的一部分.这就像是说你应该把它们称为"像素着色器"或其他类似的废话,只是因为微软在实际获取图像之前不知道某些东西不是像素.这是正确的OpenGL术语,OP问了一个OpenGL问题,所以我按照OpenGL回答了它. (2认同)