我正在尝试实现 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);
}
在这些截图中,shineDump 值为 32.0f,specularStrength 为 0.5f。
哪一个看起来正确?在我看来,与第一个选项相比,第二个选项看起来是正确的,但是很多教程都使用第一个选项中的公式。