R2 Shaders
R2LightPositional.h
Go to the documentation of this file.
1 #ifndef R2_LIGHT_POSITIONAL_H
2 #define R2_LIGHT_POSITIONAL_H
3 
4 /// \file R2LightPositional.h
5 /// \brief Functions and types related to positional lighting
6 
7 /// Vectors used when calculating positional 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  /// Direction from light source to surface (referred to as `-L` in most texts)
16  /// The surface normal (referred to as `N` in most texts)
17  vec3 normal;
18  /// The distance between the surface and light source
19  float distance;
20 };
21 
22 /// A positional light type
23 
25  /// The light color. The components are assumed to be in the range `[0, 1]`.
26  vec3 color;
27  /// The light intensity.
28  float intensity;
29  /// The eye-space light position.
30  vec3 position;
31  /// The inverse light range `(1.0 / range)`.
33  /// The inverse falloff value `(1.0 / falloff)`.
35 };
36 
37 /// Calculate the vectors required to calculate positional lighting.
38 ///
39 /// @param light The light parameters
40 /// @param p The surface position (eye-space)
41 /// @param n The surface normal (eye-space)
42 ///
43 /// @return A set of lighting vectors
44 
47  const R2_light_positional_t light,
48  const vec3 p,
49  const vec3 n)
50 {
51  vec3 position_diff = p - light.position;
52  vec3 observer_to_surface = normalize (p);
53  vec3 light_to_surface = normalize (position_diff);
55  float distance = length (position_diff);
56 
58  observer_to_surface,
59  surface_to_light,
60  light_to_surface,
61  n,
62  distance
63  );
64 }
65 
66 ///
67 /// Given a `light` at `distance` from the current point on the lit
68 /// surface, calculate the amount of attenuation. The returned value
69 /// is `1.0` for "no attenuation" and `0.0` for "fully attenuated".
70 ///
71 /// @param light The light
72 /// @param distance The distance from the light to the surface
73 ///
74 /// @return An attenuation value in the range `[0.0, 1.0]`
75 ///
76 
77 float
79  const R2_light_positional_t light,
80  const float distance)
81 {
82  float linear = distance * light.inverse_range;
83  float exponential = pow (linear, light.inverse_falloff);
84  return 1.0 - clamp (exponential, 0.0, 1.0);
85 }
86 
87 #endif // R2_LIGHT_POSITIONAL_H
vec3 color
The light color. The components are assumed to be in the range [0, 1].
float R2_lightPositionalAttenuation(const R2_light_positional_t light, const float distance)
Vectors used when calculating positional lighting.
vec3 surface_to_light
Direction from surface to light source (referred to as L in most texts)
float inverse_falloff
The inverse falloff value (1.0 / falloff).
float intensity
The light intensity.
float inverse_range
The inverse light range (1.0 / range).
float distance
The distance between the surface and light source.
A positional light type.
R2_light_positional_vectors_t R2_lightPositionalVectors(const R2_light_positional_t light, const vec3 p, const vec3 n)
vec3 light_to_surface
Direction from light source to surface (referred to as -L in most texts)
vec3 observer_to_surface
Direction from observer to surface (referred to as V in most texts)
vec3 normal
The surface normal (referred to as N in most texts)
vec3 position
The eye-space light position.