我实施了Phong照明。一切似乎都可以正常工作-圆环和球体都按预期方式进行了照明,等等。但是我注意到定向光的镜面照明有些奇怪。
这是两个屏幕截图。
第一:
第二:
如您所见,当相机远离物体时,更多区域具有镜面照明。
以下是简化的顶点着色器:
#version 330 core
layout(location = 0) in vec3 vertexPos;
layout(location = 1) in vec3 vertexNorm;
layout(location = 2) in vec2 vertexUV;
uniform mat4 MVP;
uniform mat4 M;
out vec2 fragmentUV;
out vec3 fragmentNormal;
out vec3 fragmentPos;
void main() {
fragmentUV = vertexUV;
fragmentNormal = (M * vec4(vertexNorm, 0)).xyz;
fragmentPos = (M * vec4(vertexPos, 1)).xyz;
gl_Position = MVP * vec4(vertexPos, 1);
}
...和片段着色器:
#version 330 core
in vec2 fragmentUV;
in vec3 fragmentNormal;
in vec3 fragmentPos;
struct DirectionalLight {
vec3 Color;
vec3 Direction;
float AmbientIntensity;
float DiffuseIntensity;
};
uniform sampler2D textureSampler;
uniform vec3 cameraPos;
uniform float materialSpecularFactor;
uniform float materialSpecularIntensity;
uniform DirectionalLight directionalLight;
out vec4 color;
void main() {
vec3 normal = normalize(fragmentNormal); // should be normalized after interpolation
vec4 ambientColor = vec4(directionalLight.Color, 1) * directionalLight.AmbientIntensity;
float diffuseFactor = clamp(dot(normal, -directionalLight.Direction), 0, 1);
vec4 diffuseColor = vec4(directionalLight.Color, 1) * directionalLight.DiffuseIntensity * diffuseFactor;
vec3 vertexToCamera = normalize(cameraPos - fragmentPos);
vec3 lightReflect = normalize(reflect(directionalLight.Direction, normal));
float specularFactor = pow(clamp(dot(vertexToCamera, lightReflect), 0, 1), materialSpecularFactor);
vec4 specularColor = vec4(directionalLight.Color, 1) * materialSpecularIntensity * specularFactor;
color = texture(textureSampler, fragmentUV) * (ambientColor + diffuseColor + specularColor);
}
在此存储库中可以找到完整的源代码,没有任何简化。
我想知道我是否实施了错误的操作。还是Phong照明可以吗?
2
有一个计算机图形堆栈,您可能会对此问题找到更好的答案。
—
安德鲁·威尔逊