forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.h
More file actions
199 lines (154 loc) · 5.19 KB
/
Material.h
File metadata and controls
199 lines (154 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef MATERIALXVIEW_MATERIAL_H
#define MATERIALXVIEW_MATERIAL_H
#include <MaterialXRenderGlsl/GlslProgram.h>
#include <MaterialXGenGlsl/GlslShaderGenerator.h>
#include <MaterialXGenShader/UnitSystem.h>
namespace mx = MaterialX;
using MaterialPtr = std::shared_ptr<class Material>;
class DocumentModifiers
{
public:
mx::StringMap remapElements;
mx::StringSet skipElements;
std::string filePrefixTerminator;
};
class ShadowState
{
public:
mx::ImagePtr shadowMap;
mx::Matrix44 shadowMatrix;
mx::ImagePtr ambientOcclusionMap;
float ambientOcclusionGain = 0.0f;
};
class Material
{
public:
Material() :
_hasTransparency(false)
{
}
~Material() { }
static MaterialPtr create()
{
return std::make_shared<Material>();
}
/// Return the document associated with this material
mx::DocumentPtr getDocument() const
{
return _doc;
}
/// Set the renderable element associated with this material
void setDocument(mx::DocumentPtr doc)
{
_doc = doc;
}
/// Return the renderable element associated with this material
mx::TypedElementPtr getElement() const
{
return _elem;
}
/// Set the renderable element associated with this material
void setElement(mx::TypedElementPtr val)
{
_elem = val;
}
/// Return the material node associated with this material
mx::NodePtr getMaterialNode() const
{
return _materialNode;
}
/// Set the material node associated with this material
void setMaterialNode(mx::NodePtr node)
{
_materialNode = node;
}
/// Get any associated udim identifier
const std::string& getUdim()
{
return _udim;
}
/// Set udim identifier
void setUdim(const std::string& val)
{
_udim = val;
}
/// Load shader source from file.
bool loadSource(const mx::FilePath& vertexShaderFile,
const mx::FilePath& pixelShaderFile,
bool hasTransparency);
/// Generate a shader from our currently stored element and
/// the given generator context.
bool generateShader(mx::GenContext& context);
/// Generate a shader from the given hardware shader.
bool generateShader(mx::ShaderPtr hwShader);
/// Generate an environment background shader
bool generateEnvironmentShader(mx::GenContext& context,
const mx::FilePath& filename,
mx::DocumentPtr stdLib,
const mx::FilePath& imagePath);
/// Copy shader from one material to this one
void copyShader(MaterialPtr material)
{
_hwShader = material->_hwShader;
_glProgram = material->_glProgram;
}
/// Return the underlying hardware shader.
mx::ShaderPtr getShader() const
{
return _hwShader;
}
/// Return the underlying GLSL program.
mx::GlslProgramPtr getProgram() const
{
return _glProgram;
}
/// Return true if this material has transparency.
bool hasTransparency() const
{
return _hasTransparency;
}
/// Bind shader
void bindShader();
/// Bind viewing information for this material.
void bindViewInformation(mx::CameraPtr camera);
/// Bind all images for this material.
void bindImages(mx::ImageHandlerPtr imageHandler, const mx::FileSearchPath& searchPath, bool enableMipmaps = true);
/// Unbbind all images for this material.
void unbindImages(mx::ImageHandlerPtr imageHandler);
/// Bind a single image.
mx::ImagePtr bindImage(const mx::FilePath& filePath, const std::string& uniformName, mx::ImageHandlerPtr imageHandler,
const mx::ImageSamplingProperties& samplingProperties);
/// Bind lights to shader.
void bindLighting(mx::LightHandlerPtr lightHandler, mx::ImageHandlerPtr imageHandler, const ShadowState& shadowState);
/// Bind units.
void bindUnits(mx::UnitConverterRegistryPtr& registry, const mx::GenContext& context);
/// Bind the given mesh to this material.
void bindMesh(mx::MeshPtr mesh);
/// Bind a mesh partition to this material.
bool bindPartition(mx::MeshPartitionPtr part) const;
/// Draw the given mesh partition.
void drawPartition(mx::MeshPartitionPtr part) const;
/// Unbind all geometry from this material.
void unbindGeometry();
/// Return the block of public uniforms for this material.
mx::VariableBlock* getPublicUniforms() const;
/// Find a public uniform from its MaterialX path.
mx::ShaderPort* findUniform(const std::string& path) const;
/// Modify the value of the uniform with the given path.
void modifyUniform(const std::string& path, mx::ConstValuePtr value, std::string valueString = mx::EMPTY_STRING);
protected:
void clearShader();
void updateUniformsList();
protected:
mx::ShaderPtr _hwShader;
mx::GlslProgramPtr _glProgram;
mx::MeshPtr _boundMesh;
mx::DocumentPtr _doc;
mx::TypedElementPtr _elem;
mx::NodePtr _materialNode;
std::string _udim;
bool _hasTransparency;
mx::StringSet _uniformVariable;
mx::ImageVec _boundImages;
};
#endif // MATERIALXVIEW_MATERIAL_H