SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLMaterial.h
Go to the documentation of this file.
1 /**
2  * \file SLMaterial.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLMATERIAL_H
11 #define SLMATERIAL_H
12 
13 #include <SLDrawBits.h>
14 #include <SLGLProgramGeneric.h>
15 #include <SLGLProgram.h>
16 #include <SLGLTexture.h>
17 #include <SLNode.h>
18 #include <SLParticleSystem.h>
19 
20 class SLSkybox;
21 class SLSceneView;
22 class SLAssetManager;
23 
24 //-----------------------------------------------------------------------------
25 //! Defines a standard CG material with textures and a shader program
26 /** @details The SLMaterial class defines a material with either the
27  Blinn-Phong (default) or the Cook-Torrance reflection) model.<br>
28  <br>
29  In the Blinn-Phong reflection model the following parameters get used:<br>
30  The ambient, diffuse, specular and emissive color as well as the shininess
31  parameter as shininess exponent. Instead of the diffuse color a texture of
32  type TT_diffuse can be used.<br>
33  In the Cook-Torrance reflection model only the diffuse color and the
34  roughness and metallic parameter are used. All parameters can be provided
35  also as a texture of the appropriate type. This reflection model
36  corresponds to the Physically Based Rendering (PBR) material model.<br>
37  In addition it has coefficients for reflectivity (kr), transparency (kt) and
38  refraction index (kn) that can be used in special shaders and ray tracing.<br>
39  A material has an array of empty vectors of SLGLTexture pointers. One for
40  each SLTextureType.<br>
41  The shading has to be implemented in the GLSL program (SLGLProgram) with a
42  vertex and fragment shader. If no SLGLProgram is assigned, a pair of
43  shaders (a vertex and fragment shader) gets automatically generated
44  according to the reflection model and the material parameters. See
45  SLGLProgramGenerated for more details.
46  * @remarks It is important that during instantiation NO OpenGL functions (gl*)
47  * get called because this constructor will be most probably called in a parallel
48  * thread from within an SLScene::registerAssetsToLoad or SLScene::assemble
49  * function. All objects that get rendered have to do their OpenGL initialization
50  * when they are used the first time during rendering in the main thread.
51  * For this material it means, that OpenGL shader programs in _programs
52  * and the textures in _textures do not get built until the first frame is
53  * rendered.
54 */
55 class SLMaterial : public SLObject
56 {
57 public:
58  //! Default ctor for Blinn-Phong reflection model materials without textures
59  explicit SLMaterial(SLAssetManager* am,
60  const SLchar* name,
61  const SLCol4f& amdi = SLCol4f::WHITE,
62  const SLCol4f& spec = SLCol4f::WHITE,
63  SLfloat shininess = 100.0f,
64  SLfloat kr = 0.0,
65  SLfloat kt = 0.0f,
66  SLfloat kn = 1.0f,
67  SLGLProgram* program = nullptr);
68 
69  //! Ctor for textured Blinn-Phong reflection model materials
71  const SLchar* name,
72  SLGLTexture* texture1,
73  SLGLTexture* texture2 = nullptr,
74  SLGLTexture* texture3 = nullptr,
75  SLGLTexture* texture4 = nullptr,
76  SLGLProgram* program = nullptr);
77 
78  //! Ctor for PBR material with Cook-Torrance material parameters
80  const SLchar* name,
85  SLGLProgram* program = nullptr);
86 
87  //! Ctor for PBR material with Cook-Torrance material textures
89  const SLchar* name,
91  SLGLTexture* texture1,
92  SLGLTexture* texture2 = nullptr,
93  SLGLTexture* texture3 = nullptr,
94  SLGLTexture* texture4 = nullptr,
95  SLGLTexture* texture5 = nullptr,
96  SLGLProgram* program = nullptr);
97 
98  //! Ctor for Particle System material with one texture (Draw and update)
100  const SLchar* name,
102  SLGLTexture* texture,
103  SLGLProgram* program = nullptr,
104  SLGLProgram* programTF = nullptr);
105 
106  //! Ctor for uniform color material without lighting
107  explicit SLMaterial(SLAssetManager* am,
108  SLGLProgram* colorUniformProgram,
109  const SLCol4f& uniformColor,
110  const SLchar* name = (const char*)"Uniform color");
111 
112  //! Ctor for only a program
114  const SLchar* name,
116 
117  ~SLMaterial() override;
118  void generateProgramPS(bool drawInstanced = false);
119  void activate(SLCamera* cam, SLVLight* lights, SLbool supportGPUSkinning);
121 
122  void deleteDataGpu();
123 
124  //! Returns true if there is any transparency in diffuse alpha or textures
126  {
127  if (_diffuse.a < 1.0)
128  return true;
129 
130  for (int i = 0; i < _textures[TT_diffuse].size(); i++)
131  {
132  if (_textures[TT_diffuse][i]->hasAlpha())
133  return true;
134  }
135  return false;
136  }
137 
138  //! Returns true if a material has a 3D texture
140  {
141  return !_textures3d.empty();
142  }
143  SLbool usesUVIndex(SLbyte uvIndex);
145  {
146  return !_textures[TT_normal].empty() &&
147  _textures[TT_normal][0]->target() == GL_TEXTURE_2D;
148  }
150  {
151  return !_textures[tt].empty();
152  }
154  {
155  return (_textures[tt].size() > texIndex &&
156  _textures[tt][texIndex]->uvIndex() == uvIndex);
157  }
158 
160  {
161  _numTextures -= (SLint)_textures[tt].size();
162  _textures[tt].clear();
163  }
164  void addTexture(SLGLTexture* texture);
166 
167  // Setters
170  void ambient(const SLCol4f& ambi) { _ambient = ambi; }
171  void diffuse(const SLCol4f& diff) { _diffuse = diff; }
172  void ambientDiffuse(const SLCol4f& am_di) { _ambient = _diffuse = am_di; }
173  void specular(const SLCol4f& spec) { _specular = spec; }
174  void emissive(const SLCol4f& emis) { _emissive = emis; }
175  void transmissive(const SLCol4f& transm) { _transmissive = transm; }
176  void translucency(SLfloat transl) { _translucency = transl; }
177  void shininess(SLfloat shin)
178  {
179  if (shin < 0.0f) shin = 0.0;
180  _shininess = shin;
181  }
182  void roughness(SLfloat r) { _roughness = Utils::clamp(r, 0.0f, 1.0f); }
183  void metalness(SLfloat m) { _metalness = Utils::clamp(m, 0.0f, 1.0f); }
184  void kr(SLfloat kr)
185  {
186  if (kr < 0.0f) kr = 0.0f;
187  if (kr > 1.0f) kr = 1.0f;
188  _kr = kr;
189  }
190  void kt(SLfloat kt)
191  {
192  if (kt < 0.0f) kt = 0.0f;
193  if (kt > 1.0f) kt = 1.0f;
194  _kt = kt;
195  _ambient.w = 1.0f - kt;
196  _diffuse.w = 1.0f - kt;
197  _specular.w = 1.0f - kt;
198  }
199  void kn(SLfloat kn)
200  {
201  assert(kn >= 0.0f);
202  _kn = kn;
203  }
204  void getsShadows(SLbool receivesShadows) { _getsShadows = receivesShadows; }
205  void program(SLGLProgram* sp) { _program = sp; }
206  void programTF(SLGLProgram* sp) { _programTF = sp; }
207  void skybox(SLSkybox* sb) { _skybox = sb; }
208  void ps(SLParticleSystem* ps) { _ps = ps; }
209 
210  // Getters
213  SLCol4f ambient() { return _ambient; }
214  SLCol4f diffuse() { return _diffuse; }
215  SLCol4f specular() { return _specular; }
216  SLCol4f emissive() { return _emissive; }
217  SLfloat shininess() const { return _shininess; }
218  SLfloat roughness() const { return _roughness; }
219  SLfloat metalness() const { return _metalness; }
221  SLfloat translucency() const { return _translucency; }
222  SLfloat kr() const { return _kr; }
223  SLfloat kt() const { return _kt; }
224  SLfloat kn() const { return _kn; }
225  SLbool getsShadows() const { return _getsShadows; }
227  SLGLProgram* program() { return _program; }
229  SLSkybox* skybox() { return _skybox; }
230  SLParticleSystem* ps() { return _ps; }
233  SLVGLTexture& textures(SLTextureType type) { return _textures[type]; }
235 
236  // Static variables & functions
237  static SLfloat K; //!< PM: Constant of gloss calibration (slope of point light at dist 1)
238  static SLfloat PERFECT; //!< PM: shininess/translucency limit
239 
240 protected:
241  SLAssetManager* _assetManager; //!< pointer to the asset manager (the owner) if available
242  SLReflectionModel _reflectionModel; //!< reflection model (RM_BlinnPhong or RM_CookTorrance)
243  SLCol4f _ambient; //!< ambient color (RGB reflection coefficients)
244  SLCol4f _diffuse; //!< diffuse color (RGB reflection coefficients)
245  SLCol4f _specular; //!< specular color (RGB reflection coefficients)
246  SLCol4f _emissive; //!< emissive color coefficients
247  SLfloat _shininess; //!< shininess exponent in Blinn-Phong model
248  SLfloat _roughness; //!< roughness property (0-1) in Cook-Torrance model
249  SLfloat _metalness; //!< metallic property (0-1) in Cook-Torrance model
250  SLCol4f _transmissive; //!< transmissive color (RGB reflection coefficients) for path tracing
251  SLfloat _translucency; //!< translucency exponent for light refraction for path tracing
252  SLfloat _kr{}; //!< reflection coefficient 0.0 - 1.0 used for ray and path tracing
253  SLfloat _kt{}; //!< transmission coefficient 0.0 - 1.0 used for ray and path tracing
254  SLfloat _kn{}; //!< refraction index
255  SLbool _getsShadows; //!< true if shadows are visible on this material
256  SLGLProgram* _program{}; //!< pointer to a GLSL shader program
257  SLGLProgram* _programTF{}; //!< pointer to a GLSL shader program for transformFeedback
258  SLint _numTextures; //!< number of textures in all _textures vectors array
259  SLSkybox* _skybox; //!< pointer to the skybox
260 
261  // For particle system
262  SLParticleSystem* _ps; //!< pointer to a particle system
263 
264  SLVGLTexture _textures[TT_numTextureType]; //!< array of texture vectors one for each type
265  SLVGLTexture _textures3d; //!< texture vector for diffuse 3D textures
266  SLGLTexture* _errorTexture = nullptr; //!< pointer to error texture that is shown if another texture fails
267  SLstring _compileErrorTexFilePath; //!< path to the error texture
268 
269  SLVNode _nodesVisible2D; //!< Vector of all visible 2D nodes of with this material
270  SLVNode _nodesVisible3D; //!< Vector of all visible 3D nodes of with this material
271 };
272 //-----------------------------------------------------------------------------
273 //! STL vector of material pointers
274 typedef vector<SLMaterial*> SLVMaterial;
275 //-----------------------------------------------------------------------------
276 //! Global default gray color material for meshes that don't define their own.
277 /*!
278  * Because the default material depends a default shader program
279  * (SLGLDefaultProgPerVrtBlinn or SLGLDefaultProgPerVrtBlinnTm) that itself
280  * depends on the scene configuration (e.g. the num. of lights) ist MUST be
281  * deleted at scene destruction.
282  */
284 {
285 public:
287  {
288  if (!_instance)
290  return _instance;
291  }
292  static void deleteInstance()
293  {
294  if (_instance)
295  {
296  delete _instance;
297  _instance = nullptr;
298  }
299  }
300 
301 private:
303  : SLMaterial(nullptr, "default", SLVec4f::GRAY, SLVec4f::WHITE)
304  {
305  ambient({0.2f, 0.2f, 0.2f});
306  }
307 
309 };
310 //-----------------------------------------------------------------------------
311 //! Global default color attribute material for meshes that have colors per vertex
313 {
314 public:
316  {
317  if (!_instance)
319  return _instance;
320  }
321  static void deleteInstance()
322  {
323  if (_instance)
324  {
325  delete _instance;
326  _instance = nullptr;
327  }
328  }
329 
330 private:
332  : SLMaterial(nullptr, "ColorAttribute")
333  {
335  }
336 
338 };
339 //-----------------------------------------------------------------------------
340 #endif
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
signed char SLbyte
Definition: SL.h:166
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLReflectionModel
Light Reflection Models for shader generation usd in SLMaterial.
Definition: SLEnums.h:288
@ SP_colorAttribute
SLTextureType
Texture type enumeration & their filename appendix for auto type detection.
Definition: SLGLTexture.h:76
@ TT_numTextureType
Definition: SLGLTexture.h:95
@ TT_normal
Definition: SLGLTexture.h:79
@ TT_diffuse
Definition: SLGLTexture.h:78
vector< SLGLTexture * > SLVGLTexture
STL vector of SLGLTexture pointers.
Definition: SLGLTexture.h:342
vector< SLLight * > SLVLight
STL vector of light pointers.
Definition: SLLight.h:232
vector< SLMaterial * > SLVMaterial
STL vector of material pointers.
Definition: SLMaterial.h:274
deque< SLNode * > SLVNode
SLVNode typedef for a vector of SLNodes.
Definition: SLNode.h:26
Toplevel holder of the assets meshes, materials, textures and shaders.
Active or visible camera node class.
Definition: SLCamera.h:54
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
static SLGLProgramGeneric * get(SLStdShaderProg id)
Get program reference for given id.
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
Global default color attribute material for meshes that have colors per vertex.
Definition: SLMaterial.h:313
static SLMaterialDefaultColorAttribute * _instance
Definition: SLMaterial.h:337
static SLMaterialDefaultColorAttribute * instance()
Definition: SLMaterial.h:315
Global default gray color material for meshes that don't define their own.
Definition: SLMaterial.h:284
static SLMaterialDefaultGray * _instance
Definition: SLMaterial.h:308
static void deleteInstance()
Definition: SLMaterial.h:292
static SLMaterialDefaultGray * instance()
Definition: SLMaterial.h:286
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
SLGLProgram * program()
Definition: SLMaterial.h:227
void ps(SLParticleSystem *ps)
Definition: SLMaterial.h:208
SLCol4f _emissive
emissive color coefficients
Definition: SLMaterial.h:246
SLCol4f _specular
specular color (RGB reflection coefficients)
Definition: SLMaterial.h:245
SLfloat shininess() const
Definition: SLMaterial.h:217
SLfloat _kt
transmission coefficient 0.0 - 1.0 used for ray and path tracing
Definition: SLMaterial.h:253
SLMaterial(SLAssetManager *am, const SLchar *name, const SLCol4f &amdi=SLCol4f::WHITE, const SLCol4f &spec=SLCol4f::WHITE, SLfloat shininess=100.0f, SLfloat kr=0.0, SLfloat kt=0.0f, SLfloat kn=1.0f, SLGLProgram *program=nullptr)
Default ctor for Blinn-Phong reflection model materials without textures.
Definition: SLMaterial.cpp:37
static SLfloat K
PM: Constant of gloss calibration (slope of point light at dist 1)
Definition: SLMaterial.h:237
void deleteDataGpu()
Definition: SLMaterial.cpp:376
SLbool getsShadows() const
Definition: SLMaterial.h:225
SLstring texturesString()
Returns a unique string that represent all textures used.
Definition: SLMaterial.cpp:687
void translucency(SLfloat transl)
Definition: SLMaterial.h:176
SLfloat _kr
reflection coefficient 0.0 - 1.0 used for ray and path tracing
Definition: SLMaterial.h:252
static SLfloat PERFECT
PM: shininess/translucency limit.
Definition: SLMaterial.h:238
void reflectionModel(SLReflectionModel rm)
Definition: SLMaterial.h:169
SLVNode & nodesVisible2D()
Definition: SLMaterial.h:231
SLVGLTexture _textures[TT_numTextureType]
array of texture vectors one for each type
Definition: SLMaterial.h:264
SLParticleSystem * ps()
Definition: SLMaterial.h:230
void specular(const SLCol4f &spec)
Definition: SLMaterial.h:173
SLCol4f diffuse()
Definition: SLMaterial.h:214
SLAssetManager * _assetManager
pointer to the asset manager (the owner) if available
Definition: SLMaterial.h:241
SLint passToUniforms(SLGLProgram *program, SLint nextTexUnit)
Passes all material parameters as uniforms to the passed shader program.
Definition: SLMaterial.cpp:560
SLVGLTexture _textures3d
texture vector for diffuse 3D textures
Definition: SLMaterial.h:265
SLfloat kr() const
Definition: SLMaterial.h:222
SLSkybox * skybox()
Definition: SLMaterial.h:229
SLbool has3DTexture()
Returns true if a material has a 3D texture.
Definition: SLMaterial.h:139
SLfloat _metalness
metallic property (0-1) in Cook-Torrance model
Definition: SLMaterial.h:249
void programTF(SLGLProgram *sp)
Definition: SLMaterial.h:206
void removeTextureType(SLTextureType tt)
Definition: SLMaterial.h:159
void skybox(SLSkybox *sb)
Definition: SLMaterial.h:207
SLGLProgram * _programTF
pointer to a GLSL shader program for transformFeedback
Definition: SLMaterial.h:257
SLfloat _roughness
roughness property (0-1) in Cook-Torrance model
Definition: SLMaterial.h:248
void diffuse(const SLCol4f &diff)
Definition: SLMaterial.h:171
SLVNode _nodesVisible3D
Vector of all visible 3D nodes of with this material.
Definition: SLMaterial.h:270
void generateProgramPS(bool drawInstanced=false)
Definition: SLMaterial.cpp:391
SLCol4f _ambient
ambient color (RGB reflection coefficients)
Definition: SLMaterial.h:243
SLCol4f _diffuse
diffuse color (RGB reflection coefficients)
Definition: SLMaterial.h:244
SLbool _getsShadows
true if shadows are visible on this material
Definition: SLMaterial.h:255
SLfloat roughness() const
Definition: SLMaterial.h:218
SLfloat metalness() const
Definition: SLMaterial.h:219
void assetManager(SLAssetManager *am)
Definition: SLMaterial.h:168
SLuint numTextures()
Definition: SLMaterial.h:226
void addTexture(SLGLTexture *texture)
Adds the passed texture to the equivalent texture type vector.
Definition: SLMaterial.cpp:348
SLbool hasTextureTypeWithUVIndex(SLTextureType tt, SLuint texIndex, SLbyte uvIndex)
Definition: SLMaterial.h:153
void activate(SLCamera *cam, SLVLight *lights, SLbool supportGPUSkinning)
Definition: SLMaterial.cpp:507
~SLMaterial() override
Definition: SLMaterial.cpp:367
SLbool hasAlpha()
Returns true if there is any transparency in diffuse alpha or textures.
Definition: SLMaterial.h:125
void kt(SLfloat kt)
Definition: SLMaterial.h:190
void shininess(SLfloat shin)
Definition: SLMaterial.h:177
void ambient(const SLCol4f &ambi)
Definition: SLMaterial.h:170
SLCol4f transmissive()
Definition: SLMaterial.h:220
void ambientDiffuse(const SLCol4f &am_di)
Definition: SLMaterial.h:172
SLCol4f ambient()
Definition: SLMaterial.h:213
SLfloat kn() const
Definition: SLMaterial.h:224
SLCol4f specular()
Definition: SLMaterial.h:215
SLAssetManager * assetManager()
Definition: SLMaterial.h:211
SLVNode _nodesVisible2D
Vector of all visible 2D nodes of with this material.
Definition: SLMaterial.h:269
SLVNode & nodesVisible3D()
Definition: SLMaterial.h:232
SLVGLTexture & textures(SLTextureType type)
Definition: SLMaterial.h:233
SLfloat kt() const
Definition: SLMaterial.h:223
SLCol4f _transmissive
transmissive color (RGB reflection coefficients) for path tracing
Definition: SLMaterial.h:250
SLbool needsTangents()
Definition: SLMaterial.h:144
SLParticleSystem * _ps
pointer to a particle system
Definition: SLMaterial.h:262
void transmissive(const SLCol4f &transm)
Definition: SLMaterial.h:175
SLGLProgram * _program
pointer to a GLSL shader program
Definition: SLMaterial.h:256
SLfloat translucency() const
Definition: SLMaterial.h:221
SLVGLTexture & textures3d()
Definition: SLMaterial.h:234
void kr(SLfloat kr)
Definition: SLMaterial.h:184
void roughness(SLfloat r)
Definition: SLMaterial.h:182
SLbool hasTextureType(SLTextureType tt)
Definition: SLMaterial.h:149
void emissive(const SLCol4f &emis)
Definition: SLMaterial.h:174
void kn(SLfloat kn)
Definition: SLMaterial.h:199
SLSkybox * _skybox
pointer to the skybox
Definition: SLMaterial.h:259
SLfloat _kn
refraction index
Definition: SLMaterial.h:254
SLbool usesUVIndex(SLbyte uvIndex)
Returns true if the specified uvIndex is used by one of the textures.
Definition: SLMaterial.cpp:707
SLReflectionModel _reflectionModel
reflection model (RM_BlinnPhong or RM_CookTorrance)
Definition: SLMaterial.h:242
void metalness(SLfloat m)
Definition: SLMaterial.h:183
void program(SLGLProgram *sp)
Definition: SLMaterial.h:205
SLGLTexture * _errorTexture
pointer to error texture that is shown if another texture fails
Definition: SLMaterial.h:266
void getsShadows(SLbool receivesShadows)
Definition: SLMaterial.h:204
SLint _numTextures
number of textures in all _textures vectors array
Definition: SLMaterial.h:258
SLfloat _shininess
shininess exponent in Blinn-Phong model
Definition: SLMaterial.h:247
SLGLProgram * programTF()
Definition: SLMaterial.h:228
SLCol4f emissive()
Definition: SLMaterial.h:216
SLstring _compileErrorTexFilePath
path to the error texture
Definition: SLMaterial.h:267
SLReflectionModel reflectionModel()
Definition: SLMaterial.h:212
SLfloat _translucency
translucency exponent for light refraction for path tracing
Definition: SLMaterial.h:251
Base class for all other classes.
Definition: SLObject.h:23
const SLstring & name() const
Definition: SLObject.h:38
SLParticleSystem creates a particle meshes from a point primitive buffer.
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29
T w
Definition: SLVec4.h:32
static SLVec4 WHITE
Definition: SLVec4.h:215
T a
Definition: SLVec4.h:33
T clamp(T a, T min, T max)
Definition: Utils.h:253