3.1 Directional Light

struct DirectionalLight {

vec3 Intensity;

vec3 Direction;

};

  • -L: direction of incoming light ray

  • N: surface normal

  • R: reflected light direction

  • V: direction from surface to the scene camera

Diffuse Intensity Id:

    • depends on intensity of the light source

    • depends on the angle between N and L

// diffuse Intensity

Id = Id + light.Intensity * max(0, dot(N, L));

To avoid negative light intensities, we use max(0, ...)

Specular Intensity Is:

    • depends on intensity of the light source

    • depends on the angle between R and V

    • depends on the shininess Ns of the surface material

// specular intensity

Is = Is + light.Intensity * max(0, pow(max(0, dot(R, V)), max(1, Ns)));

https://en.wikipedia.org/wiki/Phong_reflection_model

Shader Source Code:

Fragment Shader: "model.fragmentshader.txt"

#version 450 core

struct Material {

vec3 Kd; // diffuse

vec3 Ks; // specular

float Ns; // shininess

};

struct DirectionalLight {

vec3 Intensity;

vec3 Direction;

};

in VS_FS_INTERFACE {

vec3 position;

vec3 normal;

} vertex;

// example material and other lighting properties

uniform Material material = Material ( vec3(0, 0, 0), vec3(0, 0, 0), 0 );

uniform vec3 AmbientIntensity = vec3(0, 0, 0);

uniform vec3 CameraPosition = vec3(0, 0, 0);

out layout (location = 0) vec4 out_color;

void main ()

{

// material

vec3 Kd = material.Kd;

vec3 Ks = material.Ks;

float Ns = material.Ns;

// light parts

vec3 Ia = AmbientIntensity;

vec3 Id = vec3(0, 0, 0);

vec3 Is = vec3(0, 0, 0);

// process light sources here ...

DirectionalLight light = DirectionalLight( vec3(1, 1, 1), vec3(2, -1, -5) );

vec3 N = normalize(vertex.normal);

vec3 L = normalize(-light.Direction);

vec3 R = normalize(reflect(light.Direction, N));

vec3 V = normalize(CameraPosition - vertex.position);

// diffuse Intensity

Id = Id + light.Intensity * max(0, dot(N, L));

// specular intensity

Is = Is + light.Intensity * max(0, pow(max(0, dot(R, V)), max(1, Ns)));

// final fragment color

vec3 color = Kd * (Ia + Id) + Ks * Is;

out_color = vec4(color, 1);

}