3.2 Point Light
struct PointLight {
vec3 Intensity;
vec3 Position;
float AttenuationConstant;
float AttenuationLinear;
float AttenuationQuadratic;
};
Point Lights dont have a specific direction, but a certain position in the scene. In addition, they have attenuation factors to dim the light depending on the distance between light source and the lit surface.
The attenuation can be calculated many different ways, usually a point light has 3 factors:
AttenuationConstant
AttenuationLinear
AttenuationQuadratic
float Attenuation = 1.0f / (
light.AttenuationConstant +
light.AttenuationLinear * Distance +
light.AttenuationQuadratic * Distance * Distance);
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;
};
struct PointLight {
vec3 Intensity;
vec3 Position;
float AttenuationConstant;
float AttenuationLinear;
float AttenuationQuadratic;
};
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 ...
PointLight light = PointLight( vec3(1, 1, 1), vec3(-2, 4, 5), 1.0f, 0.0f, 0.1f );
vec3 N = normalize(vertex.normal);
vec3 L = normalize(light.Position - vertex.position);
vec3 R = normalize(reflect(-L, N));
vec3 V = normalize(CameraPosition - vertex.position);
float Distance = length(light.Position - vertex.position);
float Attenuation =
1.0f / (light.AttenuationConstant + light.AttenuationLinear * Distance + light.AttenuationQuadratic * Distance * Distance);
// diffuse Intensity
Id = Id + light.Intensity * max(0, dot(N, L)) * Attenuation;
// specular intensity
Is = Is + light.Intensity * max(0, pow(max(0, dot(R, V)), max(1, Ns))) * Attenuation;
// final fragment color
vec3 color = Kd * (Ia + Id) + Ks * Is;
out_color = vec4(color, 1);
}