gpt4 book ai didi

opengl - 蓬照明 : add specular lighting separately or with ambient and diffuse?

转载 作者:行者123 更新时间:2023-12-01 22:10:53 26 4
gpt4 key购买 nike

我正在尝试实现 Phong 照明。在某些教程中,将镜面光照添加到环境光照和漫射光照中,然后将总光照乘以纹理颜色。我还看到了一个教程,其中在添加环境光和漫射光并乘以纹理颜色后单独添加镜面照明。

这是一个片段着色器,其中包含两个选项和屏幕截图。

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;
in vec3 normals;
in vec3 fragPosition;


//texture samplers
uniform sampler2D texture1;
uniform vec3 ambientLight;
uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform vec3 viewPosition;
uniform float specularStrength;
uniform float shineDamp;

void main()
{
vec3 norm = normalize(normals);
vec3 lightDir = normalize(lightPosition - fragPosition);

float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;

vec3 viewDir = normalize(viewPosition - fragPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shineDamp);
vec3 specular = specularStrength * spec * lightColor;

// 1. Specular is added to ambient and diffuse lights and this result is multiplied with texture
//FragColor = vec4((ambientLight + diffuse + specular), 1.0f) * texture(texture1, TexCoord);

// 2. Specular is added separately to result of multiplication of ambient + diffuse and texture
//FragColor = vec4((ambientLight + diffuse), 1.0f) * texture(texture1, TexCoord) + vec4(specular, 1.0);
}
  • 选项 1. 将镜面反射添加到环境光和漫反射

    enter image description here

  • 选项 2. 单独添加镜面反射。

    enter image description here

在这些屏幕截图中,shineDump 值为 32.0f,specularStrength 为 0.5f。

哪一个看起来正确?在我看来,与第一个选项相比,第二个选项看起来是正确的,但很多教程都使用第一个选项中的公式。

最佳答案

I am trying to implement Phong lighting. In some tutorials specular lighting is added to ambient and diffuse lighting and then total lighting is multiplied by texture color.

这是黑暗时代的历史文物,当时照明方程仍然硬连线在 GPU 中,并且 Gouraud shading是标准。仅在顶点处评估光照模型,并将所得的光照值插值到整个图元上。由于纹理通常用于模拟 Material 的表面属性,因此通常在每个片段处对纹理进行采样(以便我们可以为图元提供超出几何中指定级别的结构)。但由于高洛德着色,我们需要用每个片段的纹理数据来调制已经计算出的光值。最简单的方法是将两者相乘。

I also saw a tutorial where specular lighting was added separately after the addition of ambient and diffuse lights was multiplied with texture color.

通过纹理颜色调节整个照明会给很多 Material 带来不切实际的结果。为了解决这些问题,有时会分离镜面反射部分。现在,我们计算每个顶点的环境光+漫反射部分和镜面反射部分,对它们进行插值,用纹理调制环境光+漫反射,然后为每个片段添加镜面反射部分。

但是,现在没有人会使用 Gouraud 着色,而是计算每个片段的照明。我们不再有不同的频率来评估光模型和纹理采样,因此这些问题变得毫无意义。由于某些表面反射的实际光线取决于 Material ,并且我们使用纹理来模拟该 Material ,因此我们可以直接使用纹理作为照明计算的输入,即作为漫反射率或镜面反射率, 管他呢。这还允许我们在图元上改变任意 Material 属性,而不仅仅是“颜色”,光泽度/粗糙度也可以来自纹理、法线方向以及照明模型可能使用的任何参数。

关于opengl - 蓬照明 : add specular lighting separately or with ambient and diffuse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com