Ocean
Lighting.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_RENDERING_GI_LIGHTING_H
9 #define META_OCEAN_RENDERING_GI_LIGHTING_H
10 
15 
16 #include "ocean/math/Quaternion.h"
17 #include "ocean/math/RGBAColor.h"
18 #include "ocean/math/Rotation.h"
19 
20 namespace Ocean
21 {
22 
23 namespace Rendering
24 {
25 
26 namespace GlobalIllumination
27 {
28 
29 // Forward declaration.
30 class TracingObject;
31 
32 // Forward declaration.
33 class TracingGroup;
34 
35 /**
36  * This class implements object lighting functions.
37  * @ingroup renderinggi
38  */
39 class OCEAN_RENDERING_GI_EXPORT Lighting
40 {
41  public:
42 
43  /**
44  * Definition of individual lighting modes.
45  */
47  {
48  /// No lighting is applied.
49  LM_UNLIT = 0,
50  /// Lambert lighting is applied.
51  LM_SHADING_LAMBERT = (1 << 0),
52  /// Lambert and specular lighting is applied.
53  LM_SHADING_SPECULAR = (1 << 1) | LM_SHADING_LAMBERT,
54  /// Transparency is supported.
55  LM_TRANSPARENCY = (1 << 2),
56  /// Specular reflection is supported.
57  LM_REFLECTION_SPECULAR = (1 << 3),
58  /// Transparency and specular refraction is supported
59  LM_REFRACTION_SPECULAR = (1 << 4) | LM_TRANSPARENCY,
60  /// Shadows are supported.
61  LM_SHADOWS = (1 << 5),
62  /// Supersample shadows are supported.
63  LM_SHADOWS_SUPERSAMPLE = (1 << 6) | LM_SHADOWS,
64  /// Translucent shadows and light damping is supported.
65  LM_SHADOWS_TRANSLUCENT_LIGHT = (1 << 7) | LM_SHADOWS,
66  /// Full shading is supported.
67  LM_SHADING_FULL = LM_SHADING_LAMBERT | LM_SHADING_SPECULAR,
68  /// Full reflection and refraction is supported.
69  LM_REFLECTION_REFRACTION_FULL = LM_REFLECTION_SPECULAR | LM_REFRACTION_SPECULAR,
70  /// Full shadows are supported.
71  LM_SHADOWS_FULL = LM_SHADOWS | LM_SHADOWS_SUPERSAMPLE | LM_SHADOWS_TRANSLUCENT_LIGHT,
72  /// Entire lighting functions are supported.
73  LM_LIGHTING_FULL = LM_SHADING_FULL | LM_REFLECTION_REFRACTION_FULL | LM_SHADOWS_FULL
74  };
75 
76  /**
77  * Determines the light (the color) for a specified viewing ray, intersection point, appearance information and light sources, including damped light from translucent objects between the intersection object and all light sources.
78  * @param viewPosition The start position of the viewing ray
79  * @param viewObjectDirection The direction of the viewing ray
80  * @param objectPosition The intersction location of the viewing ray with any kind of object
81  * @param objectNormal The normal at the intersection location of the object
82  * @param textureCoordinate The texture coordinate at the location of the intersection
83  * @param material The material information of the object which is intersected
84  * @param textures The textures of the object
85  * @param lightSources The light sources defining the entire amount of light in the scene
86  * @param object The tracing object which is intersected
87  * @param root The root of all tracing objects defining the entire scene
88  * @param bounces The number of reflection bounces to be used, with range [0, infinity)
89  * @param lightingModes The lighting modes defining lighting technique to be applied
90  * @param color The resulting color/light for the defined viewing ray
91  * @return True, if succeeded
92  */
93  static bool dampedLight(const Vector3& viewPosition, const Vector3& viewObjectDirection, const Vector3& objectPosition, const Vector3& objectNormal, const Vector2& textureCoordinate, const GIMaterial* material, const GITextures* textures, const LightSources& lightSources, const TracingObject& object, const TracingGroup& root, const unsigned int bounces, const LightingModes lightingModes, RGBAColor& color);
94 
95  /**
96  * Determines the direct lighting for a specific location and light source using the Lambert shading with optional additional specular shading.
97  * The light as distributed from the light source is expected to be not attenuated by any obstacle between light and object.<br>
98  * However, the resulting color/light can be adjusted by any kind of adjustment factor e.g., to integrate any kind of attenuation.
99  * @param viewObjectDirection Direction starting at the viewing position (the center of projection) and pointing to the object intersection location, with unit length
100  * @param objectPosition The location on the object where the viewing ray intersects the object
101  * @param objectNormal The normal on the object where the viewing ray intersects the object
102  * @param lightObjectDirection Direction starting at the light source and pointing to the object intersection location, with unit length
103  * @param materialLightDiffuse Combination of diffuse light and material components
104  * @param materialLightSpecular Combination of specular light and material components
105  * @param materialSpecularExponent Specular material exponent
106  * @param lightingModes The lighting modes defining lighting technique to be applied
107  * @param diffuse Resulting diffuse color
108  * @param specular Resulting specular color
109  * @return True, if succeeded
110  */
111  static bool directLight(const Vector3& viewObjectDirection, const Vector3& objectPosition, const Vector3& objectNormal, const Vector3& lightObjectDirection, const RGBAColor& materialLightDiffuse, const RGBAColor& materialLightSpecular, const Scalar materialSpecularExponent, const LightingModes lightingModes, RGBAColor& diffuse, RGBAColor& specular);
112 
113  /**
114  * Determines the light damping factors for a specific position and light source.
115  * @param lightPosition Position of the light source
116  * @param lightObjectDirection Direction starting at the light source pointing to the object intersection position, with unit length
117  * @param lightObjectDistance Distance between the object's intersection point and the light source
118  * @param lightColor Color of the light source
119  * @param root Root tracing object
120  * @param lightingModes The lighting modes defining lighting technique to be applied
121  * @return Resulting damping factors
122  */
123  static RGBAColor lightDampingFactors(const Vector3& lightPosition, const Vector3& lightObjectDirection, const Scalar lightObjectDistance, const RGBAColor& lightColor, const TracingGroup& root, const LightingModes lightingModes);
124 
125  protected:
126 
127  /**
128  * Returns the attenuation factor for a given point light.
129  * @param lightAttenuation Constant, linear and square light attenuation factors
130  * @param lightObjectDistance Distance between the object's intersection point and the light source
131  * @param intensity Intensity of the light source, with range [0, 1]
132  * @return Resulting attenuation factor
133  */
134  static inline Scalar pointLightAttenuationFactor(const Vector3& lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity);
135 
136  /**
137  * Returns the attenuation factor for a given point light.
138  * @param lightAttenuation Constant, linear and square light attenuation factors
139  * @param lightObjectDistanceSqr Square distance between the object's intersection point and the light source, with range [0, infinity)
140  * @param intensity Intensity of the light source, with range [0, 1]
141  * @return Resulting attenuation factor
142  */
143  static inline Scalar pointLightAttenuationFactorSqr(const Vector3& lightAttenuation, const Scalar lightObjectDistanceSqr, const Scalar intensity);
144 
145  /**
146  * Returns the attenuation factor for a given spot light.
147  * @param lightAttenuation Constant, linear and square light attenuation factors
148  * @param lightObjectDistance Distance between the object's intersection point and the light source
149  * @param intensity Intensity of the light source, with range [0, 1]
150  * @param lightDirection Direction of the spotlight starting at the light source pointing into the scene, with unit length
151  * @param lightObjectDirection Direction starting at the light source pointing to the object intersection position, with unit length
152  * @param coneAngleCos Cosine of the spont light cone angle
153  * @param spotExponent Lighting exponent of the spot light, with range [0, 128]
154  * @return Resulting attenuation factor
155  */
156  static inline Scalar spotLightAttenuationFactor(const Vector3& lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity, const Vector3& lightDirection, const Vector3& lightObjectDirection, const Scalar coneAngleCos, const Scalar spotExponent);
157 };
158 
159 inline Scalar Lighting::pointLightAttenuationFactor(const Vector3& lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity)
160 {
161  ocean_assert(intensity >= 0 && intensity <= 1);
162 
163  if (lightAttenuation.isNull())
164  return intensity;
165 
166  ocean_assert(lightAttenuation[0] >= 0);
167  ocean_assert(lightAttenuation[1] >= 0);
168  ocean_assert(lightAttenuation[2] >= 0);
169 
170  ocean_assert(intensity >= 0 && intensity <= 1);
171 
172  // check whether we have a simple attenuation case
173  if (lightAttenuation[1] == 0 && lightAttenuation[2] == 0)
174  return intensity;
175 
176  const Scalar factor = lightAttenuation[0] + lightAttenuation[1] * lightObjectDistance + lightAttenuation[2] * Numeric::sqr(lightObjectDistance);
177  ocean_assert(factor < Numeric::eps());
178 
179  if (factor < Numeric::eps())
180  {
181  Log::warning() << "Infinite small attenuation factor.";
182  return intensity;
183  }
184 
185  return intensity / factor;
186 }
187 
188 inline Scalar Lighting::pointLightAttenuationFactorSqr(const Vector3& lightAttenuation, const Scalar lightObjectDistanceSqr, const Scalar intensity)
189 {
190  ocean_assert(intensity >= 0 && intensity <= 1);
191 
192  if (lightAttenuation.isNull())
193  return intensity;
194 
195  ocean_assert(lightAttenuation[0] >= 0);
196  ocean_assert(lightAttenuation[1] >= 0);
197  ocean_assert(lightAttenuation[2] >= 0);
198 
199  ocean_assert(lightObjectDistanceSqr >= 0);
200 
201  ocean_assert(intensity >= 0 && intensity <= 1);
202 
203  // check whether we have a simple attenuation case
204  if (lightAttenuation[1] == 0 && lightAttenuation[2] == 0)
205  return intensity;
206 
207  const Scalar factor = lightAttenuation[0] + lightAttenuation[1] * Numeric::sqrt(lightObjectDistanceSqr) + lightAttenuation[2] * lightObjectDistanceSqr;
208  ocean_assert(factor < Numeric::eps());
209 
210  if (factor < Numeric::eps())
211  {
212  Log::warning() << "Infinite small attenuation factor.";
213  return intensity;
214  }
215 
216  return intensity / factor;
217 }
218 
219 inline Scalar Lighting::spotLightAttenuationFactor(const Vector3& lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity, const Vector3& lightDirection, const Vector3& lightObjectDirection, const Scalar coneAngleCos, const Scalar spotExponent)
220 {
221  ocean_assert(Numeric::isEqual(lightDirection.length(), 1));
222  ocean_assert(Numeric::isEqual(lightObjectDirection.length(), 1));
223 
224  ocean_assert(intensity >= 0 && intensity <= 1);
225 
226  const Scalar factor = lightDirection * lightObjectDirection;
227 
228  if (factor < coneAngleCos)
229  return 0;
230 
231  if (spotExponent == 0)
232  return pointLightAttenuationFactor(lightAttenuation, lightObjectDistance, intensity);
233 
234  return pointLightAttenuationFactor(lightAttenuation, lightObjectDistance, intensity) * Numeric::pow(factor, spotExponent * Scalar(128));
235 }
236 
237 }
238 
239 }
240 
241 }
242 
243 #endif // META_OCEAN_RENDERING_GI_LIGHTING_H
static MessageObject warning()
Returns the message for warning messages.
Definition: Messenger.h:1069
static T pow(const T x, const T y)
Returns x raised to the power of y.
Definition: Numeric.h:1860
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static constexpr T eps()
Returns a small epsilon.
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition: Numeric.h:2386
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition: Numeric.h:1495
This class implements a color defined by red, green, blue and alpha parameters.
Definition: RGBAColor.h:41
This class implements a Global Illumination material object.
Definition: GIMaterial.h:33
This class implements a Global Illumination texture attributes object.
Definition: GITextures.h:34
This class implements object lighting functions.
Definition: Lighting.h:40
LightingModes
Definition of individual lighting modes.
Definition: Lighting.h:47
static bool directLight(const Vector3 &viewObjectDirection, const Vector3 &objectPosition, const Vector3 &objectNormal, const Vector3 &lightObjectDirection, const RGBAColor &materialLightDiffuse, const RGBAColor &materialLightSpecular, const Scalar materialSpecularExponent, const LightingModes lightingModes, RGBAColor &diffuse, RGBAColor &specular)
Determines the direct lighting for a specific location and light source using the Lambert shading wit...
static Scalar spotLightAttenuationFactor(const Vector3 &lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity, const Vector3 &lightDirection, const Vector3 &lightObjectDirection, const Scalar coneAngleCos, const Scalar spotExponent)
Returns the attenuation factor for a given spot light.
Definition: Lighting.h:219
static bool dampedLight(const Vector3 &viewPosition, const Vector3 &viewObjectDirection, const Vector3 &objectPosition, const Vector3 &objectNormal, const Vector2 &textureCoordinate, const GIMaterial *material, const GITextures *textures, const LightSources &lightSources, const TracingObject &object, const TracingGroup &root, const unsigned int bounces, const LightingModes lightingModes, RGBAColor &color)
Determines the light (the color) for a specified viewing ray, intersection point, appearance informat...
static Scalar pointLightAttenuationFactor(const Vector3 &lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity)
Returns the attenuation factor for a given point light.
Definition: Lighting.h:159
static Scalar pointLightAttenuationFactorSqr(const Vector3 &lightAttenuation, const Scalar lightObjectDistanceSqr, const Scalar intensity)
Returns the attenuation factor for a given point light.
Definition: Lighting.h:188
static RGBAColor lightDampingFactors(const Vector3 &lightPosition, const Vector3 &lightObjectDirection, const Scalar lightObjectDistance, const RGBAColor &lightColor, const TracingGroup &root, const LightingModes lightingModes)
Determines the light damping factors for a specific position and light source.
This class implements a group of tracing objects.
Definition: TracingGroup.h:28
This class is the abstract base class for all tracing objects.
Definition: TracingObject.h:39
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition: Vector3.h:854
T length() const
Returns the length of the vector.
Definition: Vector3.h:664
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< LightPair > LightSources
Definition of a vector holding light pairs.
Definition: GILightSource.h:40
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15