【问题标题】:Trouble with GLSL dot productGLSL 点积问题
【发布时间】:2021-11-29 16:05:14
【问题描述】:

所以我尝试使用 OpenGL 实现基本的漫反射照明。我编写了一个简单的着色器,它将采用法线向量和光向量,并使用所述向量的点积计算像素的亮度。这是我的输出:

  • 来自左侧的光([1, 0, 0] 作为光矢量)
  • 光下降([0, -1, 0] 作为光矢量)
  • 来自后面的光([0, 0, 1] 作为光矢量)

如您所见,它在前两种情况下运行良好,但在第三种情况下完全失效。顺便说一句,[0, 0, -1] 也不起作用,并且 [0, 1, 1] 给出的输出与光亮时相同([0, 1, 0])。这是我的着色器:

  • 顶点着色器:
#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform vec3 lightDirection;

out vec3 normal;
out vec3 lightDir;

void main()
{
    normal = normalize(aNormal);
    lightDir = normalize(lightDirection);

    gl_Position = projection * view * model * vec4(aPos, 1.0f);
}
  • 片段着色器:
#version 330 core

in vec3 normal;
in vec3 lightDir;

out vec4 FragColor;

void main()
{
    float grey = max(dot(-lightDir, normal), 0.0f);
    FragColor = vec4(grey, grey, grey, 1.0f);
}

我认为问题与点积有关,但我找不到原因。

【问题讨论】:

    标签: c++ opengl glsl


    【解决方案1】:

    漫射光是使用公式max(dot(-lightDir, normal), 0.0f); 计算的。所以如果dot (-lightDir, normal)小于0,场景就全黑了。
    2 个Unit vectorDot product 是两个向量之间夹角的余弦值。因此,如果角度 > 90° 且 这意味着,当物体背面被照亮时,它会显示为完全黑色。


    光的方向是世界空间中的一个向量。 dot(-lightDir, normal) 仅在法线也是世界空间中的向量时才有意义。
    normal 从模型空间转换到世界空间:

    normal = inverse(transpose(mat3(model))) * normalize(aNormal);
    

    (Why transforming normals with the transpose of the inverse of the modelview matrix?)

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2012-04-16
      • 2013-06-30
      • 1970-01-01
      相关资源
      最近更新 更多