HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_Material.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Render Library (C++)
7  *
8  * COMMENTS: Material definition for lit geometry
9  *
10  */
11 
12 #ifndef __RE_Material_h__
13 #define __RE_Material_h__
14 
15 #include "RE_API.h"
16 #include <DEP/DEP_MicroNode.h>
17 #include <SYS/SYS_AtomicInt.h>
18 #include <UT/UT_Color.h>
19 #include <UT/UT_IntArray.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_Options.h>
22 #include <UT/UT_Rect.h>
23 #include <UT/UT_String.h>
24 #include <UT/UT_UniquePtr.h>
25 #include <UT/UT_Vector3.h>
26 #include <UT/UT_ValArray.h>
27 #include <UT/UT_XformOrder.h>
28 #include "RE_Types.h"
29 #include "RE_TextureTypes.h"
30 #include "RE_TextureMap.h"
31 #include "RE_UniformBlock.h"
32 
33 class RE_OGLRender;
34 class RE_OGLTexture;
35 class RE_Render;
36 class RE_Shader;
37 class RE_Texture2DMap;
38 class RE_TextureMap;
39 class RE_TextureCubeMap;
40 
42 {
43 public:
44  /// This class is used to provide texture parameters for each texture layer
46  virtual ~RE_TextureCallback();
47 
49  {
64 
70 
72 
73  NUM_TEXMAP_TYPES
74  };
75 
76  virtual int getNumLayers() const { return 1; }
77  virtual int64 getVersion() const { return 0; }
78  virtual void updateAllSources(RE_Material *mat,
79  const UT_Options *opts,
80  fpreal time) {}
81  virtual RE_TextureMap *createTexture(RE_Render *r, int layer,
82  RE_TexMapType type,
83  UT_String &uvsetname);
84 
85  /// Returns the amount of main memory (NOT graphics memory!)
86  /// owned by this RE_TextureCallback.
87  virtual int64 getMemoryUsage(bool inclusive) const = 0;
88 
89  static bool fetchRegOrUDIM(const UT_StringRef &tex_name,
90  const UT_StringRef &node_path,
91  RE_TextureMap *&map,
92  const char *udim_marker = nullptr,
93  const char *uvtile_marker = nullptr,
94  bool allow_preload = false);
95 };
96 
98 {
99 public:
100  explicit RE_Material(const char *materialName);
101 
102  const UT_StringHolder &
103  getMaterialName() const { return myMaterialName; }
104  int getUniqueId() const { return myUniqueId; }
105 
106  int64 getVersion() const { return myVersion; }
107  void setVersion(int64 v) { myVersion = v; }
108 
109  void dirty(bool dirty = true) { myDirtyFlag = dirty; }
110  bool isDirty(fpreal time) const
111  {
112  if(myDirtyFlag)
113  return true;
114  if(myTimeDep && !SYSisEqual(time, myTime))
115  return true;
116  return false;
117  }
118 
119  void setTimeDependent(bool timedep, fpreal t = 0.0)
120  {
121  myTimeDep = timedep;
122  myTime = timedep ? t : 0.0;
123  }
124  bool isTimeDependent() const { return myTimeDep; }
125  fpreal getCurrentTime() const { return myTime; }
126 
127  void incref() { myRefCount.add(1); }
128  void decref()
129  {
130  if (!myRefCount.add(-1))
131  delete this;
132  }
133 
134  void setNodePath(UT_StringHolder path, int node_id);
135  const UT_StringHolder &
136  getNodePath() const { return myNodePath; }
137  int getNodeId() const { return myNodeID; }
138 
139  // Parameter overrides
140  void setOverrides(const UT_Options *overrides);
141  const UT_Options &getOverrides() const { return myOverrides; }
142  bool hasOverrides() const { return myOverrides.getNumOptions()>0; }
143 
144  // Vertex array overrides
146  { myVertexOverrides = ovr; }
148  { return myVertexOverrides; }
149 
151  {
155  TERRAIN_SURFACE
156  };
157  void setMaterialType(RE_MaterialType type);
158  RE_MaterialType getMaterialType() const { return myMaterialType; }
159 
160  void markDefaultShader(bool def) { myDefaultShader = def; }
161  bool isDefaultShader() const { return myDefaultShader; }
162 
163  void assignShader(RE_Shader *sh,
165  bool owned = true);
166  void assignShader(UT_UniquePtr<RE_Shader> &&sh,
168 
169  // methods to deal with textures
170  void clearTexture(RE_Render *r);
171  void clearDiffuseMap();
172 
173  void reserveDiffuseLayers(int nlayers);
174 
175  // General diffuse texture, 1 allowed per layer.
176  RE_TextureHolder getTexture(RE_Render *r,
177  int layer,
178  bool preload=false,
179  bool mat_changed = true);
180 
181  // Extra textures, only 1 allowed for the first layer.
182  RE_TextureHolder getSpecularMap(RE_Render *r, bool preload = false,
183  bool mat_change = true);
184  RE_TextureHolder getBumpMap(RE_Render *r, bool preload = false,
185  bool mat_change = true);
186  RE_TextureHolder getNormalMap(RE_Render *r, bool preload = false,
187  bool mat_change = true);
188  RE_TextureHolder getOpacityMap(RE_Render *r, bool preload = false,
189  bool mat_change = true);
190  RE_TextureHolder getDisplaceMap(RE_Render *r, bool preload = false,
191  bool mat_change = true);
192  RE_TextureHolder getEmissionMap(RE_Render *r, bool preload = false,
193  bool mat_change = true);
194  RE_TextureHolder getRoughnessMap(RE_Render *r, bool preload = false,
195  bool mat_change = true);
196  RE_TextureHolder getOcclusionMap(RE_Render *r, bool preload = false,
197  bool mat_change = true);
198  RE_TextureHolder getMetallicMap(RE_Render *r, bool preload = false,
199  bool mat_change = true);
200  RE_TextureHolder getCoatIntensityMap(RE_Render *r, bool preload = false,
201  bool mat_change = true);
202  RE_TextureHolder getCoatRoughnessMap(RE_Render *r, bool preload = false,
203  bool mat_change = true);
204  RE_TextureHolder getReflectionAmountMap(RE_Render *r, bool preload = false,
205  bool mat_change = true);
206 
207  RE_TextureHolder getHairDiffuseMap(RE_Render *r, bool preload = false);
208  RE_TextureHolder getHairSpecularMap(RE_Render *r, bool preload = false);
209  RE_TextureHolder getHairDiffuseRamp(RE_Render *r, bool preload = false);
210  RE_TextureHolder getHairSpecularRamp(RE_Render *r, bool preload = false);
211  RE_TextureHolder getHairAlphaRamp(RE_Render *r, bool preload = false);
212 
213  // Environment map to affect specular reflection. One allowed for layer 0.
214  RE_TextureHolder getEnvMap(RE_Render *r, bool preload = false);
215 
216  bool areTexturesPending() { return myTextureLoadPending; }
217 
218  void clearTextureLayer(int layer);
219 
220  // In order for these to return the correct value, the corresponding
221  // get..() method must be called first.
222  bool hasSpecularMap() const { return mySpecMap.myTex.isValid(); }
223  bool hasBumpMap() const { return myBumpMap.myTex.isValid(); }
224  bool hasNormalMap() const { return myNormalMap.myTex.isValid(); }
225  bool hasOpacityMap() const{ return myOpacityMap.myTex.isValid(); }
226  bool hasDisplaceMap() const{return myDisplaceMap.myTex.isValid();}
227  bool hasEnvMap() const { return myEnvMap.isValid(); }
228  bool hasEmissionMap() const
229  { return myEmissionMap.myTex.isValid(); }
230  bool hasRoughnessMap() const
231  { return myRoughMap.myTex.isValid(); }
232  bool hasOcclusionMap() const
233  { return myOcclusionMap.myTex.isValid(); }
234  bool hasMetallicMap() const
235  { return myMetallicMap.myTex.isValid(); }
236  bool hasCoatRoughnessMap() const
237  { return myCoatRoughMap.myTex.isValid(); }
238  bool hasCoatIntensityMap() const
239  { return myCoatIntMap.myTex.isValid(); }
241  { return myReflectAmountMap.myTex.isValid(); }
242 
243  bool hasHairDiffuseMap() const{ return myHairDiffMap.isValid(); }
244  bool hasHairSpecularMap() const{ return myHairSpecMap.isValid();}
245  bool hasHairDiffuseRamp() const{ return myHairDiffRamp.isValid();}
246  bool hasHairSpecularRamp() const{return myHairSpecRamp.isValid();}
247  bool hasHairAlphaRamp() const{ return myHairAlphaRamp.isValid(); }
248 
249  // Query the material if it is opaque or transparent. 'check_diffuse_alpha'
250  // will check the diffuse texture's alpha channel.
251  bool isTransparent(bool check_diffuse_alpha) const;
252 
253  // Returns true if this material has a texture map (diffuse, bump, etc).
254  bool isTextured() const { return myTexturedFlag; }
255 
256  // Returns true if textures are used, and the uvsets used by all layers of
257  // the material.
258  const UT_Map<UT_StringHolder, int> &getUVSets();
259 
260  // Return the texture coord set used by 'layer'
261  const UT_StringHolder &getLayerUVSet(int layer) const;
262 
263  // This allows the creator of this object to hook up a callback
264  // with which an instance of RE_Material could ask for a texture
265  // if and when the texture is needed. The presence of a callback
266  // marks this material as being textured, so only set when necessary.
267  void setCallback(RE_TextureCallback *cb);
268  RE_TextureCallback *getCallback() { return myTexCallback; }
269 
270  void setTextureFormat(RE_TextureCompress format);
271  RE_TextureCompress getTextureFormat() const { return myTexFormat; }
272 
273  void limitTextureSize(bool limit, int w, int h);
274  bool limitTextureSize() const { return myLimitTextureSize; }
275  void getMaxTextureSize(int &w,int &h)
276  { myMaxTextureW = w;
277  myMaxTextureH = h; }
278 
279  void setTextureScale(float sc);
280  float getTextureScale() const { return myTextureScale; }
281 
282  void setTextureMemLimitMB(int mem);
283  int getTextureMemLimitMB() const { return myTextureMemLimitMB; }
284 
285  void mipMapTextures(bool yes);
286  bool mipMapTextures() const { return myMipMapTextures; }
287 
288  void setAnisotropySamples(int samp);
289  int getAnisotropySamples() const { return myTextureAnisotropy; }
290 
291  void allowShaders(bool yes) { myAllowShaders = yes; }
292  bool allowShaders() const { return myAllowShaders; }
293 
294  // methods to deal with lighting
295  void doLighting(bool yes );
296  bool isLit() const { return myDoLighting; }
297 
298  RE_Shader *createShader(RE_Render *r,
299  const char *name = 0,
302  { return myShader[sht]; }
305  { return myOwnShader[sht]; }
306 
307  /// caches the source for the shader in the material.
308  void setShaderSource(const char *vert_src,
309  const char *geom_src,
310  const char *frag_src,
311  RE_ShaderTarget ptype
313 
314  /// checks this materials' shader against the source, and returns true if
315  /// it matches.
316  bool hasShaderSource(const char *vert_src,
317  const char *geom_src,
318  const char *frag_src,
319  RE_ShaderTarget ptype
321 
322  void setShaderProgram(const char *program_file);
323  bool hasShaderProgram(const char *program_file) const;
326  { return myShaderProgramFile.isstring() ||
327  myVertexSource[ptype].isstring() ||
328  myGeometrySource[ptype].isstring() ||
329  myFragmentSource[ptype].isstring(); }
330  bool customShaderDirty() const { return myCustomShaderDirty; }
331 
332  void removeShader();
333 
334  // methods to access and change the material information
335  float amb( unsigned idx ) const { return myAmb[idx]; }
336  UT_Vector3F amb() const { return myAmb; }
337  float diff( unsigned idx ) const { return myDiff[idx]; }
338  UT_Vector3F diff() const { return myDiff; }
339  float spec( unsigned idx ) const { return mySpec[idx]; }
340  UT_Vector3F spec() const { return mySpec; }
341  float emit( unsigned idx ) const { return myEmit[idx]; }
342  UT_Vector3F emit() const { return myEmit; }
343  UT_Vector3F metallicEdgeTint() const { return myMetallicEdge; }
344  float metallic() const { return myMetallic; }
345  float alpha() const { return myAlpha; }
346  float roughness() const { return myRoughness; }
347  float reflection() const { return myReflection; }
348  float coatRoughness() const { return myCoatRough; }
349  float coatIntensity() const { return myCoatIntensity; }
350  float diffuseRoughness() const { return myDiffRoughness; }
351 
352  /// Sets the perpendicular and parallel alpha factors. If the parallel
353  /// alpha is less than zero, they are assumed to be the same.
354  void setAlpha( float alpha_perp, float alpha_para = -1.0 );
355 
356  /// If true, the shader generates its own alpha independent of the material
357  /// parameters. This indicates that the material requires a transparency
358  /// pass, since the renderer can otherwise not determine this from the
359  /// shader code.
361  { myShaderAlphaFlag = enabled; }
362  bool hasShaderAlpha() const { return myShaderAlphaFlag; }
363 
364  // give roughness: this method will calculate shininess needed by OGL
365  void setRoughness( float r );
366 
367  // give shininess: this method will enforce sh within [0.001, 1e10]
368  void setShininess( float sh );
369 
370  void setDiffuseRoughness(fpreal r);
371 
372  void setAmb( float r, float g, float b );
373  void setDiff( float r, float g, float b );
374  void setSpec( float r, float g, float b );
375  void setEmit( float r, float g, float b );
376  void setMetallic(float metal);
377  void setMetallicEdge(const UT_Vector3F &metal);
378 
379  void setAmbient( UT_Color col );
380  void setDiffuse( UT_Color col );
381  void setSpecular( UT_Color col );
382  void setEmission( UT_Color col );
383 
384  void setAmbient( const UT_Vector3F &col );
385  void setDiffuse( const UT_Vector3F &col );
386  void setSpecular( const UT_Vector3F &col );
387  void setEmission( const UT_Vector3F &col );
388 
389  void setSpecularTint(fpreal t);
390  fpreal getSpecularTint() const { return mySpecTint; }
391 
392  enum { PHONG=0, BLINN=1, GGX=2, SPEC_ANGLE=3 };
393  void setSpecularModel(int spec_model);
394  int getSpecularModel() const { return mySpecModel; }
395  void setCoatSpecularModel(int spec_model);
396  int getCoatSpecuarModel() const { return myCoatModel; }
397  void setCoatIntensity(fpreal i);
398  fpreal getCoatIntensity() const { return myCoatIntensity; }
399  void setCoatRoughness(fpreal r);
400  fpreal getCoatRoughness() const { return myCoatRough; }
401 
402  void setIOR(float ior);
403  fpreal getIOR() const { return myIOR; }
404 
405  void setReflection(float r, bool explicit_r);
406  fpreal getReflection() const;
407 
408  void usePackedColor(bool enable);
409  void useGeometryColor(bool enable);
410  bool usePackedColor() const { return myUsePackedColor; }
411  bool useGeometryColor() const { return myUseGeoColor; }
412 
413  // returns true if reflection was explicity set, not derived from IoR.
414  bool hasExplicitReflection() const { return myExplicitReflection; }
415 
416  // Parameters for the bump, spec, opac, env, disp and normal maps.
417  enum re_NormalMapType { NMLMAP_TANGENT=0, NMLMAP_WORLD=1, NMLMAP_OBJECT=2 };
418 
419  void setOpacityMapParms(bool invert);
420  void setNormalParms(re_NormalMapType type, bool bias,
422  bool flip_x, bool flip_y, bool flip_z);
423  void setEnvParms(float scale, const UT_Vector3 &rot,
424  const UT_XformOrder &rord);
425 
426  enum re_Displace {
430  DISPLACE_VECTOR_OBJECT };
431  void setDisplaceParms(float scale, float offset,
432  re_Displace vector_space, bool y_up);
433  void setDisplaceLOD(float lod) { myDisplaceLOD = lod; }
434  float getDisplaceLOD() const { return myDisplaceLOD; }
435  void setRoughParms(bool invert_map, int component);
436  void setOcclusionParms(int component);
437  void setMetallicParms(int component);
438  void setCoatRoughnessParms(int component);
439  void setCoatIntensityParms(int component);
440  void setReflectionAmountParms(int component, bool affect_ior);
441 
442  void setUVTransform(RE_TextureCallback::RE_TexMapType maptype,
443  const UT_Matrix3F &xform,
444  int layer = 0);
445  void clearUVTransform(RE_TextureCallback::RE_TexMapType maptype,
446  int layer = 0);
447 
448  // Hair parameters
449  void enableHairParameters(bool e) { myHairMaterialEnabled = e; }
450 
451  void setHairDiffuse(float intensity,
452  UT_Vector3F root_col,
453  UT_Vector3F tip_col,
454  bool use_map,
455  bool use_ramp);
456 
457  void setHairSpecular(float intensity,
458  float angle,
459  float shift,
460  UT_Vector3F root_col,
461  UT_Vector3F tip_col,
462  bool use_map,
463  bool use_ramp);
464  void setHairAlpha(float alpha,
465  bool use_alpha_ramp);
466 
467  // randomize hue, sat, and intensity of diffuse (and specular intensity)
468  void setHairRandomize(bool random_hue, UT_Vector2F hue_range,
469  bool random_sat, UT_Vector2F sat_range,
470  bool random_int, UT_Vector2F int_range,
471  bool random_spec_int,
472  UT_Vector2F spec_int_range);
473 
474  // Game shader
475 
476  // Use a transformed UV set.
477  void setUVTransform(int layer,
478  UT_Vector2F t,
479  fpreal r,
480  UT_Vector2F s);
481  void getUVTransform(int layer,
482  UT_Vector2F &t,
483  fpreal &r,
484  UT_Vector2F &s ) const;
485  void setUVLayerTint(int layer, bool use_tint, UT_Vector4F tint);
486 
487  // Use triplanar projection.
488  void setUVTriplanar(int layer,
489  fpreal scale = 1.0,
490  fpreal sharp = 1.0,
493  bool isUVTriplanar(int layer) const;
494  fpreal getUVTriplanarSharp(int layer) const;
495  fpreal getUVTriplanarScale(int layer) const;
496  UT_Vector3F getUVTriplanarBlend(int layer) const;
497  UT_Vector3F getUVTriplanarOffset(int layer) const;
498  bool getUVLayerUseTint(int layer) const;
499  UT_Vector4F getUVLayerTint(int layer) const;
500 
501 
502  // For environment lights, which temporarily apply their map to any
503  // materials that don't already have an env map.
504  static void setEnvMapOverride(RE_TextureHolder env_map,
505  const UT_Matrix3F &rot,
506  const UT_Vector3F &col);
507  static void clearEnvMapOverride();
508  static RE_TextureHolder getEnvMapOverride();
509 
510  void setReflectionMap(RE_Texture *map) { myReflectMap = map; }
511  void clearReflectionMap() { myReflectMap = NULL; }
512 
513 #ifdef UT_DEBUG
514  // debug output
515  void output();
516 #endif
517 
518  RE_MaterialPtr cloneWithoutTexture() const;
519 
520  void loadTextureMaps(RE_Render *r, bool preload_only = false);
521 
522  // Maps can only be preloaded with loadTextureMaps(), not on the fly during
523  // the render.
524  void preloadMapsOnly(bool preload) { myPreloadMaps = preload; }
525 
526  // Adds an explicit map to 'maptype' which is not overriden by the
527  // RE_TextureCallback. NULL is acceptable (blocks the RE_TextureCB map).
528  void assignTextureMap(RE_Texture *tex,
530  int index = 0);
531  // Remove the explicit map from 'maptype', allowing RE_TextureCallback to
532  // be called for the map.
533  void removeTextureMap(RE_TextureCallback::RE_TexMapType maptype,
534  int index = 0);
535 
537  { return myMaterialType==FILM_SURFACE ?myNumDiffuseLayers :1; }
538 
539  bool updateShaderForMaterial(
540  RE_Render *r,
541  int layer,
542  bool lit,
543  bool textured,
545  RE_Shader *shader = NULL,
546  bool allow_partial = false);
547  void restoreShader(RE_Render *r,
549 
550  /// Returns the amount of main memory (NOT graphics memory!)
551  /// owned by this RE_Material.
552  int64 getMemoryUsage(bool inclusive) const;
553 
554 
555  void writeMaterialParmsToBlockGL4(RE_Render *r,
556  RE_UniformBlock *block,
557  int material_idx,
558  int layer,
559  bool lit,
560  bool textured);
561  // For bindless texturing.
562  void makeTexturesResident(RE_Render *r);
563  void removeTexturesFromResident(RE_Render *r);
564 
565  DEP_MicroNode &dependencies() { return myDepNode; }
566 
568  { return myMaterialBlock; }
570  { return myHairMaterialBlock; }
572  { return myGameMaterialBlock; }
573 private:
574 
575  void loadShaderAttribNames();
576 
577  class re_MaterialTextureMap
578  {
579  public:
580  re_MaterialTextureMap()
581  : myMap(NULL), myHasMap(0), myMapIndex(-1),
582  myArrayMapIndex(-1), myTexCoords(0), myComponent(0),
583  myExplicitMap(false) {}
584 
585  RE_TextureMap *myMap;
586  RE_TextureHolder myTex;
587  RE_CacheVersion myTexVersion;
588  UT_StringHolder myUVSet;
589  int myHasMap;
590  UT_DimRect myUDIMArea;
591  int myMapIndex;
592  int myArrayMapIndex;
593  int myTexCoords;
594  int myComponent;
595  bool myExplicitMap;
596  };
597  re_MaterialTextureMap *fetchMapInfo(RE_TextureCallback::RE_TexMapType t,
598  int index);
599 
600  RE_TextureHolder fetchTexture(RE_Render *r, RE_TextureMap *map,
601  bool preload_only = false,
602  bool mipmap = true);
603  RE_TextureHolder fetchMap(RE_Render *r,
605  re_MaterialTextureMap &map,
606  bool preload,
607  bool changed,
608  bool promote_1chan_to_rgb = false);
609  void setHasTexMap(re_MaterialTextureMap &map, bool has_map);
610 
611  bool checkForMap(re_MaterialTextureMap &map, bool textured);
612  bool bindTextureMap(RE_Render *r, re_MaterialTextureMap &map,
613  const char *udim_stride_name,
615  const char *component_name = NULL);
616  void bindUVTransform(RE_TextureCallback::RE_TexMapType type,
617  int layer = 0);
618 private:
619  ~RE_Material(); // No subclassing
620 
621  void updateHairMaterialForShader(RE_Render *r,
622  bool textured,
623  RE_ShaderTarget sht,
624  RE_Shader *sh);
625  void updateGameMaterialForShader(RE_Render *r,
626  bool textured,
627  RE_ShaderTarget sht,
628  RE_Shader *sh);
629 
630  // -- Bindless texturing support
631  void makeResident(RE_Render *r, RE_TextureHolder texmap);
632  void removeResident(RE_Render *r, RE_TextureHolder texmap);
633  bool bindTextureHandle(RE_Render *r,
634  re_MaterialTextureMap &map,
635  RE_UniformBlock *material_block,
636  int material_index,
637  const char *texture_name);
638  // --
639 
640  UT_Vector4F getComponentVector(int comp,
641  const re_MaterialTextureMap &map) const;
642 
643  DEP_MicroNode myDepNode;
644  RE_TextureCallback *myTexCallback;
645  UT_Vector3F myAmb;
646  UT_Vector3F myDiff;
647  UT_Vector3F mySpec;
648  UT_Vector3F myEmit;
649  UT_Vector3F myMetallicEdge;
650  float myMetallic;
651  float myAlpha;
652  float myRoughness;
653  float myDiffRoughness;
654  float mySpecTint;
655  RE_MaterialType myMaterialType;
656 
657  // I own map if if I created my RE_OGLTex
659 
660  RE_TextureHolder myEnvMap;
661  RE_Texture *myReflectMap; // always temporary during a render
662 
663  RE_TextureHolder myHairDiffMap;
664  RE_TextureHolder myHairSpecMap;
665  RE_TextureHolder myHairDiffRamp;
666  RE_TextureHolder myHairSpecRamp;
667  RE_TextureHolder myHairAlphaRamp;
668 
669  re_MaterialTextureMap mySpecMap;
670  re_MaterialTextureMap myOpacityMap;
671  re_MaterialTextureMap myBumpMap;
672  re_MaterialTextureMap myRoughMap;
673  re_MaterialTextureMap myEmissionMap;
674  re_MaterialTextureMap myNormalMap;
675  re_MaterialTextureMap myDisplaceMap;
676  re_MaterialTextureMap myOcclusionMap;
677  re_MaterialTextureMap myMetallicMap;
678  re_MaterialTextureMap myCoatRoughMap;
679  re_MaterialTextureMap myCoatIntMap;
680  re_MaterialTextureMap myReflectAmountMap;
681  re_MaterialTextureMap myGameBlendMap;
682 
683 
684  // The above maps are only valid pointers if myRenderCount is equal to
685  // RE_OGLRender::getRenderCount.
686  int64 myRenderCount;
687 
688 
689  UT_StringHolder myMaterialName;
690  UT_StringHolder myNodePath;
691  int myNodeID;
692  UT_Options myOverrides;
693  UT_StringMap<UT_StringHolder> myVertexOverrides;
694  unsigned myDoLighting:1,
695  myMipMapTextures:1,
696  myLimitTextureSize:1,
697  myDefaultShader:1,
698  myAllowShaders:1,
699  myTrivialTextureAlpha:1,
700  myInvertRoughMap:1,
701  myPreloadMaps:1,
702  myTextureLoadPending:1,
703  myCustomShaderDirty:1;
704  int myTextureAnisotropy;
705  int myNumDiffuseLayers;
706 
707  RE_TextureCompress myTexFormat;
708  int myMaxTextureW, myMaxTextureH;
709  float myTextureScale;
710  int myTextureMemLimitMB;
711  float myAlphaPara;
712  bool myShaderAlphaFlag;
713  bool myUseGeoColor;
714  bool myUsePackedColor;
715  bool myDirtyFlag;
716 
717  // Specular parms
718  fpreal myIOR;
719  fpreal32 myReflection;
720  int mySpecModel;
721  bool myReflectAsIOR;
722  bool myExplicitReflection;
723 
724  // Coat parms
725  fpreal myCoatRough;
726  fpreal myCoatIntensity;
727  int myCoatModel;
728 
729  // normal map parms
730  bool myNormalBias;
731  bool myNormalFlipX;
732  bool myNormalFlipY;
733  bool myNormalFlipZ;
734  UT_Vector2F myNormalScale;
735  re_NormalMapType myNormalMapType;
736 
737  // environment map parms
738  UT_Vector3 myEnvRotation;
739  float myEnvScale;
740  UT_XformOrder myEnvRotOrder;
741 
742  // displacement map parms
743  float myDisplaceScale;
744  float myDisplaceOffset;
745  re_Displace myDisplaceSpace;
746  bool myDisplaceYUp;
747  float myDisplaceLOD;
748 
749  bool myOpacityMapInvert;
750  int myRoughComp;
751  int myOcclusionComp;
752  int myMetallicComp;
753 
754  RE_Shader *myShader[RE_SHADER_NUM_TARGETS];
755  bool myOwnShader[RE_SHADER_NUM_TARGETS];
756  UT_String myVertexSource[RE_SHADER_NUM_TARGETS];
757  UT_String myGeometrySource[RE_SHADER_NUM_TARGETS];
758  UT_String myFragmentSource[RE_SHADER_NUM_TARGETS];
759  UT_String myShaderProgramFile;
760 
761  RE_UniformBlockHandle myMaterialBlock;
762  int myMaterialOverride;
763  bool myMatBlockDirty;
764  bool myMatBlockHasEnvMapTexture;
765  bool myMatBlockHasReflectMapTexture;
766  bool myMatBlockHasDiffuseTexture;
767  bool myTexturedFlag;
768 
769  // Hair parameters
770  class re_HairCompParms
771  {
772  public:
773  re_HairCompParms() : intensity(0.5), shift(0.0), angle(1.0),
774  root_col(1,1,1), tip_col(1,1,1),
775  use_ramp(false), use_map(false) {}
776  float intensity;
777  float angle;
778  float shift;
779  UT_Vector3F root_col;
780  UT_Vector3F tip_col;
781  bool use_ramp;
782  bool use_map;
783  };
784  struct re_HairRandomParms
785  {
786  re_HairRandomParms() : randomize(false) {}
787  bool randomize;
789  };
790 
791  bool myHairMaterialEnabled;
792  re_HairCompParms myHairDiffuse;
793  re_HairCompParms myHairSpecular;
794  float myHairAlpha;
795  bool myHairUseAlphaRamp;
796  re_HairRandomParms myHairRandomDiffHue;
797  re_HairRandomParms myHairRandomDiffSat;
798  re_HairRandomParms myHairRandomDiffInt;
799  re_HairRandomParms myHairRandomSpecInt;
800  RE_UniformBlockHandle myHairMaterialBlock;
801  int myHairMatBlockIndex;
802  int myHairRandomTextureIndex;
803  int myHairDiffBaseMapIndex;
804  UT_DimRect myHairDiffBaseMapUDIMArea;
805  UT_DimRect myHairSpecBaseMapUDIMArea;
806 
807  RE_TextureHolder myRandomTexture;
808 
809  // cached texture units for 'myLastMatShader'.
810  int myLastMatShader;
811  int myLastMatShaderSerial;
812 
813  int myMatBlockDiffuseIndex;
814  int myMatBlockDiffuseArrayIndex;
815 
816  int myMatBlockEnvMapIndex;
817  int myMatBlockReflectMapIndex;
818  int myMatBlockIndex;
819  int myHairMaterialOverride;
820 
821  // hair material block.
822  void *myLastHairShader;
823  int myLastHairShaderSerial;
824  int myHairDiffIndex;
825  int myHairDiffArrayIndex;
826  int myHairSpecIndex;
827  int myHairSpecArrayIndex;
828  int myHairDiffRampIndex;
829  int myHairSpecRampIndex;
830  int myHairAlphaRampIndex;
831  int myMatBlockHairIndex;
832 
833  // game shader material block
834  RE_UniformBlockHandle myGameMaterialBlock;
835  int myGameMaterialOverride;
836 
837  struct re_GameLayer
838  {
839  re_GameLayer() : triplanar(false), scale(1,1), trans(0,0), rot(0),
840  sharp(1), unit(-1), tpscale(1), transform(1.0),
841  tpblend(1,1,1), tpoff(0,0,0), use_tint(false),
842  tint(1.0, 1.0, 1.0, 1.0) {}
843  bool triplanar;
846  float rot;
847  float sharp;
848  float tpscale;
849  UT_Vector3F tpblend;
850  UT_Vector3F tpoff;
852  bool use_tint;
853  UT_Vector4F tint;
854 
855  // unit for shader
856  int unit;
857  };
858  UT_Array<re_GameLayer> myGameLayers;
859  const RE_Shader *myLastGameShader;
860  int myLastGameShaderSerial;
861  int myGameBlendIndex;
862  int myGameMatBlockIndex;
863 
864  int myPrevSeamlessCubeMap;
865 
866  bool myTimeDep;
867  fpreal myTime;
868 
869  SYS_AtomicInt32 myRefCount;
870  int64 myUniqueId;
871  int64 myVersion;
872  int64 myTexCallbackVersion;
873  UT_StringMap<int> myUVSets;
874  int64 myTexUVVersion;
875 
876  class re_UVTransform
877  {
878  public:
879  re_UVTransform() : transform(1.0), identity(true) {}
880  re_UVTransform(const UT_Matrix3F &t, bool i)
881  : transform(t), identity(i) {}
883  bool identity;
884  };
885  UT_Array<re_UVTransform> myTexUVTransform;
886  UT_Array<re_UVTransform> myDiffuseUVTransform;
887 };
888 
889 static inline void intrusive_ptr_add_ref(RE_Material *m) { m->incref(); }
890 static inline void intrusive_ptr_release(RE_Material *m) { m->decref(); }
891 
892 inline void
894 {
895  setAmb(col.x(), col.y(), col.z());
896 }
897 inline void
899 {
900  setDiff(col.x(), col.y(), col.z());
901 }
902 inline void
904 {
905  setSpec(col.x(), col.y(), col.z());
906 }
907 inline void
909 {
910  setEmit(col.x(), col.y(), col.z());
911 }
912 
913 
914 // This contains a list of materials for drawing a given geometry, and is
915 // normally used as a transient list of materials during a redraw.
916 // This class does not own the materials as they are also held in
917 // a higher-level cache as well.
918 class RE_API RE_MaterialAtlas : public UT_Map<int,RE_MaterialPtr>
919 {
920 public:
921  // Default material for unassigned prims.
923  { return myDefault; }
924 
925  // Factory default material if materials are disabled.
927  { return myFactory; }
928 
929  bool isDirty(fpreal time) const;
930 
931  RE_MaterialPtr get(int material_id) const
932  {
933  if(material_id == -1)
934  return myDefault ? myDefault : myFactory;
935  const_iterator entry = find(material_id);
936  if(entry != end())
937  return entry->second;
938 
939  return NULL;
940  }
941 
942 
943  // Add a new material to the atlas
944  void add(const RE_MaterialPtr &material)
945  { (*this)[ material->getUniqueId() ] = material; }
946 
947  // Set the default material, which will be used if no other material
948  // assignments are present.
949  void setDefaultMaterial(const RE_MaterialPtr &material,bool add_list=true)
950  { myDefault = material; if(add_list && material) add(material); }
951 
952  // Sets the factory default material, which is used when all materials
953  // are turned off (as the default material may be the object material).
954  void setFactoryMaterial(const RE_MaterialPtr &material,bool add_list=true)
955  { myFactory = material; if(add_list && material) add(material); }
956 
957 private:
958  RE_MaterialPtr myDefault;
959  RE_MaterialPtr myFactory;
960 };
961 
962 #endif
GLdouble s
Definition: glew.h:1390
void setDiffuse(UT_Color col)
bool mipMapTextures() const
Definition: RE_Material.h:286
RE_TextureCallback * getCallback()
Definition: RE_Material.h:268
GLenum GLint * range
Definition: glew.h:3500
UT_Vector3F spec() const
Definition: RE_Material.h:340
bool hasOverrides() const
Definition: RE_Material.h:142
GLboolean invert
Definition: glew.h:1422
virtual int64 getVersion() const
Definition: RE_Material.h:77
bool hasHairDiffuseMap() const
Definition: RE_Material.h:243
float alpha() const
Definition: RE_Material.h:345
void setVersion(int64 v)
Definition: RE_Material.h:107
float getDisplaceLOD() const
Definition: RE_Material.h:434
bool hasOpacityMap() const
Definition: RE_Material.h:225
GLuint const GLchar * name
Definition: glew.h:1814
int64 getVersion() const
Definition: RE_Material.h:106
float reflection() const
Definition: RE_Material.h:347
Unsorted map container.
Definition: UT_Map.h:83
const UT_StringHolder & getNodePath() const
Definition: RE_Material.h:136
void dirty(bool dirty=true)
Definition: RE_Material.h:109
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1520
void incref()
Definition: RE_Material.h:127
#define RE_API
Definition: RE_API.h:10
bool isTimeDependent() const
Definition: RE_Material.h:124
GLuint GLenum GLenum transform
Definition: glew.h:14742
bool hasBumpMap() const
Definition: RE_Material.h:223
GT_API const UT_StringHolder time
GLuint index
Definition: glew.h:1814
UT_Vector3F diff() const
Definition: RE_Material.h:338
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:13880
float roughness() const
Definition: RE_Material.h:346
const RE_UniformBlockHandle & hairBlock() const
Definition: RE_Material.h:569
GA_API const UT_StringHolder rot
void setEmit(float r, float g, float b)
float metallic() const
Definition: RE_Material.h:344
int getSpecularModel() const
Definition: RE_Material.h:394
bool areTexturesPending()
Definition: RE_Material.h:216
void setShaderAlpha(bool enabled)
Definition: RE_Material.h:360
bool hasShaderAlpha() const
Definition: RE_Material.h:362
const RE_MaterialPtr & getDefaultMaterial() const
Definition: RE_Material.h:922
void decref()
Definition: RE_Material.h:128
GLint limit
Definition: glew.h:12988
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
void setSpecular(UT_Color col)
bool hasCoatIntensityMap() const
Definition: RE_Material.h:238
const UT_Options & getOverrides() const
Definition: RE_Material.h:141
const GLdouble * m
Definition: glew.h:9124
void setSpec(float r, float g, float b)
const GLdouble * v
Definition: glew.h:1391
bool hasRoughnessMap() const
Definition: RE_Material.h:230
GLuint shader
Definition: glew.h:1813
fpreal getCurrentTime() const
Definition: RE_Material.h:125
int getTextureMemLimitMB() const
Definition: RE_Material.h:283
const UT_StringMap< UT_StringHolder > & vertexOverrides() const
Definition: RE_Material.h:147
GLdouble angle
Definition: glew.h:9135
void setAmb(float r, float g, float b)
float fpreal32
Definition: SYS_Types.h:200
int getAnisotropySamples() const
Definition: RE_Material.h:289
bool isShaderOwned(RE_ShaderTarget sht=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:303
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
virtual int getNumLayers() const
Definition: RE_Material.h:76
vint4 blend(const vint4 &a, const vint4 &b, const vbool4 &mask)
Definition: simd.h:4648
RE_MaterialType getMaterialType() const
Definition: RE_Material.h:158
bool hasHairSpecularRamp() const
Definition: RE_Material.h:246
float amb(unsigned idx) const
Definition: RE_Material.h:335
DEP_MicroNode & dependencies()
Definition: RE_Material.h:565
void setFactoryMaterial(const RE_MaterialPtr &material, bool add_list=true)
Definition: RE_Material.h:954
const RE_UniformBlockHandle & materialBlock() const
Definition: RE_Material.h:567
SYS_FORCE_INLINE T & y()
Definition: UT_Vector3.h:513
void intrusive_ptr_release(T *x)
Definition: refcnt.h:233
bool hasDisplaceMap() const
Definition: RE_Material.h:226
GLclampf f
Definition: glew.h:3499
int getCoatSpecuarModel() const
Definition: RE_Material.h:396
const RE_UniformBlockHandle & gameMatBlock() const
Definition: RE_Material.h:571
GA_API const UT_StringHolder trans
bool hasHairDiffuseRamp() const
Definition: RE_Material.h:245
bool hasReflectionAmountMap() const
Definition: RE_Material.h:240
bool hasNormalMap() const
Definition: RE_Material.h:224
void setReflectionMap(RE_Texture *map)
Definition: RE_Material.h:510
bool usePackedColor() const
Definition: RE_Material.h:410
bool limitTextureSize() const
Definition: RE_Material.h:274
SYS_FORCE_INLINE T & z()
Definition: UT_Vector3.h:515
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
fpreal getIOR() const
Definition: RE_Material.h:403
GLuint GLuint end
Definition: glew.h:1253
void setTimeDependent(bool timedep, fpreal t=0.0)
Definition: RE_Material.h:119
bool customShaderDirty() const
Definition: RE_Material.h:330
bool hasMetallicMap() const
Definition: RE_Material.h:234
long long int64
Definition: SYS_Types.h:116
png_const_structrp png_const_inforp int * unit
Definition: png.h:2161
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
GLfloat bias
Definition: glew.h:10274
virtual void updateAllSources(RE_Material *mat, const UT_Options *opts, fpreal time)
Definition: RE_Material.h:78
void setEmission(UT_Color col)
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
bool isLit() const
Definition: RE_Material.h:296
fpreal getCoatIntensity() const
Definition: RE_Material.h:398
bool hasOcclusionMap() const
Definition: RE_Material.h:232
void markDefaultShader(bool def)
Definition: RE_Material.h:160
int getUniqueId() const
Definition: RE_Material.h:104
bool hasSpecularMap() const
Definition: RE_Material.h:222
bool allowShaders() const
Definition: RE_Material.h:292
void enableHairParameters(bool e)
Definition: RE_Material.h:449
float diff(unsigned idx) const
Definition: RE_Material.h:337
void setDefaultMaterial(const RE_MaterialPtr &material, bool add_list=true)
Definition: RE_Material.h:949
UT_Vector3T< fpreal32 > UT_Vector3F
GLsizei const GLchar *const * path
Definition: glew.h:6461
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
RE_Shader * getShader(RE_ShaderTarget sht=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:301
float emit(unsigned idx) const
Definition: RE_Material.h:341
float coatRoughness() const
Definition: RE_Material.h:348
A map of string to various well defined value types.
Definition: UT_Options.h:84
float diffuseRoughness() const
Definition: RE_Material.h:350
bool hasCoatRoughnessMap() const
Definition: RE_Material.h:236
float spec(unsigned idx) const
Definition: RE_Material.h:339
float coatIntensity() const
Definition: RE_Material.h:349
void intrusive_ptr_add_ref(T *x)
Definition: refcnt.h:224
void setDiff(float r, float g, float b)
GLenum GLsizei const GLuint GLboolean enabled
Definition: glew.h:2579
void clearReflectionMap()
Definition: RE_Material.h:511
bool hasEnvMap() const
Definition: RE_Material.h:227
const RE_MaterialPtr & getFactoryMaterial() const
Definition: RE_Material.h:926
bool useGeometryColor() const
Definition: RE_Material.h:411
void setVertexOverrides(const UT_StringMap< UT_StringHolder > &ovr)
Definition: RE_Material.h:145
UT_Vector3F metallicEdgeTint() const
Definition: RE_Material.h:343
fpreal64 fpreal
Definition: SYS_Types.h:277
GLint lod
Definition: glew.h:1385
bool hasHairAlphaRamp() const
Definition: RE_Material.h:247
void setDisplaceLOD(float lod)
Definition: RE_Material.h:433
bool hasEmissionMap() const
Definition: RE_Material.h:228
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2104
void preloadMapsOnly(bool preload)
Definition: RE_Material.h:524
RE_TextureCompress getTextureFormat() const
Definition: RE_Material.h:271
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
GLint GLboolean GLint layer
Definition: glew.h:3601
void setAmbient(UT_Color col)
UT_Vector3F amb() const
Definition: RE_Material.h:336
const UT_StringHolder & getMaterialName() const
Definition: RE_Material.h:103
SYS_FORCE_INLINE T & x()
Definition: UT_Vector3.h:511
int getNodeId() const
Definition: RE_Material.h:137
Simple class for a mutli-integer cache tag.
UT_Vector3F emit() const
Definition: RE_Material.h:342
void add(const RE_MaterialPtr &material)
Definition: RE_Material.h:944
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void getMaxTextureSize(int &w, int &h)
Definition: RE_Material.h:275
RE_ShaderTarget
Definition: RE_Types.h:250
bool hasExplicitReflection() const
Definition: RE_Material.h:414
RE_TextureCompress
bool isDirty(fpreal time) const
Definition: RE_Material.h:110
fpreal getCoatRoughness() const
Definition: RE_Material.h:400
Base::const_iterator const_iterator
Definition: UT_Map.h:94
GLboolean enable
Definition: glew.h:2745
GLdouble GLdouble t
Definition: glew.h:1398
bool isTextured() const
Definition: RE_Material.h:254
int getNumDiffuseLayers() const
Definition: RE_Material.h:536
bool hasHairSpecularMap() const
Definition: RE_Material.h:244
float getTextureScale() const
Definition: RE_Material.h:280
bool isDefaultShader() const
Definition: RE_Material.h:161
fpreal getSpecularTint() const
Definition: RE_Material.h:390
void allowShaders(bool yes)
Definition: RE_Material.h:291
GLboolean GLboolean g
Definition: glew.h:9477
bool hasCustomShader(RE_ShaderTarget ptype=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:324
GLintptr offset
Definition: glew.h:1682