HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LightHandler.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_LIGHTHANDLER_H
7 #define MATERIALX_LIGHTHANDLER_H
8 
9 /// @file
10 /// Handler for hardware lights
11 
12 #include <MaterialXRender/Export.h>
13 #include <MaterialXRender/Image.h>
14 #include <MaterialXRender/Util.h>
15 
16 #include <MaterialXCore/Document.h>
17 
19 
21 
22 class GenContext;
23 
24 /// Shared pointer to a LightHandler
25 using LightHandlerPtr = std::shared_ptr<class LightHandler>;
26 
27 /// An unordered map from light names to light indices.
28 using LightIdMap = std::unordered_map<string, unsigned int>;
29 
30 /// @class LightHandler
31 /// Utility light handler for creating and providing
32 /// light data for shader binding.
34 {
35  public:
37  _lightTransform(Matrix44::IDENTITY),
38  _directLighting(true),
39  _indirectLighting(true),
40  _envSampleCount(DEFAULT_ENV_SAMPLE_COUNT),
41  _refractionTwoSided(false)
42  {
43  }
44  virtual ~LightHandler() { }
45 
46  /// Create a new light handler
47  static LightHandlerPtr create() { return std::make_shared<LightHandler>(); }
48 
49  /// @name Global State
50  /// @{
51 
52  /// Set the light transform.
53  void setLightTransform(const Matrix44& mat)
54  {
55  _lightTransform = mat;
56  }
57 
58  /// Return the light transform.
60  {
61  return _lightTransform;
62  }
63 
64  /// Set whether direct lighting is enabled.
65  void setDirectLighting(bool enable)
66  {
67  _directLighting = enable;
68  }
69 
70  /// Return whether direct lighting is enabled.
71  bool getDirectLighting() const
72  {
73  return _directLighting;
74  }
75 
76  /// Set whether indirect lighting is enabled.
77  void setIndirectLighting(bool enable)
78  {
79  _indirectLighting = enable;
80  }
81 
82  /// Return whether indirect lighting is enabled.
83  bool getIndirectLighting() const
84  {
85  return _indirectLighting;
86  }
87 
88  /// @}
89  /// @name Environment Lighting
90  /// @{
91 
92  /// Set the environment radiance map
94  {
95  _envRadianceMap = map;
96  }
97 
98  /// Return the environment radiance map
100  {
101  return _envRadianceMap;
102  }
103 
104  /// Set the environment irradiance map
106  {
107  _envIrradianceMap = map;
108  }
109 
110  /// Return the environment irradiance map
112  {
113  return _envIrradianceMap;
114  }
115 
116  /// Set the environment lighting sample count.
118  {
119  _envSampleCount = count;
120  }
121 
122  /// Return the environment lighting sample count.
123  int getEnvSampleCount() const
124  {
125  return _envSampleCount;
126  }
127 
128  /// Set the two-sided refraction property.
129  void setRefractionTwoSided(bool enable)
130  {
131  _refractionTwoSided = enable;
132  }
133 
134  /// Return the two-sided refraction property.
136  {
137  return _refractionTwoSided;
138  }
139 
140  /// @}
141  /// @name Albedo Table
142  /// @{
143 
144  /// Set the directional albedo table
146  {
147  _albedoTable = table;
148  }
149 
150  /// Return the directional albedo table
152  {
153  return _albedoTable;
154  }
155 
156  /// @}
157  /// @name Light Sources
158  /// @{
159 
160  /// Add a light source.
161  void addLightSource(NodePtr node);
162 
163  /// Set the vector of light sources.
164  void setLightSources(const vector<NodePtr>& lights)
165  {
166  _lightSources = lights;
167  }
168 
169  /// Return the vector of light sources.
170  const vector<NodePtr>& getLightSources() const
171  {
172  return _lightSources;
173  }
174 
175  /// Return the first light source, if any, of the given category.
176  NodePtr getFirstLightOfCategory(const string& category)
177  {
178  for (NodePtr light : _lightSources)
179  {
180  if (light->getCategory() == category)
181  {
182  return light;
183  }
184  }
185  return nullptr;
186  }
187 
188  /// @}
189  /// @name Light IDs
190  /// @{
191 
192  /// Get a list of identifiers associated with a given light nodedef
193  const std::unordered_map<string, unsigned int>& getLightIdMap() const
194  {
195  return _lightIdMap;
196  }
197 
198  /// From a set of nodes, create a mapping of corresponding
199  /// nodedef identifiers to numbers
200  LightIdMap computeLightIdMap(const vector<NodePtr>& nodes);
201 
202  /// Find lights to use based on an input document
203  /// @param doc Document to scan for lights
204  /// @param lights List of lights found in document
205  void findLights(DocumentPtr doc, vector<NodePtr>& lights);
206 
207  /// Register light node definitions and light count with a given generation context
208  /// @param doc Document containing light nodes and definitions
209  /// @param lights Lights to register
210  /// @param context Context to update
211  void registerLights(DocumentPtr doc, const vector<NodePtr>& lights, GenContext& context);
212 
213  /// @}
214 
215  protected:
219 
223 
225 
227 
228  vector<NodePtr> _lightSources;
229  std::unordered_map<string, unsigned int> _lightIdMap;
230 };
231 
233 
234 #endif
ImagePtr getEnvRadianceMap() const
Return the environment radiance map.
Definition: LightHandler.h:99
void setLightTransform(const Matrix44 &mat)
Set the light transform.
Definition: LightHandler.h:53
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
std::unordered_map< string, unsigned int > _lightIdMap
Definition: LightHandler.h:229
int getRefractionTwoSided() const
Return the two-sided refraction property.
Definition: LightHandler.h:135
vector< NodePtr > _lightSources
Definition: LightHandler.h:228
void setRefractionTwoSided(bool enable)
Set the two-sided refraction property.
Definition: LightHandler.h:129
void setLightSources(const vector< NodePtr > &lights)
Set the vector of light sources.
Definition: LightHandler.h:164
const vector< NodePtr > & getLightSources() const
Return the vector of light sources.
Definition: LightHandler.h:170
bool _indirectLighting
Definition: LightHandler.h:218
void setDirectLighting(bool enable)
Set whether direct lighting is enabled.
Definition: LightHandler.h:65
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:25
Matrix44 getLightTransform() const
Return the light transform.
Definition: LightHandler.h:59
bool getDirectLighting() const
Return whether direct lighting is enabled.
Definition: LightHandler.h:71
void setEnvSampleCount(int count)
Set the environment lighting sample count.
Definition: LightHandler.h:117
ImagePtr getAlbedoTable() const
Return the directional albedo table.
Definition: LightHandler.h:151
bool getIndirectLighting() const
Return whether indirect lighting is enabled.
Definition: LightHandler.h:83
bool _refractionTwoSided
Definition: LightHandler.h:224
MATERIALX_NAMESPACE_BEGIN MX_RENDER_API const int DEFAULT_ENV_SAMPLE_COUNT
const std::unordered_map< string, unsigned int > & getLightIdMap() const
Get a list of identifiers associated with a given light nodedef.
Definition: LightHandler.h:193
std::unordered_map< string, unsigned int > LightIdMap
An unordered map from light names to light indices.
Definition: LightHandler.h:28
static LightHandlerPtr create()
Create a new light handler.
Definition: LightHandler.h:47
int getEnvSampleCount() const
Return the environment lighting sample count.
Definition: LightHandler.h:123
void setEnvIrradianceMap(ImagePtr map)
Set the environment irradiance map.
Definition: LightHandler.h:105
ImagePtr _envIrradianceMap
Definition: LightHandler.h:221
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
Matrix44 _lightTransform
Definition: LightHandler.h:216
#define MX_RENDER_API
Definition: Export.h:18
void setIndirectLighting(bool enable)
Set whether indirect lighting is enabled.
Definition: LightHandler.h:77
void setAlbedoTable(ImagePtr table)
Set the directional albedo table.
Definition: LightHandler.h:145
bool _directLighting
Definition: LightHandler.h:217
ImagePtr getEnvIrradianceMap() const
Return the environment irradiance map.
Definition: LightHandler.h:111
ImagePtr _envRadianceMap
Definition: LightHandler.h:220
ImagePtr _albedoTable
Definition: LightHandler.h:226
void setEnvRadianceMap(ImagePtr map)
Set the environment radiance map.
Definition: LightHandler.h:93
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
virtual ~LightHandler()
Definition: LightHandler.h:44
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
GLint GLsizei count
Definition: glcorearb.h:405
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24
NodePtr getFirstLightOfCategory(const string &category)
Return the first light source, if any, of the given category.
Definition: LightHandler.h:176