I implemented Phong lighting. Everything seems to work - torus and spheres are lighted as expected, etc. But I notices something strange regarding specular lighting of directional light.
Here are two screenshots.
First:
Second:
As you can see more area has specular lighting when camera is far from an object.
Here are simplified vertex shader:
#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);
}
... and fragment shader:
#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);
}
Full source code without any simplification could be found in this repository.
I wanted to know if I implemented something wrong. Or is it OK for Phong lighting?