R2 Shaders
R2LightDirectional.h
Go to the documentation of this file.
1 #ifndef R2_LIGHT_DIRECTIONAL_H
2 #define R2_LIGHT_DIRECTIONAL_H
3 
4 /// \file R2LightDirectional.h
5 /// \brief Functions and types related to directional lighting
6 
7 /// Vectors used when calculating directional lighting
8 
10  /// Direction from observer to surface (referred to as `V` in most texts)
12  /// Direction from surface to light source (referred to as `L` in most texts)
14  /// The surface normal (referred to as `N` in most texts)
15  vec3 normal;
16  /// Reflection between observer and normal (referred to as `R` in most texts)
17  vec3 reflection;
18 };
19 
20 /// A directional light type
21 
23  /// The light color. The components are assumed to be in the range `[0, 1]`.
24  vec3 color;
25  /// The light direction. Assumed to be normalized.
26  vec3 direction;
27  /// The light intensity.
28  float intensity;
29 };
30 
31 /// Calculate the vectors required to calculate directional lighting.
32 ///
33 /// @param light The light parameters
34 /// @param p The surface position
35 /// @param n The surface normal
36 ///
37 /// @return A set of lighting vectors
38 
41  const R2_light_directional_t light,
42  const vec3 p,
43  const vec3 n)
44 {
45  vec3 observer_to_surface = normalize (p);
46  vec3 surface_to_light = normalize (-light.direction);
47  vec3 reflection = reflect (observer_to_surface, n);
48  return R2_light_directional_vectors_t (observer_to_surface, surface_to_light, n, reflection);
49 }
50 
51 /// Calculate the diffuse term for a directional light.
52 ///
53 /// @param light The light parameters
54 /// @param v The calculated light vectors
55 ///
56 /// @return The diffuse term
57 
58 vec3
60  const R2_light_directional_t light,
62 {
63  float factor = max (0.0, dot (v.surface_to_light, v.normal));
64  return (light.color * light.intensity) * factor;
65 }
66 
67 /// Calculate the specular term for a directional light
68 ///
69 /// @param light The light parameters
70 /// @param v The calculated light vectors
71 /// @param specular_color The surface specular color
72 /// @param specular_exponent The surface specular exponent
73 ///
74 /// @return The specular term
75 
76 vec3
78  const R2_light_directional_t light,
80  const vec3 specular_color,
81  const float specular_exponent)
82 {
83  float base_factor =
84  max (0.0, dot (v.reflection, v.surface_to_light));
85  float factor =
86  pow (base_factor, specular_exponent);
87  vec3 color =
88  (light.color * light.intensity) * factor;
89  return color * specular_color;
90 }
91 
92 #endif
vec3 R2_lightDirectionalDiffuseTerm(const R2_light_directional_t light, const R2_light_directional_vectors_t v)
vec3 direction
The light direction. Assumed to be normalized.
vec3 color
The light color. The components are assumed to be in the range [0, 1].
Vectors used when calculating directional lighting.
vec3 reflection
Reflection between observer and normal (referred to as R in most texts)
vec3 surface_to_light
Direction from surface to light source (referred to as L in most texts)
vec3 normal
The surface normal (referred to as N in most texts)
vec3 R2_lightDirectionalSpecularTerm(const R2_light_directional_t light, const R2_light_directional_vectors_t v, const vec3 specular_color, const float specular_exponent)
float intensity
The light intensity.
A directional light type.
R2_light_directional_vectors_t R2_lightDirectionalVectors(const R2_light_directional_t light, const vec3 p, const vec3 n)
vec3 observer_to_surface
Direction from observer to surface (referred to as V in most texts)