HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Harmonics.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_HARMONICS_H
7 #define MATERIALX_HARMONICS_H
8 
9 /// @file
10 /// Spherical harmonics functionality
11 
12 #include <MaterialXRender/Export.h>
13 #include <MaterialXRender/Image.h>
14 #include <MaterialXRender/Types.h>
15 
17 
18 /// Class template for a vector of spherical harmonic coefficients.
19 ///
20 /// Template parameter C is the coefficient type (e.g. double, Color3).
21 /// Template parameter B is the number of spherical harmonic bands.
22 template <class C, size_t B> class ShCoeffs
23 {
24  public:
25  static const size_t NUM_BANDS = B;
26  static const size_t NUM_COEFFS = B * B;
27 
28  public:
29  ShCoeffs() = default;
30  explicit ShCoeffs(const std::array<C, NUM_COEFFS>& arr) :
31  _arr(arr) { }
32 
33  /// @name Comparison Operators
34  /// @{
35 
36  /// Return true if the given vector is identical to this one.
37  bool operator==(const ShCoeffs& rhs) const { return _arr == rhs._arr; }
38 
39  /// Return true if the given vector differs from this one.
40  bool operator!=(const ShCoeffs& rhs) const { return _arr != rhs._arr; }
41 
42  /// @}
43  /// @name Indexing Operators
44  /// @{
45 
46  /// Return the coefficient at the given index.
47  C& operator[](size_t i) { return _arr.at(i); }
48 
49  /// Return the const coefficient at the given index.
50  const C& operator[](size_t i) const { return _arr.at(i); }
51 
52  /// @}
53 
54  protected:
55  std::array<C, NUM_COEFFS> _arr;
56 };
57 
58 /// Double-precision scalar coefficients for third-order spherical harmonics.
60 
61 /// Double-precision color coefficients for third-order spherical harmonics.
63 
64 /// Project an environment map to third-order SH, with an optional convolution
65 /// to convert radiance to irradiance.
66 /// @param env An environment map in lat-long format.
67 /// @param irradiance If true, then the returned signal will be convolved
68 /// by a clamped cosine kernel to generate irradiance.
69 /// @return The projection of the environment to third-order SH.
70 MX_RENDER_API Sh3ColorCoeffs projectEnvironment(ConstImagePtr env, bool irradiance = false);
71 
72 /// Normalize an environment to the given radiance.
73 /// @param env An environment map in lat-long format.
74 /// @param envRadiance The radiance to which the environment map should be normalized.
75 /// @param maxTexelRadiance The maximum radiance allowed for any individual texel of the map.
76 /// @return A new normalized environment map, in the same format as the original.
77 MX_RENDER_API ImagePtr normalizeEnvironment(ConstImagePtr env, float envRadiance, float maxTexelRadiance);
78 
79 /// Compute the dominant light direction and color of an environment map.
80 /// @param env An environment map in lat-long format.
81 /// @param lightDir Returns the dominant light direction of the environment.
82 /// @param lightColor Returns the color of the light from the dominant direction.
83 MX_RENDER_API void computeDominantLight(ConstImagePtr env, Vector3& lightDir, Color3& lightColor);
84 
85 /// Render the given spherical harmonic signal to an environment map.
86 /// @param shEnv The color signal of the environment encoded as third-order SH.
87 /// @param width The width of the output environment map.
88 /// @param height The height of the output environment map.
89 /// @return An environment map in the lat-long format.
90 MX_RENDER_API ImagePtr renderEnvironment(const Sh3ColorCoeffs& shEnv, unsigned int width, unsigned int height);
91 
92 /// Render a reference irradiance map from the given environment map,
93 /// using brute-force computations for a slow but accurate result.
94 /// @param env An environment map in lat-long format.
95 /// @param width The width of the output irradiance map.
96 /// @param height The height of the output irradiance map.
97 /// @return An irradiance map in the lat-long format.
99 
101 
102 #endif
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
std::array< C, NUM_COEFFS > _arr
Definition: Harmonics.h:55
MX_RENDER_API ImagePtr renderReferenceIrradiance(ConstImagePtr env, unsigned int width, unsigned int height)
MX_RENDER_API Sh3ColorCoeffs projectEnvironment(ConstImagePtr env, bool irradiance=false)
MX_RENDER_API ImagePtr renderEnvironment(const Sh3ColorCoeffs &shEnv, unsigned int width, unsigned int height)
bool operator==(const ShCoeffs &rhs) const
Return true if the given vector is identical to this one.
Definition: Harmonics.h:37
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
MX_RENDER_API ImagePtr normalizeEnvironment(ConstImagePtr env, float envRadiance, float maxTexelRadiance)
bool operator!=(const ShCoeffs &rhs) const
Return true if the given vector differs from this one.
Definition: Harmonics.h:40
C & operator[](size_t i)
Return the coefficient at the given index.
Definition: Harmonics.h:47
ShCoeffs()=default
static const size_t NUM_BANDS
Definition: Harmonics.h:25
MX_RENDER_API void computeDominantLight(ConstImagePtr env, Vector3 &lightDir, Color3 &lightColor)
ShCoeffs(const std::array< C, NUM_COEFFS > &arr)
Definition: Harmonics.h:30
#define MX_RENDER_API
Definition: Export.h:18
Definition: Types.h:305
GLint GLsizei width
Definition: glcorearb.h:103
const C & operator[](size_t i) const
Return the const coefficient at the given index.
Definition: Harmonics.h:50
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
static const size_t NUM_COEFFS
Definition: Harmonics.h:26
shared_ptr< const Image > ConstImagePtr
A shared pointer to a const image.
Definition: Image.h:26