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  _usePrefilteredMap(false),
41  _envLightIntensity(1.0f),
42  _envSampleCount(DEFAULT_ENV_SAMPLE_COUNT),
43  _refractionTwoSided(false)
44  {
45  }
46  virtual ~LightHandler() { }
47 
48  /// Create a new light handler
49  static LightHandlerPtr create() { return std::make_shared<LightHandler>(); }
50 
51  /// @name Global State
52  /// @{
53 
54  /// Set the light transform.
55  void setLightTransform(const Matrix44& mat)
56  {
57  _lightTransform = mat;
58  }
59 
60  /// Return the light transform.
62  {
63  return _lightTransform;
64  }
65 
66  /// Set whether direct lighting is enabled.
67  void setDirectLighting(bool enable)
68  {
69  _directLighting = enable;
70  }
71 
72  /// Return whether direct lighting is enabled.
73  bool getDirectLighting() const
74  {
75  return _directLighting;
76  }
77 
78  /// Set whether indirect lighting is enabled.
79  void setIndirectLighting(bool enable)
80  {
81  _indirectLighting = enable;
82  }
83 
84  /// Return whether indirect lighting is enabled.
85  bool getIndirectLighting() const
86  {
87  return _indirectLighting;
88  }
89 
90  /// @}
91  /// @name Environment Lighting
92  /// @{
93 
94  /// Set the environment radiance map
96  {
97  _envRadianceMap = map;
98  }
99 
100  /// Return the environment radiance map
102  {
103  return _envRadianceMap;
104  }
105 
106  /// Set the environment radiance map for the prefiltered environment lighting model.
108  {
109  _envPrefilteredMap = map;
110  }
111 
112  /// Return the environment radiance map for the prefiltered environment lighting model.
114  {
115  return _envPrefilteredMap;
116  }
117 
118  /// Set whether to use the prefiltered environment lighting model.
120  {
121  _usePrefilteredMap = val;
122  }
123 
124  /// Return whether to use the prefiltered environment lighting model.
126  {
127  return _usePrefilteredMap;
128  }
129 
130  /// Set the environment irradiance map
132  {
133  _envIrradianceMap = map;
134  }
135 
136  /// Return the environment irradiance map
138  {
139  return _envIrradianceMap;
140  }
141 
142  /// Set the environment lighting sample count.
144  {
145  _envSampleCount = count;
146  }
147 
148  /// Return the environment lighting sample count.
149  int getEnvSampleCount() const
150  {
151  return _envSampleCount;
152  }
153 
154  /// Set the environment light intensity.
155  void setEnvLightIntensity(const float intensity)
156  {
157  _envLightIntensity = intensity;
158  }
159 
160  /// Return the environment light intensity.
162  {
163  return _envLightIntensity;
164  }
165 
166  /// Set the two-sided refraction property.
167  void setRefractionTwoSided(bool enable)
168  {
169  _refractionTwoSided = enable;
170  }
171 
172  /// Return the two-sided refraction property.
174  {
175  return _refractionTwoSided;
176  }
177 
178  /// @}
179  /// @name Albedo Table
180  /// @{
181 
182  /// Set the directional albedo table
184  {
185  _albedoTable = table;
186  }
187 
188  /// Return the directional albedo table
190  {
191  return _albedoTable;
192  }
193 
194  /// @}
195  /// @name Light Sources
196  /// @{
197 
198  /// Add a light source.
199  void addLightSource(NodePtr node);
200 
201  /// Set the vector of light sources.
202  void setLightSources(const vector<NodePtr>& lights)
203  {
204  _lightSources = lights;
205  }
206 
207  /// Return the vector of light sources.
208  const vector<NodePtr>& getLightSources() const
209  {
210  return _lightSources;
211  }
212 
213  /// Return the first light source, if any, of the given category.
214  NodePtr getFirstLightOfCategory(const string& category)
215  {
216  for (NodePtr light : _lightSources)
217  {
218  if (light->getCategory() == category)
219  {
220  return light;
221  }
222  }
223  return nullptr;
224  }
225 
226  /// @}
227  /// @name Light IDs
228  /// @{
229 
230  /// Get a list of identifiers associated with a given light nodedef
231  const std::unordered_map<string, unsigned int>& getLightIdMap() const
232  {
233  return _lightIdMap;
234  }
235 
236  /// From a set of nodes, create a mapping of corresponding
237  /// nodedef identifiers to numbers
238  LightIdMap computeLightIdMap(const vector<NodePtr>& nodes);
239 
240  /// Find lights to use based on an input document
241  /// @param doc Document to scan for lights
242  /// @param lights List of lights found in document
243  void findLights(DocumentPtr doc, vector<NodePtr>& lights);
244 
245  /// Register light node definitions and light count with a given generation context
246  /// @param doc Document containing light nodes and definitions
247  /// @param lights Lights to register
248  /// @param context Context to update
249  void registerLights(DocumentPtr doc, const vector<NodePtr>& lights, GenContext& context);
250 
251  /// @}
252 
253  protected:
258 
264 
266 
268 
269  vector<NodePtr> _lightSources;
270  std::unordered_map<string, unsigned int> _lightIdMap;
271 };
272 
274 
275 #endif
bool getUsePrefilteredMap()
Return whether to use the prefiltered environment lighting model.
Definition: LightHandler.h:125
ImagePtr getEnvRadianceMap() const
Return the environment radiance map.
Definition: LightHandler.h:101
void setLightTransform(const Matrix44 &mat)
Set the light transform.
Definition: LightHandler.h:55
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
std::unordered_map< string, unsigned int > _lightIdMap
Definition: LightHandler.h:270
int getRefractionTwoSided() const
Return the two-sided refraction property.
Definition: LightHandler.h:173
vector< NodePtr > _lightSources
Definition: LightHandler.h:269
void setRefractionTwoSided(bool enable)
Set the two-sided refraction property.
Definition: LightHandler.h:167
void setLightSources(const vector< NodePtr > &lights)
Set the vector of light sources.
Definition: LightHandler.h:202
const vector< NodePtr > & getLightSources() const
Return the vector of light sources.
Definition: LightHandler.h:208
ImagePtr _envPrefilteredMap
Definition: LightHandler.h:260
bool _indirectLighting
Definition: LightHandler.h:256
void setDirectLighting(bool enable)
Set whether direct lighting is enabled.
Definition: LightHandler.h:67
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:61
bool getDirectLighting() const
Return whether direct lighting is enabled.
Definition: LightHandler.h:73
GLfloat f
Definition: glcorearb.h:1926
void setEnvSampleCount(int count)
Set the environment lighting sample count.
Definition: LightHandler.h:143
ImagePtr getAlbedoTable() const
Return the directional albedo table.
Definition: LightHandler.h:189
bool getIndirectLighting() const
Return whether indirect lighting is enabled.
Definition: LightHandler.h:85
bool _refractionTwoSided
Definition: LightHandler.h:265
MATERIALX_NAMESPACE_BEGIN MX_RENDER_API const int DEFAULT_ENV_SAMPLE_COUNT
void setEnvPrefilteredMap(ImagePtr map)
Set the environment radiance map for the prefiltered environment lighting model.
Definition: LightHandler.h:107
const std::unordered_map< string, unsigned int > & getLightIdMap() const
Get a list of identifiers associated with a given light nodedef.
Definition: LightHandler.h:231
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:49
void setUsePrefilteredMap(bool val)
Set whether to use the prefiltered environment lighting model.
Definition: LightHandler.h:119
int getEnvSampleCount() const
Return the environment lighting sample count.
Definition: LightHandler.h:149
void setEnvIrradianceMap(ImagePtr map)
Set the environment irradiance map.
Definition: LightHandler.h:131
ImagePtr _envIrradianceMap
Definition: LightHandler.h:261
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:254
ImagePtr getEnvPrefilteredMap() const
Return the environment radiance map for the prefiltered environment lighting model.
Definition: LightHandler.h:113
void setEnvLightIntensity(const float intensity)
Set the environment light intensity.
Definition: LightHandler.h:155
#define MX_RENDER_API
Definition: Export.h:18
void setIndirectLighting(bool enable)
Set whether indirect lighting is enabled.
Definition: LightHandler.h:79
float getEnvLightIntensity()
Return the environment light intensity.
Definition: LightHandler.h:161
float _envLightIntensity
Definition: LightHandler.h:262
GLuint GLfloat * val
Definition: glcorearb.h:1608
void setAlbedoTable(ImagePtr table)
Set the directional albedo table.
Definition: LightHandler.h:183
bool _directLighting
Definition: LightHandler.h:255
ImagePtr getEnvIrradianceMap() const
Return the environment irradiance map.
Definition: LightHandler.h:137
ImagePtr _envRadianceMap
Definition: LightHandler.h:259
ImagePtr _albedoTable
Definition: LightHandler.h:267
void setEnvRadianceMap(ImagePtr map)
Set the environment radiance map.
Definition: LightHandler.h:95
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
virtual ~LightHandler()
Definition: LightHandler.h:46
bool _usePrefilteredMap
Definition: LightHandler.h:257
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:214