R2 Core 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
9 struct R2_light_positional_vectors_t {
10 /// Direction from observer to surface (referred to as `V` in most texts)
11 vec3 observer_to_surface;
12 /// Direction from surface to light source (referred to as `L` in most texts)
13 vec3 surface_to_light;
14 /// Direction from light source to surface (referred to as `-L` in most texts)
15 vec3 light_to_surface;
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
24 struct R2_light_positional_t {
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)`.
32 float inverse_range;
33 /// The inverse falloff value `(1.0 / falloff)`.
34 float inverse_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
45 R2_light_positional_vectors_t
46 R2_lightPositionalVectors (
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);
54 vec3 surface_to_light = -light_to_surface;
55 float distance = length (position_diff);
56
57 return R2_light_positional_vectors_t (
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
78 R2_lightPositionalAttenuation(
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
R2_light_positional_t::color
vec3 color
The light color. The components are assumed to be in the range [0, 1].
Definition: R2LightPositional.h:26
R2_lightPositionalAttenuation
float R2_lightPositionalAttenuation(const R2_light_positional_t light, const float distance)
Definition: R2LightPositional.h:78
R2_light_positional_vectors_t
Vectors used when calculating positional lighting.
Definition: R2LightPositional.h:9
R2_light_positional_vectors_t::surface_to_light
vec3 surface_to_light
Direction from surface to light source (referred to as L in most texts)
Definition: R2LightPositional.h:13
R2_light_positional_t::inverse_falloff
float inverse_falloff
The inverse falloff value (1.0 / falloff).
Definition: R2LightPositional.h:34
R2_light_positional_t::intensity
float intensity
The light intensity.
Definition: R2LightPositional.h:28
R2_light_positional_t::inverse_range
float inverse_range
The inverse light range (1.0 / range).
Definition: R2LightPositional.h:32
R2_light_positional_vectors_t::distance
float distance
The distance between the surface and light source.
Definition: R2LightPositional.h:19
R2_light_positional_t
A positional light type.
Definition: R2LightPositional.h:24
R2_lightPositionalVectors
R2_light_positional_vectors_t R2_lightPositionalVectors(const R2_light_positional_t light, const vec3 p, const vec3 n)
Definition: R2LightPositional.h:46
R2_light_positional_vectors_t::light_to_surface
vec3 light_to_surface
Direction from light source to surface (referred to as -L in most texts)
Definition: R2LightPositional.h:15
R2_light_positional_vectors_t::observer_to_surface
vec3 observer_to_surface
Direction from observer to surface (referred to as V in most texts)
Definition: R2LightPositional.h:11
R2_light_positional_vectors_t::normal
vec3 normal
The surface normal (referred to as N in most texts)
Definition: R2LightPositional.h:17
R2_light_positional_t::position
vec3 position
The eye-space light position.
Definition: R2LightPositional.h:30