reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofMaterial.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofShader.h"
5// FIXME: constants deprecated only and ctor
6#include "ofConstants.h"
7#include "ofColor.h"
8
9#include "glm/fwd.hpp"
10#include "glm/vec2.hpp"
11#include <map>
12
31
32// Material concept: "Anything graphical applied to the polygons"
33//
34// Diederick Huijbers <diederick[at]apollomedia[dot]nl>
35//
36// references:
37// * Wavefront material file spec: http://people.sc.fsu.edu/~jburkardt/data/mtl/mtl.html
38// * Ogre3D: http://www.ogre3d.org/docs/manual/manual_11.html#SEC14
39// * assimp material: http://assimp.sourceforge.net/lib_html/ai_material_8h.html#7dd415ff703a2cc53d1c22ddbbd7dde0
40
41// PBR Material based on GLTF spec from KhronosGroup: https://github.com/KhronosGroup/glTF
42
44
124 ofFloatColor diffuse { 0.8f, 0.8f, 0.8f, 1.0f };
125 ofFloatColor ambient { 0.2f, 0.2f, 0.2f, 1.0f };
126 ofFloatColor specular { 0.0f, 0.0f, 0.0f, 1.0f };
127 ofFloatColor emissive { 0.0f, 0.0f, 0.0f, 1.0f };
128 float shininess{ 0.2f };
129
130 bool isPbr = false;
131 // PBR //
132 // the diffuse color is used for the albedo color, metallic color
133 float metallic {0.0};
134 float roughness {0.5};
135 float reflectance {0.5};
136 bool clearCoatEnabled = false;
137 float clearCoatStrength = 1.0;
142
143 glm::vec2 texCoordScale = { 1.0,1.0 };
144
145 // these are not to be set manually
146 std::string postFragment="";
147 std::string customUniforms="";
148 std::string uniqueIdString="";
149 std::string mainFragment="";
150 std::string mainFragmentKey="";
151
152 std::string mainVertex="";
153 std::string mainVertexKey="";
154
155};
156
157
163public:
164 ofMaterial();
165 virtual ~ofMaterial(){};
166
170 static std::string getUniformName( const ofMaterialTextureType& aMaterialTextureType );
171
173 static bool isPBRSupported();
174
177 const bool isPBR() const { return data.isPbr; }
179 void setPBR(bool ab);
180
183 void setup(const ofMaterialSettings & settings);
184
189 void setShaderMain(std::string aShaderSrc, GLenum atype, std::string skey);
190
196 void setColors(ofFloatColor oDiffuse, ofFloatColor oAmbient, ofFloatColor oSpecular, ofFloatColor emissive);
197
200 void setDiffuseColor(ofFloatColor oDiffuse);
201
204 void setAmbientColor(ofFloatColor oAmbient);
205
208 void setSpecularColor(ofFloatColor oSpecular);
209
212 void setEmissiveColor(ofFloatColor oEmmisive);
213
215 void setShininess(float nShininess);
216
220 void setTexCoordScale( float xscale, float yscale );
221
226 bool loadTexture( const ofMaterialTextureType& aMaterialTextureType, std::string apath );
227 bool loadTexture( const ofMaterialTextureType& aMaterialTextureType, std::string apath, bool bTex2d, bool mirrorY );
230 bool isPBRTexture(const ofMaterialTextureType& aMaterialTextureType);
232 void setTexture(const ofMaterialTextureType& aMaterialTextureType,const ofTexture & aTex);
235 void setDiffuseTexture(const ofTexture & aTex);
238 void setSpecularTexture(const ofTexture & aTex);
241 void setAmbientTexture(const ofTexture & aTex);
244 void setEmissiveTexture(const ofTexture & aTex);
247 void setNormalTexture(const ofTexture & aTex);
250 void setOcclusionTexture(const ofTexture & aTex);
251
252 // PBR only textures //
255 void setAoRoughnessMetallicTexture(const ofTexture & aTex);
258 void setRoughnessMetallicTexture(const ofTexture & aTex);
261 void setRoughnessTexture(const ofTexture & aTex);
264 void setMetallicTexture(const ofTexture& aTex);
267 void setDisplacementTexture(const ofTexture & aTex);
270 void setClearCoatTexture( const ofTexture& aTex );
271
272 //-- PBR properties --//
275 void setMetallic( const float& ametallic );
278 void setRoughness( const float& aroughness );
281 void setReflectance( const float& areflectance );
284 void setClearCoatEnabled( bool ab );
287 void setClearCoatStrength( const float& astrength );
290 void setClearCoatRoughness( const float& aroughness );
293 void setDisplacementStrength( const float& astrength );
296 void setDisplacementNormalsStrength( const float& astrength );
299 void setNormalGeomToNormalMapMix( const float& astrength );
300 //--! PBR properties !--//
301
302 // documented in ofBaseMaterial
307 float getShininess() const;
308
309 float getMetallic() const;
310 float getRoughness() const;
311 float getReflectance() const;
312 bool isClearCoatEnabled() const;
313 float getClearCoatStrength() const;
314 float getClearCoatRoughness() const;
315 float getDisplacementStrength() const;
316 float getDisplacementNormalsStrength() const;
317 float getNormalGeomToNormalMapMix() const;
318
321 OF_DEPRECATED_MSG("Use getSettings() instead", Data getData() const);
323
325 OF_DEPRECATED_MSG("Use setup(settings) instead", void setData(const ofMaterial::Data& data));
326
327 // documented in ofBaseMaterial
328 void begin() const;
329 void end() const;
330
331 virtual void uploadMatrices(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
332
334 void setCustomUniform1f(const std::string & name, float value);
335 void setCustomUniform2f(const std::string & name, glm::vec2 value);
336 void setCustomUniform3f(const std::string & name, glm::vec3 value);
337 void setCustomUniform4f(const std::string & name, glm::vec4 value);
338 void setCustomUniformMatrix4f(const std::string & name, glm::mat4 value);
339 void setCustomUniformMatrix3f(const std::string & name, glm::mat3 value);
340
341 void setCustomUniform1i(const std::string & name, int value);
342 void setCustomUniform2i(const std::string & name, glm::vec<2, int, glm::precision::defaultp> value);
343 void setCustomUniform3i(const std::string & name, glm::vec<3, int, glm::precision::defaultp> value);
344 void setCustomUniform4i(const std::string & name, glm::vec<4, int, glm::precision::defaultp> value);
345 void setCustomUniformTexture(const std::string & name, const ofTexture & value );
346 void setCustomUniformTexture(const std::string & name, int textureTarget, GLint textureID);
347 void setCustomUniformTexture(const std::string & name, const ofTexture & value, int textureLocation);
348 void setCustomUniformTexture(const std::string & name, int textureTarget, GLint textureID, int textureLocation);
349 bool removeCustomUniformTexture(const ofMaterialTextureType& aMaterialTextureType);
350 bool removeCustomUniformTexture(const std::string & name);
352 bool hasTexture(const ofMaterialTextureType& aMaterialTextureType) const;
353
354 void addShaderDefine( const std::string & aDefineName );
355 void addShaderDefine( const std::string & aDefineName, const std::string & aDefineValue );
356 bool removeShaderDefine( const std::string & aDefineName );
359 const std::string getDefinesString() const;
360
361 void setCustomShader(std::shared_ptr<ofShader> aCustomShader);
362
363
364protected:
366 void unbind(ofGLProgrammableRenderer & renderer) const;
367
368private:
369
370 void mergeCustomUniformTextures();
371 void mergeCustomUniformTextures(ofMaterialTextureType mainType, std::vector<ofMaterialTextureType> mergeTypes);
372
373 const std::string getShaderStringId() const;
374
375 void initShaders(ofGLProgrammableRenderer & renderer) const;
376 const ofShader & getShader(int textureTarget, bool geometryHasColor, ofGLProgrammableRenderer & renderer) const;
377 void updateMaterial(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
378 void updateLights(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
379 void updateShadows(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
380 void updateEnvironmentMaps(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
381
383
384 struct Shaders{
385 ofShader noTexture;
386 ofShader color;
387 ofShader texture2DColor;
388 ofShader textureRectColor;
389 ofShader texture2D;
390 ofShader textureRect;
391 size_t numLights;
392 size_t numCubeMaps;
393 std::string shaderId;
394 };
395 struct TextureUnifom{
396 int textureTarget;
397 GLint textureID;
398 int textureLocation;
399 std::string shaderDefine;
400 };
401 const TextureUnifom getCustomUniformTexture(const ofMaterialTextureType& aMaterialTextureType) const;
402 TextureUnifom getCustomUniformTexture(const ofMaterialTextureType& aMaterialTextureType);
403 TextureUnifom getCustomUniformTexture(const std::string & name);
404
405 mutable std::unordered_map<ofGLProgrammableRenderer*, std::shared_ptr<Shaders>> shaders;
406 static std::unordered_map<ofGLProgrammableRenderer*, std::unordered_map<std::string, std::weak_ptr<Shaders>>> shadersMap;
407// static std::string vertexShader;
408// static std::string fragmentShader;
409 std::unordered_map<std::string, float> uniforms1f;
410 std::unordered_map<std::string, glm::vec2> uniforms2f;
411 std::unordered_map<std::string, glm::vec3> uniforms3f;
412 std::unordered_map<std::string, glm::vec4> uniforms4f;
413 std::unordered_map<std::string, float> uniforms1i;
414 std::unordered_map<std::string, glm::vec<2, int, glm::precision::defaultp>> uniforms2i;
415 std::unordered_map<std::string, glm::vec<3, int, glm::precision::defaultp>> uniforms3i;
416 std::unordered_map<std::string, glm::vec<4, int, glm::precision::defaultp>> uniforms4i;
417 std::unordered_map<std::string, glm::mat4> uniforms4m;
418 std::unordered_map<std::string, glm::mat3> uniforms3m;
419 std::unordered_map<std::string, TextureUnifom> uniformstex;
420
421 std::unordered_map<std::string, std::string> mCustomUniforms;
422 std::unordered_map<std::string, std::string> mDefines;
423
424 mutable std::unordered_map<std::string, int> mShaderIdsToRemove;
425
426 // unordered_map works well here on modern compilers
427 // std::unordered_map<ofMaterialTextureType, std::shared_ptr<ofTexture> > mLocalTextures;
428 std::map<ofMaterialTextureType, std::shared_ptr<ofTexture> > mLocalTextures;
429
430 std::shared_ptr<ofShader> customShader;
431 bool bHasCustomShader = false;
432 bool mBDefinesDirty = true;
433 mutable const ofShader* currentRenderShader = nullptr;
434 bool bPrintedPBRRenderWarning = false;
435
436};
material parameter properties that can be applied to vertices in the OpenGL lighting model used in de...
Definition ofMaterialBaseTypes.h:41
Definition ofGLProgrammableRenderer.h:17
material parameter properties that can be applied to vertices in the OpenGL lighting model used in de...
Definition ofMaterial.h:162
void addShaderDefine(const std::string &aDefineName)
Definition ofMaterial.cpp:1187
void setup(const ofMaterialSettings &settings)
setup using settings struct
Definition ofMaterial.cpp:110
float getClearCoatStrength() const
Definition ofMaterial.cpp:476
void setMetallicTexture(const ofTexture &aTex)
set a metallic texture. (PBR)
Definition ofMaterial.cpp:325
void setCustomUniform2f(const std::string &name, glm::vec2 value)
Definition ofMaterial.cpp:1045
float getClearCoatRoughness() const
Definition ofMaterial.cpp:481
void setMetallic(const float &ametallic)
set metalness. (PBR)
Definition ofMaterial.cpp:340
void setDiffuseColor(ofFloatColor oDiffuse)
set the diffuse reflectance. (Phong, PBR)
Definition ofMaterial.cpp:160
void setCustomUniformMatrix3f(const std::string &name, glm::mat3 value)
Definition ofMaterial.cpp:1085
void setCustomUniform1f(const std::string &name, float value)
set custom uniforms to be used by the shader. as of 0.12.0 onwards these are added to the fragment sh...
Definition ofMaterial.cpp:1040
ofMaterialSettings Data
Definition ofMaterial.h:320
void setSpecularColor(ofFloatColor oSpecular)
set the specular reflectance. (Phong)
Definition ofMaterial.cpp:176
void setNormalGeomToNormalMapMix(const float &astrength)
percent to mix between geometry normal and normal map normal / displacement normal....
Definition ofMaterial.cpp:436
float getDisplacementStrength() const
Definition ofMaterial.cpp:486
void setClearCoatTexture(const ofTexture &aTex)
set a clear coat texture. (PBR)
Definition ofMaterial.cpp:335
void setDisplacementTexture(const ofTexture &aTex)
set a vertex displacement texture. (PBR)
Definition ofMaterial.cpp:330
virtual void uploadMatrices(const ofShader &shader, ofGLProgrammableRenderer &renderer) const
upload the given renderer's normal matrix to the material shader
Definition ofMaterial.cpp:540
void setReflectance(const float &areflectance)
set reflectance - only applies to dielectrics, ie, non metals. (PBR)
Definition ofMaterial.cpp:363
float getReflectance() const
Definition ofMaterial.cpp:466
void setRoughness(const float &aroughness)
set roughness. (PBR)
Definition ofMaterial.cpp:352
void end() const
end using this material's properties
Definition ofMaterial.cpp:533
float getShininess() const
Definition ofMaterial.cpp:451
void setDisplacementNormalsStrength(const float &astrength)
offset strength to calculate normals for vertex displacement. (PBR)
Definition ofMaterial.cpp:426
void setCustomUniformMatrix4f(const std::string &name, glm::mat4 value)
Definition ofMaterial.cpp:1080
float getRoughness() const
Definition ofMaterial.cpp:461
void setCustomUniform3f(const std::string &name, glm::vec3 value)
Definition ofMaterial.cpp:1050
void setCustomUniformTexture(const std::string &name, const ofTexture &value)
Definition ofMaterial.cpp:1091
void setSpecularTexture(const ofTexture &aTex)
set a specular map texture. (Phong)
Definition ofMaterial.cpp:283
void setCustomUniform4i(const std::string &name, glm::vec< 4, int, glm::precision::defaultp > value)
Definition ofMaterial.cpp:1075
void setNormalTexture(const ofTexture &aTex)
set a normal texture. (Phong, PBR)
Definition ofMaterial.cpp:298
void setClearCoatRoughness(const float &aroughness)
set clear coat roughness. Must call setClearCoatEnabled(true) to take effect. (PBR)
Definition ofMaterial.cpp:402
void setAmbientTexture(const ofTexture &aTex)
set an ambient light texture. (Phong)
Definition ofMaterial.cpp:288
OF_DEPRECATED_MSG("Use getSettings() instead", Data getData() const)
virtual ~ofMaterial()
Definition ofMaterial.h:165
ofFloatColor getEmissiveColor() const
Definition ofMaterial.cpp:516
void setEmissiveColor(ofFloatColor oEmmisive)
set the emitted light intensity. (Phong, PBR)
Definition ofMaterial.cpp:184
void setRoughnessTexture(const ofTexture &aTex)
set a roughness texture. (PBR)
Definition ofMaterial.cpp:320
static std::string getUniformName(const ofMaterialTextureType &aMaterialTextureType)
get the shader uniform name for the material texture type.
Definition ofMaterial.cpp:25
ofMaterial()
Definition ofMaterial.cpp:21
ofFloatColor getAmbientColor() const
Definition ofMaterial.cpp:506
void setCustomUniform1i(const std::string &name, int value)
Definition ofMaterial.cpp:1060
void setCustomUniform3i(const std::string &name, glm::vec< 3, int, glm::precision::defaultp > value)
Definition ofMaterial.cpp:1070
static bool isPBRSupported()
\is PBR supported on this platform.
Definition ofMaterial.cpp:64
void begin() const
begin using this material's properties
Definition ofMaterial.cpp:526
float getMetallic() const
Definition ofMaterial.cpp:456
void setEmissiveTexture(const ofTexture &aTex)
set an emissive texture. (Phong, PBR)
Definition ofMaterial.cpp:293
ofMaterialSettings getSettings() const
Definition ofMaterial.cpp:521
void unbind(ofGLProgrammableRenderer &renderer) const
unbind the material, override the default so we can set current shader to nullptr
Definition ofMaterial.cpp:621
void setPBR(bool ab)
enable or disable PBR for this material. Default is disabled.
Definition ofMaterial.cpp:76
bool loadTexture(const ofMaterialTextureType &aMaterialTextureType, std::string apath)
load a texture that is stored in the material. Check which textures are PBR using isPBRTexture(ofMate...
Definition ofMaterial.cpp:209
void setTexture(const ofMaterialTextureType &aMaterialTextureType, const ofTexture &aTex)
set additonal textures to use in the shader. If texture is PBR only, will set material to PBR.
Definition ofMaterial.cpp:252
void setCustomUniform2i(const std::string &name, glm::vec< 2, int, glm::precision::defaultp > value)
Definition ofMaterial.cpp:1065
bool hasTexture(const ofMaterialTextureType &aMaterialTextureType) const
Definition ofMaterial.cpp:1178
int getHighestUniformTextureLocation() const
Definition ofMaterial.cpp:1167
void setCustomShader(std::shared_ptr< ofShader > aCustomShader)
set a custom shader controlled by the user.
Definition ofMaterial.cpp:1033
void setAoRoughnessMetallicTexture(const ofTexture &aTex)
set an occlusion, roughness, metallic texture. (PBR)
Definition ofMaterial.cpp:308
float getDisplacementNormalsStrength() const
Definition ofMaterial.cpp:491
void setClearCoatStrength(const float &astrength)
set clear coat strength. Must call setClearCoatEnabled(true) to take effect. (PBR)
Definition ofMaterial.cpp:391
bool isClearCoatEnabled() const
Definition ofMaterial.cpp:471
void setDiffuseTexture(const ofTexture &aTex)
set a diffuse texture. (Phong, PBR).
Definition ofMaterial.cpp:278
void setColors(ofFloatColor oDiffuse, ofFloatColor oAmbient, ofFloatColor oSpecular, ofFloatColor emissive)
set all material colors: reflectance type & light intensity. (Phong)
Definition ofMaterial.cpp:102
ofFloatColor getSpecularColor() const
Definition ofMaterial.cpp:511
void setRoughnessMetallicTexture(const ofTexture &aTex)
set a roughness, metallic texture. (PBR)
Definition ofMaterial.cpp:314
void setTexCoordScale(float xscale, float yscale)
set the tex coord scale used in the shader. (Phong, PBR)
Definition ofMaterial.cpp:200
void setShininess(float nShininess)
set the specular exponent. (Phong)
Definition ofMaterial.cpp:192
bool removeShaderDefine(const std::string &aDefineName)
Definition ofMaterial.cpp:1211
OF_DEPRECATED_MSG("Use setup(settings) instead", void setData(const ofMaterial::Data &data))
set the material color properties data struct
void setCustomUniform4f(const std::string &name, glm::vec4 value)
Definition ofMaterial.cpp:1055
bool removeCustomUniformTexture(const ofMaterialTextureType &aMaterialTextureType)
Definition ofMaterial.cpp:1152
void setShaderMain(std::string aShaderSrc, GLenum atype, std::string skey)
override the default main shader functions for vert or frag shader. (PBR)
Definition ofMaterial.cpp:132
void setAmbientColor(ofFloatColor oAmbient)
set the ambient reflectance. (Phong)
Definition ofMaterial.cpp:168
ofFloatColor getDiffuseColor() const
Definition ofMaterial.cpp:501
void setClearCoatEnabled(bool ab)
set clear coat. Disabled by default. (PBR)
Definition ofMaterial.cpp:374
const std::string getDefinesString() const
string holding all of the defines added to the shader. (Phong, PBR)
Definition ofMaterial.cpp:1229
float getNormalGeomToNormalMapMix() const
Definition ofMaterial.cpp:496
const bool isPBR() const
is this material pbr. Setting PBR functions or textures will automatically set the material to pbr....
Definition ofMaterial.h:177
void setOcclusionTexture(const ofTexture &aTex)
set an occlusion texture. (Phong, PBR)
Definition ofMaterial.cpp:303
void setDisplacementStrength(const float &astrength)
amount of vertex displacement. Must have set OF_MATERIAL_TEXTURE_DISPLACEMENT to take effect....
Definition ofMaterial.cpp:413
bool isPBRTexture(const ofMaterialTextureType &aMaterialTextureType)
check if texture is PBR only.
Definition ofMaterial.cpp:247
Definition ofShader.h:35
A wrapper class for an OpenGL texture.
Definition ofTexture.h:253
ofMaterialTextureType
Definition ofMaterial.h:13
@ OF_MATERIAL_TEXTURE_AMBIENT
Definition ofMaterial.h:17
@ OF_MATERIAL_TEXTURE_ROUGHNESS
Definition ofMaterial.h:23
@ OF_MATERIAL_TEXTURE_METALLIC
Definition ofMaterial.h:24
@ OF_MATERIAL_TEXTURE_CLEARCOAT_ROUGHNESS
Definition ofMaterial.h:27
@ OF_MATERIAL_TEXTURE_ROUGHNESS_METALLIC
Definition ofMaterial.h:22
@ OF_MATERIAL_TEXTURE_AO_ROUGHNESS_METALLIC
Definition ofMaterial.h:21
@ OF_MATERIAL_TEXTURE_DIFFUSE
Definition ofMaterial.h:15
@ OF_MATERIAL_TEXTURE_EMISSIVE
Definition ofMaterial.h:18
@ OF_MATERIAL_TEXTURE_CLEARCOAT
Definition ofMaterial.h:26
@ OF_MATERIAL_TEXTURE_CLEARCOAT_NORMAL
Definition ofMaterial.h:29
@ OF_MATERIAL_TEXTURE_SPECULAR
Definition ofMaterial.h:16
@ OF_MATERIAL_TEXTURE_DISPLACEMENT
Definition ofMaterial.h:25
@ OF_MATERIAL_TEXTURE_OCCLUSION
Definition ofMaterial.h:20
@ OF_MATERIAL_TEXTURE_NONE
Definition ofMaterial.h:14
@ OF_MATERIAL_TEXTURE_CLEARCOAT_INTENSITY_ROUGHNESS
Definition ofMaterial.h:28
@ OF_MATERIAL_TEXTURE_NORMAL
Definition ofMaterial.h:19
Definition ofMaterial.h:123
float reflectance
reflectance only applies to dielectric materials (non-metallic)
Definition ofMaterial.h:135
float displacementNormalsStrength
strength of normal calculation based on displacement map
Definition ofMaterial.h:140
std::string customUniforms
Definition ofMaterial.h:147
std::string mainVertex
access fragment main function with this key make unique for new instances
Definition ofMaterial.h:152
float clearCoatRoughness
Definition ofMaterial.h:138
glm::vec2 texCoordScale
Definition ofMaterial.h:143
bool clearCoatEnabled
this must be enabled for clear coat to have effect, will be enabled if clear coat texture set
Definition ofMaterial.h:136
ofFloatColor ambient
ambient reflectance
Definition ofMaterial.h:125
float roughness
roughness
Definition ofMaterial.h:134
std::string postFragment
Definition ofMaterial.h:146
float displacementStrength
strength of vertex displacement from displacement map in world space
Definition ofMaterial.h:139
std::string uniqueIdString
set by ofMaterial::setCustomUniform* not to be set manually
Definition ofMaterial.h:148
std::string mainFragment
set by ofMaterial to cache shader
Definition ofMaterial.h:149
ofFloatColor diffuse
diffuse reflectance
Definition ofMaterial.h:124
float normalGeomToNormalMapMix
if a normal map is set, the mix between the geometry normal and normal from the normal / displacement...
Definition ofMaterial.h:141
std::string mainVertexKey
override the default main function in the vertex shader
Definition ofMaterial.h:153
bool isPbr
Definition ofMaterial.h:130
float metallic
metallic properties, can be fraction between 0 -> 1
Definition ofMaterial.h:133
float clearCoatStrength
Definition ofMaterial.h:137
ofFloatColor specular
specular reflectance
Definition ofMaterial.h:126
ofFloatColor emissive
emitted light intensity
Definition ofMaterial.h:127
float shininess
specular exponent
Definition ofMaterial.h:128
std::string mainFragmentKey
override the default main function in the frag shader
Definition ofMaterial.h:150