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