R2 Core 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
9 struct R2_light_directional_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 /// 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
22 struct R2_light_directional_t {
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
39 R2_light_directional_vectors_t
40 R2_lightDirectionalVectors (
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
59 R2_lightDirectionalDiffuseTerm (
60 const R2_light_directional_t light,
61 const R2_light_directional_vectors_t v)
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
77 R2_lightDirectionalSpecularTerm (
78 const R2_light_directional_t light,
79 const R2_light_directional_vectors_t v,
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
R2_lightDirectionalDiffuseTerm
vec3 R2_lightDirectionalDiffuseTerm(const R2_light_directional_t light, const R2_light_directional_vectors_t v)
Definition: R2LightDirectional.h:59
R2_light_directional_t::direction
vec3 direction
The light direction. Assumed to be normalized.
Definition: R2LightDirectional.h:26
R2_light_directional_t::color
vec3 color
The light color. The components are assumed to be in the range [0, 1].
Definition: R2LightDirectional.h:24
R2_light_directional_vectors_t
Vectors used when calculating directional lighting.
Definition: R2LightDirectional.h:9
R2_light_directional_vectors_t::reflection
vec3 reflection
Reflection between observer and normal (referred to as R in most texts)
Definition: R2LightDirectional.h:17
R2_light_directional_vectors_t::surface_to_light
vec3 surface_to_light
Direction from surface to light source (referred to as L in most texts)
Definition: R2LightDirectional.h:13
R2_light_directional_vectors_t::normal
vec3 normal
The surface normal (referred to as N in most texts)
Definition: R2LightDirectional.h:15
R2_lightDirectionalSpecularTerm
vec3 R2_lightDirectionalSpecularTerm(const R2_light_directional_t light, const R2_light_directional_vectors_t v, const vec3 specular_color, const float specular_exponent)
Definition: R2LightDirectional.h:77
R2_light_directional_t::intensity
float intensity
The light intensity.
Definition: R2LightDirectional.h:28
R2_light_directional_t
A directional light type.
Definition: R2LightDirectional.h:22
R2_lightDirectionalVectors
R2_light_directional_vectors_t R2_lightDirectionalVectors(const R2_light_directional_t light, const vec3 p, const vec3 n)
Definition: R2LightDirectional.h:40
R2_light_directional_vectors_t::observer_to_surface
vec3 observer_to_surface
Direction from observer to surface (referred to as V in most texts)
Definition: R2LightDirectional.h:11