R2 Core Shaders

R2Fog.h
Go to the documentation of this file.
1 #ifndef R2_FOG_H
2 #define R2_FOG_H
3
4 /// \file R2Fog.h
5 /// \brief Types related to fog rendering
6
7 /// The type of fog parameters
8
9 struct R2_fog_t {
10 /// The positive eye-space Z distance that fog starts
11 /// Anything closer than this distance will not have any fog applied.
12 float z_near;
13 /// The positive eye-space Z distance that fog ends.
14 /// Anything beyond this distance will be fully fogged.
15 float z_far;
16 /// The fog color
17 vec3 color;
18 };
19
20 ///
21 /// Calculate the linear fog amount for the given positive eye-space Z value \a z.
22 ///
23 /// @param fog Fog parameters
24 /// @param z The positive eye-space Z value of the surface
25 ///
26
27 float
28 R2_fogAmountLinear(
29 const R2_fog_t fog,
30 const float z)
31 {
32 return clamp((z - fog.z_near) / (fog.z_far - fog.z_near), 0.0, 1.0);
33 }
34
35 ///
36 /// Calculate the quadratic fog amount for the given positive eye-space Z value \a z.
37 ///
38 /// @param fog Fog parameters
39 /// @param z The positive eye-space Z value of the surface
40 ///
41
42 float
43 R2_fogAmountQuadratic(
44 const R2_fog_t fog,
45 const float z)
46 {
47 float linear = R2_fogAmountLinear(fog, z);
48 return linear * linear;
49 }
50
51 ///
52 /// Calculate the inverse quadratic fog amount for the given positive eye-space Z value \a z.
53 ///
54 /// @param fog Fog parameters
55 /// @param z The positive eye-space Z value of the surface
56 ///
57
58 float
59 R2_fogAmountQuadraticInverse(
60 const R2_fog_t fog,
61 const float z)
62 {
63 return sqrt(R2_fogAmountLinear(fog, z));
64 }
65
66 ///
67 /// Calculate linear fog for the given positive eye-space Z value \a z.
68 ///
69 /// @param fog Fog parameters
70 /// @param surface The current surface color
71 /// @param z The positive eye-space Z value of the surface
72 ///
73
74 vec3
75 R2_fogLinear(
76 const R2_fog_t fog,
77 const vec3 surface,
78 const float z)
79 {
80 return mix(surface, fog.color, R2_fogAmountLinear(fog, z));
81 }
82
83 ///
84 /// Calculate quadratic fog for the given positive eye-space Z value \a z.
85 ///
86 /// @param fog Fog parameters
87 /// @param surface The current surface color
88 /// @param z The positive eye-space Z value of the surface
89 ///
90
91 vec3
92 R2_fogQuadratic(
93 const R2_fog_t fog,
94 const vec3 surface,
95 const float z)
96 {
97 return mix(surface, fog.color, R2_fogAmountQuadratic(fog, z));
98 }
99
100 ///
101 /// Calculate inverse quadratic fog for the given positive eye-space Z value \a z.
102 ///
103 /// @param fog Fog parameters
104 /// @param surface The current surface color
105 /// @param z The positive eye-space Z value of the surface
106 ///
107
108 vec3
109 R2_fogQuadraticInverse(
110 const R2_fog_t fog,
111 const vec3 surface,
112 const float z)
113 {
114 return mix(surface, fog.color, R2_fogAmountQuadraticInverse(fog, z));
115 }
116
117 #endif // R2_FOG_H
R2_fogQuadratic
vec3 R2_fogQuadratic(const R2_fog_t fog, const vec3 surface, const float z)
Definition: R2Fog.h:92
R2_fogAmountQuadratic
float R2_fogAmountQuadratic(const R2_fog_t fog, const float z)
Definition: R2Fog.h:43
R2_fogLinear
vec3 R2_fogLinear(const R2_fog_t fog, const vec3 surface, const float z)
Definition: R2Fog.h:75
R2_fogAmountQuadraticInverse
float R2_fogAmountQuadraticInverse(const R2_fog_t fog, const float z)
Definition: R2Fog.h:59
R2_fog_t::z_far
float z_far
Definition: R2Fog.h:15
R2_fogAmountLinear
float R2_fogAmountLinear(const R2_fog_t fog, const float z)
Definition: R2Fog.h:28
R2_fog_t
The type of fog parameters.
Definition: R2Fog.h:9
R2_fog_t::color
vec3 color
The fog color.
Definition: R2Fog.h:17
R2_fogQuadraticInverse
vec3 R2_fogQuadraticInverse(const R2_fog_t fog, const vec3 surface, const float z)
Definition: R2Fog.h:109
R2_fog_t::z_near
float z_near
Definition: R2Fog.h:12