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