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