17
\$\begingroup\$

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:

enter image description here

Second:

enter image description here

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?

\$\endgroup\$
  • 2
    \$\begingroup\$ There is a computer graphics stack that you might find better answers with this question. \$\endgroup\$ – Andrew Wilson Oct 8 '15 at 5:51
34
\$\begingroup\$

specular lighting of directional light

more area has specular lighting when camera is far from an object

Yep, looks right to me.

The specular area for directional lights is supposed to be more or less constant w.r.t. the camera, given the same reflection surface. To compare, we can look at the phenomenon known as sunglint, which is basically specular reflection of the Sun on water, and since the Sun is approximately a directional light source for terrestrial purposes, and the reflection surface is the Earth which is approximately flat, this is a good analogue of your Phong lighting.

Example 1: area of sunglint roughly the same as a residential lot

Sunglint over lake

http://academic.emporia.edu/aberjame/remote/lec10/lec10.htm

Example 2: area of sunglint approximate to the Great Lakes

Sunglint over Great Lakes

http://earthobservatory.nasa.gov/IOTD/view.php?id=78617

In fact the sunglint area will just keep enlarging until you are far enough from the Earth for it to be more round than flat, from your perspective.

In terms of the Phong reflection model, the specular depends on three directions: the eye, the light, and the surface. Since your camera is looking at the same surface, just moving away, the eye stays the same; the surface stays the same by virtue of being flat, and the light stays the same by definition (it's a directional light), therefore the specular reflection will be the same - from the camera/screen's perspective, of course.

\$\endgroup\$
  • 1
    \$\begingroup\$ Love the real-life examples answer to a programming question \$\endgroup\$ – defhlt Jan 3 '16 at 19:44
  • \$\begingroup\$ dammit, you made me create an account just to upvote. \$\endgroup\$ – Spongman May 29 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.