forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.h
More file actions
210 lines (161 loc) · 6.27 KB
/
Material.h
File metadata and controls
210 lines (161 loc) · 6.27 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
200
201
202
203
204
205
206
207
208
209
210
#ifndef MATERIALXVIEW_MATERIAL_H
#define MATERIALXVIEW_MATERIAL_H
#include <MaterialXRender/GeometryHandler.h>
#include <MaterialXCore/Document.h>
#include <MaterialXFormat/XmlIo.h>
#include <MaterialXFormat/File.h>
#include <MaterialXGenGlsl/GlslShaderGenerator.h>
#include <MaterialXGenShader/HwShaderGenerator.h>
#include <MaterialXGenShader/UnitSystem.h>
#include <MaterialXRender/ImageHandler.h>
#include <MaterialXRender/LightHandler.h>
#include <nanogui/common.h>
#include <nanogui/glutil.h>
namespace mx = MaterialX;
namespace ng = nanogui;
using MaterialPtr = std::shared_ptr<class Material>;
using GLShaderPtr = std::shared_ptr<ng::GLShader>;
using StringPair = std::pair<std::string, std::string>;
class DocumentModifiers
{
public:
mx::StringMap remapElements;
mx::StringSet skipElements;
std::string filePrefixTerminator;
};
class ShadowState
{
public:
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;
}
/// 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,
const std::string& shaderName,
bool hasTransparency);
/// Generate a shader from the given inputs.
bool generateShader(mx::GenContext& context);
/// Copy shader from one material to this one
void copyShader(MaterialPtr material)
{
_hwShader = material->_hwShader;
_glShader = material->_glShader;
}
/// Generate a constant color shader
bool generateConstantShader(mx::GenContext& context,
mx::DocumentPtr stdLib,
const std::string& shaderName,
const mx::Color3& color);
/// Generate an environment background shader
bool generateEnvironmentShader(mx::GenContext& context,
const mx::FilePath& filename,
mx::DocumentPtr stdLib,
const mx::FilePath& imagePath);
/// Return the underlying OpenGL shader.
GLShaderPtr getShader() const
{
return _glShader;
}
/// Return true if this material has transparency.
bool hasTransparency() const
{
return _hasTransparency;
}
/// Bind shader
void bindShader();
/// Bind viewing information for this material.
void bindViewInformation(const mx::Matrix44& world, const mx::Matrix44& view, const mx::Matrix44& proj);
/// Bind all images for this material.
void bindImages(mx::ImageHandlerPtr imageHandler, const mx::FileSearchPath& searchPath);
/// 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, const mx::Color4* fallbackColor = nullptr);
/// Bind lights to shader.
void bindLights(mx::LightHandlerPtr lightHandler, mx::ImageHandlerPtr imageHandler,
bool directLighting, bool indirectLighting, const ShadowState& shadowState,
mx::HwSpecularEnvironmentMethod specularEnvironmentMethod, int envSamples);
/// Bind units.
void bindUnits(mx::UnitConverterRegistryPtr& registry, const mx::GenContext& context);
/// Bind the given mesh to this material.
void bindMesh(mx::MeshPtr mesh) const;
/// Bind a mesh partition to this material.
bool bindPartition(mx::MeshPartitionPtr part) const;
/// Draw the given mesh partition.
void drawPartition(mx::MeshPartitionPtr part) const;
/// 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;
/// Change the uniform value inside the shader and the associated element in the MaterialX document.
void changeUniformElement(mx::ShaderPort* uniform, const std::string& value);
/// Set the value for an integer element with a given path.
void setUniformInt(const std::string& path, int value);
/// Set the value for a float element with a given path.
void setUniformFloat(const std::string& path, float value);
/// Set the value for a vector2 element with a given path.
void setUniformVec2(const std::string& path, const ng::Vector2f& value);
/// Set the value for a vector3 element with a given path.
void setUniformVec3(const std::string& path, const ng::Vector3f& value);
/// Set the value for a vector4 element with a given path.
void setUniformVec4(const std::string& path, const ng::Vector4f& value);
/// Set the value for an enumerated element with a given path.
void setUniformEnum(const std::string& path, int index, const std::string& value);
protected:
void bindUniform(const std::string& name, mx::ConstValuePtr value);
void updateUniformsList();
protected:
GLShaderPtr _glShader;
mx::ShaderPtr _hwShader;
mx::DocumentPtr _doc;
mx::TypedElementPtr _elem;
std::string _udim;
bool _hasTransparency;
mx::StringSet _uniformVariable;
std::vector<mx::ImagePtr> _boundImages;
};
#endif // MATERIALXVIEW_MATERIAL_H