Ocean
Loading...
Searching...
No Matches
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
18#include "ocean/math/Rotation.h"
19
20namespace Ocean
21{
22
23namespace Rendering
24{
25
26namespace GlobalIllumination
27{
28
29// Forward declaration.
30class TracingObject;
31
32// Forward declaration.
33class TracingGroup;
34
35/**
36 * This class implements object lighting functions.
37 * @ingroup renderinggi
38 */
39class OCEAN_RENDERING_GI_EXPORT Lighting
40{
41 public:
42
43 /**
44 * Definition of individual lighting modes.
45 */
47 {
48 /// No lighting is applied.
50 /// Lambert lighting is applied.
52 /// Lambert and specular lighting is applied.
54 /// Transparency is supported.
55 LM_TRANSPARENCY = (1 << 2),
56 /// Specular reflection is supported.
58 /// Transparency and specular refraction is supported
60 /// Shadows are supported.
61 LM_SHADOWS = (1 << 5),
62 /// Supersample shadows are supported.
64 /// Translucent shadows and light damping is supported.
66 /// Full shading is supported.
68 /// Full reflection and refraction is supported.
70 /// Full shadows are supported.
72 /// Entire lighting functions are supported.
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
159inline 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 {
165 return intensity;
166 }
167
168 ocean_assert(lightAttenuation[0] >= 0);
169 ocean_assert(lightAttenuation[1] >= 0);
170 ocean_assert(lightAttenuation[2] >= 0);
171
172 ocean_assert(intensity >= 0 && intensity <= 1);
173
174 // check whether we have a simple attenuation case
175 if (lightAttenuation[1] == 0 && lightAttenuation[2] == 0)
176 {
177 return intensity;
178 }
179
180 const Scalar factor = lightAttenuation[0] + lightAttenuation[1] * lightObjectDistance + lightAttenuation[2] * Numeric::sqr(lightObjectDistance);
181 ocean_assert(factor >= Numeric::eps());
182
183 if (factor < Numeric::eps())
184 {
185 Log::warning() << "Infinite small attenuation factor.";
186 return intensity;
187 }
188
189 return intensity / factor;
190}
191
192inline Scalar Lighting::pointLightAttenuationFactorSqr(const Vector3& lightAttenuation, const Scalar lightObjectDistanceSqr, const Scalar intensity)
193{
194 ocean_assert(intensity >= 0 && intensity <= 1);
195
196 if (lightAttenuation.isNull())
197 {
198 return intensity;
199 }
200
201 ocean_assert(lightAttenuation[0] >= 0);
202 ocean_assert(lightAttenuation[1] >= 0);
203 ocean_assert(lightAttenuation[2] >= 0);
204
205 ocean_assert(lightObjectDistanceSqr >= 0);
206
207 ocean_assert(intensity >= 0 && intensity <= 1);
208
209 // check whether we have a simple attenuation case
210 if (lightAttenuation[1] == 0 && lightAttenuation[2] == 0)
211 {
212 return intensity;
213 }
214
215 const Scalar factor = lightAttenuation[0] + lightAttenuation[1] * Numeric::sqrt(lightObjectDistanceSqr) + lightAttenuation[2] * lightObjectDistanceSqr;
216 ocean_assert(factor >= Numeric::eps());
217
218 if (factor < Numeric::eps())
219 {
220 Log::warning() << "Infinite small attenuation factor.";
221 return intensity;
222 }
223
224 return intensity / factor;
225}
226
227inline Scalar Lighting::spotLightAttenuationFactor(const Vector3& lightAttenuation, const Scalar lightObjectDistance, const Scalar intensity, const Vector3& lightDirection, const Vector3& lightObjectDirection, const Scalar coneAngleCos, const Scalar spotExponent)
228{
229 ocean_assert(Numeric::isEqual(lightDirection.length(), 1));
230 ocean_assert(Numeric::isEqual(lightObjectDirection.length(), 1));
231
232 ocean_assert(intensity >= 0 && intensity <= 1);
233
234 const Scalar factor = lightDirection * lightObjectDirection;
235
236 if (factor < coneAngleCos)
237 {
238 return 0;
239 }
240
241 if (spotExponent == 0)
242 {
243 return pointLightAttenuationFactor(lightAttenuation, lightObjectDistance, intensity);
244 }
245
246 return pointLightAttenuationFactor(lightAttenuation, lightObjectDistance, intensity) * Numeric::pow(factor, spotExponent * Scalar(128));
247}
248
249}
250
251}
252
253}
254
255#endif // META_OCEAN_RENDERING_GI_LIGHTING_H
static MessageObject warning()
Returns the message for warning messages.
Definition Messenger.h:1090
static Scalar pow(const Scalar x, const Scalar y)
static Scalar sqrt(const Scalar value)
static constexpr Scalar eps()
static bool isEqual(const Scalar first, const Scalar second)
static constexpr Scalar sqr(const Scalar value)
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
@ LM_SHADOWS_SUPERSAMPLE
Supersample shadows are supported.
Definition Lighting.h:63
@ LM_UNLIT
No lighting is applied.
Definition Lighting.h:49
@ LM_SHADING_SPECULAR
Lambert and specular lighting is applied.
Definition Lighting.h:53
@ LM_REFLECTION_REFRACTION_FULL
Full reflection and refraction is supported.
Definition Lighting.h:69
@ LM_SHADOWS_TRANSLUCENT_LIGHT
Translucent shadows and light damping is supported.
Definition Lighting.h:65
@ LM_SHADING_LAMBERT
Lambert lighting is applied.
Definition Lighting.h:51
@ LM_SHADOWS
Shadows are supported.
Definition Lighting.h:61
@ LM_TRANSPARENCY
Transparency is supported.
Definition Lighting.h:55
@ LM_SHADOWS_FULL
Full shadows are supported.
Definition Lighting.h:71
@ LM_REFLECTION_SPECULAR
Specular reflection is supported.
Definition Lighting.h:57
@ LM_LIGHTING_FULL
Entire lighting functions are supported.
Definition Lighting.h:73
@ LM_REFRACTION_SPECULAR
Transparency and specular refraction is supported.
Definition Lighting.h:59
@ LM_SHADING_FULL
Full shading is supported.
Definition Lighting.h:67
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:227
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:192
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:858
T length() const
Returns the length of the vector.
Definition Vector3.h:668
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< LightPair > LightSources
Definition of a vector holding light pairs.
Definition GILightSource.h:40
Namespace of the Rendering Global Illumination library.
Definition GIAttribute.h:23
Namespace of the Rendering library.
Definition AbsoluteTransform.h:18
The namespace covering the entire Ocean framework.
Definition Accessor.h:15