HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TIL_TextureMap.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: UT library (C++)
7  *
8  * COMMENTS: There are two classes defined here. If you're using one,
9  * you'll use the other:
10  * TIL_TextureMap - a texture map. It knows how to lookup
11  * it's raster for you.
12  * TIL_TextureCache - The cache control specifies parameters
13  * used in the internal cache for
14  * texture maps
15  * This class is simply a verb class which
16  * accesses an internal class.
17  *
18  * The only way to get at the texture cache is through a
19  * TIL_TextureMap. This allows the cache to keep a good
20  * record of references. The rasters are loaded on demand
21  * which means that unless you look up a color, you don't
22  * load the raster.
23  *
24  */
25 
26 #ifndef _TIL_TextureMap_h_
27 #define _TIL_TextureMap_h_
28 
29 #include "TIL_API.h"
30 #include <UT/UT_Vector4.h>
31 #include <UT/UT_FilterType.h>
32 #include <UT/UT_String.h>
33 #include <UT/UT_Options.h>
34 #include <UT/UT_SharedPtr.h>
35 #include <IMG/IMG_FileTypes.h>
36 #include <IMG/IMG_TextureFilter.h>
37 
38 class TIL_Raster;
39 class TIL_TextureMap;
40 class TIL_Cdf2D;
41 class IMG_TTFilterInfo;
42 class IMG_File;
43 class IMG_FileTTMap;
44 class UT_Options;
45 class til_Map; // Internal structure
46 class til_Lookup; // Internal structure
47 class UT_Filter;
48 class UT_OpCaller;
49 
50 //
51 // An enum for bump map computation type
52 //
53 
55 {
62 };
63 
65 {
66  TIL_PTEX_COMPLEMENT_S = 0x01, // Complement the S coordinate
67  TIL_PTEX_COMPLEMENT_T = 0x02, // Complement the T coordinate
68  TIL_PTEX_SWAP_ST = 0x04, // Swap S/T coordinates
69 
70  // Orientation matching http://ptex.us
72 
73  // The following are based on experimental evidence and may not be correct
74  // Mudbox has the t coordinate flipped and the ST coordinats swapped
76 };
77 
79 {
80 public:
81  /// Callback for missing texture
83  virtual ~TIL_MissingTexture();
84 
85  /// Callback invoked when a texture can't be found. It's possible @c
86  /// filename may be a @c nullptr.
87  virtual void missingTexture(const char *filename) const {};
88 
89  /// Callback invoked when a texture is converted internally to a .rat
90  virtual void convertToRat(const char *filename) const {};
91 };
92 
94 public:
95  const char *myFilename; // Filename for the raster
96  int myXres, myYres; // Resolution
98  int myImageCount; // Image count...
100  int myOwnership; // If it's a COP, this will be 0
101 };
102 
104 public:
106  : myOptions(NULL)
107  , myShared()
108  { }
110  : myOptions(options)
111  , myShared()
112  { }
115  : myOptions(options)
116  , myShared(shared)
117  { }
119  : myOptions(opts.myOptions)
120  , myShared(opts.myShared)
121  { }
123 
125  {
126  if (&src != this)
127  {
128  myShared = src.myShared;
129  myOptions = src.myOptions;
130  }
131  return *this;
132  }
133 
134  const UT_Options *get() const { return myOptions; }
135 
136 private:
137  const UT_Options *myOptions;
138  UT_SharedPtr<UT_Options> myShared;
139 };
140 
141 //
142 // The texture cache control class
143 //
145 {
146 public:
147 //
148 // Limit the raster size of a texture map to the size specified
149  static int getMaxWidth();
150  static int getMaxHeight();
151  static void setMaxSize(int w=256, int h=256);
152 
153 //
154 // Limit the maximum number of maps in core at once
155  static int getCacheSize();
156  static void setCacheSize(int size=100);
157 
158 // Limit the maps to max amount of memory (in Mb)
159  static int64 getCacheMemSize();
160  static void setCacheMemSize(int64 sizeinmb);
161 
162 //
163 // Query methods for the cache.
164  static void getImageStat(UT_Array<TIL_TextureMapStat> &stats);
165 
166  // Simple method to get memory usage
167  static int64 getMemoryUsage();
168 
169  static void getConversionStats(exint &num_conversions,
170  exint &current_memory,
171  exint &peak_memory);
172 
173  static UT_StringHolder getOIIOStats(int level=1);
174 
175 //
176 // The version of the cache updates everytime something in it changes
177 // or is removed. This way we can clear display lists, etc.
178  static int getVersion();
179 
180 //
181 // Flush all maps out of core.
182  static void clearCache(int only_out_of_date=0);
183 
184 //
185 // Flush a map by name.
186  static void clearCache(const char *mapname);
187 
188 //
189 // Set auto-flushing of the cache. That is, as soon as the map is
190 // dereferenced completely, flush it from the cache. This can slow
191 // some applications down.
192  static int getAutoFlush();
193  static void setAutoFlush(int onOff);
194 
195 //
196 // Inform the cache of any deleted rasters that it may be referencing
197  static void rasterDied(TIL_Raster *raster);
198  static void imageDataDied(void *imagedata); // for COPs 1.
199 
200 //
201 // If a texture map is not a .rat file, exit with an error on load.
202  static void setNonRATError(bool on);
203  static bool getNonRATError();
204 };
205 
206 class til_PtexData;
207 class til_OIIOData;
208 
210 {
211 public:
213  : myLookup(0)
214  , myPtex()
215  , myOIIO()
216  {
217  }
219 
220  bool isPtex() const { return myPtex.get() != nullptr; }
221  bool isOIIO() const { return myOIIO.get() != nullptr; }
222 
223  const til_Lookup *lookup() const { return myLookup; }
224  const til_PtexData *ptexData() const { return myPtex.get(); }
225  const til_OIIOData *oiioData() const { return myOIIO.get(); }
226 
227 private:
228  TIL_TextureHandle(til_Lookup *lookup);
231 
232  til_Lookup *myLookup;
235  friend class TIL_TextureMap;
236 };
237 
239 {
240 public:
241  TIL_TextureMap(const char *mapname = 0, const char *relativeTo = 0,
242  TIL_Raster *rp = 0);
243 
244  virtual ~TIL_TextureMap();
245 
246  /// Returns whether a filename has UDIM formatting characters
247  static bool hasUDIM(const char *format);
248 
249  /// Expand a file containing %(UDIM)d, %(U)d or %(V)d formatting characters
250  /// The @c iu and @c iv parameters passed in are the zero-based coordinates
251  /// (i.e. UDIM == (1000 + iu+1+ 10*iv) and U == iu+1).
252  ///
253  /// For example: expandUDIM("%s/%s_%(UDIM)%d_%(U)02d_%(V)d.rat", 3, 7)
254  /// will return "%s/%s_1074_04_8.rat"
255  static UT_StringHolder expandUDIM(const char *format, int iu, int iv);
256 
257  /// Set the callback for missing textures. This returns the previous
258  /// callback.
259  static const TIL_MissingTexture *setMissingTexture(
260  const TIL_MissingTexture *m);
261 
262  void setOpCaller(UT_OpCaller *c) { myCaller = c; }
263  void setMap(const char *name, const char *relativeTo,
264  TIL_Raster *rp = 0, bool always_resolve = true);
265 
266  void clearRaster();
267  void forceReload();
268  const char *getMap() const;
269  const TIL_Raster *getOGLRaster();
270  TIL_TextureOptions getTextureOptions() const;
271  void getResolution(int &x, int &y)
272  { getResolution(getLookupHandle(), x, y); }
273  int getNChannels() const;
274 
275  // The color channel name may be NULL representing the default channel
276  void setColorChannelName(const char *name);
277  const char *getColorChannelName() const;
278 
279  // Set the face index
280  void setFaceIndex(int index);
281  int getFaceIndex() const { return myFaceIndex; }
282  // Set the subimage for OIIO lookup
283  void setSubimage(int i) { mySubimage = i; }
284  int getSubimage() const { return mySubimage; }
285  // Set the channel offset (offset into the tuple data)
286  void setPtexChannel(int index);
287  int getPtexChannel() const { return myPtexChannel; }
288 
289  // ptexture orientation
290  TIL_PtexOrient ptexOrient() const { return myPtexOrient; }
291  void setPtexOrient(TIL_PtexOrient o) { myPtexOrient = o; }
292 
293  void setFilter(UT_FilterType filter, float xw, float yw);
294  void setXFilter(UT_FilterType filter);
295  void setYFilter(UT_FilterType filter);
296  void setFilterWidths(float xwidth, float ywidth);
297  void setFilterBlurs(float xblur, float yblur);
298  void setMipInterpolate(int onoff);
299  void setWrapMode(UT_FilterWrap uwrap, UT_FilterWrap vwrap,
300  const UT_Vector4 &border);
301  void setDeepInterp(IMG_DeepInterp interp);
302  void setBeerLambert(bool beerlambert);
303  void setSourceColorSpace(IMG_ColorSpace convert);
304  void setFilterMode(IMG_TextureFilter::FilterMode m);
305  void setZSkew(float zskew);
306 
307 
308  const UT_Filter *getXFilter() const { return myFilter.getXFilter(); };
309  const UT_Filter *getYFilter() const { return myFilter.getYFilter(); };
310  float getXFilterWidth() const { return myFilter.getXWidth(); }
311  float getYFilterWidth() const { return myFilter.getYWidth(); }
312 
313 //
314 // Functions used to set the color lookup behaviour of the map
315  void setColorLookup(bool interp = true,
316  bool needalpha = true)
317  {
318  myInterpRequired = interp;
319  myAlphaRequired = needalpha;
320  }
321  void setBumpLookup(bool interp = false,
323  {
324  myInterpRequired = interp;
325  myBumpType = type;
326  }
327 
328  UT_FilterWrap getUWrap() const { return myFilter.getXWrap(); }
329  UT_FilterWrap getVWrap() const { return myFilter.getYWrap(); }
330  const UT_Vector4 &getBorder() const { return myFilter.getBorder(); }
331 
332  // If the texture failed to load, this method will tell us whether it
333  // is using the default lookup functionality.
334  bool isDefault() const;
335  int isTTMap() const;
336  int isDSM() const;
337  bool isPtex() const;
338  bool isOIIO() const;
339  int isIES() const;
340 
341 //
342 // Lookup a single sample of the image, interpreting the map as a color
343 // When specifying four corners of the map, the order of the coordinates
344 // is very important:
345 //
346 // BAD GOOD GOOD
347 // 2--3 3--2 2--3
348 // \/ | | | |
349 // /\ | | | |
350 // 0--1 0--1 1--0
351 
352  /// When performing multiple texture evaluations, this allows the texture
353  /// class to pre-compute some common values rather than performing the
354  /// computation on every lookup.
355  TIL_TextureHandle getLookupHandle() const;
356 
357  /// @{
358  /// Evaluate using an automatic lookup handle
359  template <typename T>
360  inline void colorLookup(float u, float v, UT_Vector4T<T> &clr) const
361  {
362  TIL_TextureHandle h = getLookupHandle();
363  colorLookup(h, u, v, clr);
364  }
365  template <typename T>
366  inline void colorLookup(const float u[4], const float v[4],
367  UT_Vector4T<T> &clr) const
368  {
369  TIL_TextureHandle h = getLookupHandle();
370  colorLookup(h, u, v, clr);
371  }
372  fpreal depthLookup(float u, float v,
373  float farvalue=1E6) const
374  {
375  TIL_TextureHandle h = getLookupHandle();
376  return depthLookup(h, u, v, farvalue);
377  }
378  template <typename T>
379  void bumpLookup(float u, float v,
380  T &du, T &dv) const
381  {
382  TIL_TextureHandle h = getLookupHandle();
383  bumpLookup(h, u, v, du, dv);
384  }
385 
386  template <typename T>
387  inline void envLookup(const UT_Vector3T<T> &R, fpreal angle,
388  UT_Vector4T<T> &clr) const
389  {
390  TIL_TextureHandle h = getLookupHandle();
391  envLookup(h, R, angle, clr);
392  }
393 
394  template <typename T>
395  inline void envLookup(const UT_Vector3T<T> &r00, const UT_Vector3T<T> &r10,
396  const UT_Vector3T<T> &r11, const UT_Vector3T<T> &r01,
397  UT_Vector4T<T> &clr) const
398  {
399  TIL_TextureHandle h = getLookupHandle();
400  envLookup(h, r00, r10, r11, r01, clr);
401  }
402  /// @}
403 
404  /// @{
405  /// Evaluate using a lookup handle
406  void colorLookup(TIL_TextureHandle &h,
407  float u, float v, UT_Vector4F &clr) const;
408  void colorLookup(TIL_TextureHandle &h,
409  float u, float v, UT_Vector4D &clr) const;
410  void colorLookup(TIL_TextureHandle &h,
411  const float u[4], const float v[4],
412  UT_Vector4F &clr) const;
413  void colorLookup(TIL_TextureHandle &h,
414  const float u[4], const float v[4],
415  UT_Vector4D &clr) const;
416  float depthLookup(TIL_TextureHandle &h,
417  float u, float v,
418  float farvalue=1E6) const;
419  void bumpLookup(TIL_TextureHandle &h,
420  float u, float v,
421  fpreal32 &du, fpreal32 &dv) const;
422  void bumpLookup(TIL_TextureHandle &h,
423  float u, float v,
424  fpreal64 &du, fpreal64 &dv) const;
425 
426  void envLookup(TIL_TextureHandle &h,
427  const UT_Vector3F &R, fpreal angle,
428  UT_Vector4F &clr) const;
429  void envLookup(TIL_TextureHandle &h,
430  const UT_Vector3D &R, fpreal angle,
431  UT_Vector4D &clr) const;
432 
433  void envLookup(TIL_TextureHandle &h,
434  const UT_Vector3F &r00, const UT_Vector3F &r10,
435  const UT_Vector3F &r11, const UT_Vector3F &r01,
436  UT_Vector4 &clr) const;
437  void envLookup(TIL_TextureHandle &h,
438  const UT_Vector3D &r00, const UT_Vector3D &r10,
439  const UT_Vector3D &r11, const UT_Vector3D &r01,
440  UT_Vector4D &clr) const;
441  void getResolution(const TIL_TextureHandle &h,
442  int &x, int &y);
443  /// @}
444 
445  /// @{
446  /// Deep shadow lookup will fall back to regular depth map lookups
447  /// if this map is not a deep shadow map.
448  void deepLookup(TIL_TextureHandle &h,
449  UT_Vector3F &clr,
450  const UT_Vector3F &p,
451  const UT_Vector3F &du,
452  const UT_Vector3F &dv,
453  float quality, int maxsamples);
454  void deepLookup(TIL_TextureHandle &h,
455  UT_Vector3D &clr,
456  const UT_Vector3D &p,
457  const UT_Vector3D &du,
458  const UT_Vector3D &dv,
459  float quality, int maxsamples);
460  /// @}
461 
462  /// Query filter information
463  const IMG_TextureFilter &getFilter() const { return myFilter; }
464  IMG_TextureFilter &getFilter() { return myFilter; }
465 
466  // Adjust scales should only be called internally.
468  {
469  return myBumpType;
470  }
471 
472  bool getInterpRequired() const { return myInterpRequired; }
473  bool getAlphaRequired() const { return myAlphaRequired; }
474 
475  // Returns true if the texture is volatile (ie, sourced from a COP). It
476  // needs to be checked every time it's accessed to se if it's been updated.
477  bool isVolatileTexture() const;
478 
479  // Returns true if a volatile texture has changed, and optionally returns
480  // the new raster in the newraster parm.
481  bool hasTextureChanged(const TIL_Raster **newraster = 0);
482 
483  // Return the IMG File used (for TT maps). This may contain filters.
484  IMG_File *getFile();
485 
486  // Returns the unfiltered TT map.
487  IMG_FileTTMap *getTTMap();
488 
489  /// Return a 2d CDF for sampling
490  const TIL_Cdf2D *getCDF(int maxres=512);
491 
492  // Look up the color channel index
493  int getColorChannelIndex(IMG_FileTTMap *map) const;
494 
495 private:
496  float shadowLookup(const UT_Vector3 &p,
497  const UT_Vector3 &du,
498  const UT_Vector3 &dv,
499  float quality, int maxsamples) const;
500 
501  // This version of the shadow lookup is used when there are no
502  // derivatives available (for non-shading contexts)
503  float shadowLookup(const UT_Vector3 &Ps,
504  float du, float dv,
505  float quality, int maxsamples) const;
506 
507  TIL_TextureHandle getPtexHandle() const;
508  TIL_TextureHandle getOIIOHandle() const;
509 
510 private:
511  til_Map *myMap;
512  UT_OpCaller *myCaller;
513  IMG_TextureFilter myFilter;
514  UT_String myColorChannel;
515 
516  int myPtexFilter;
517  int myFaceIndex;
518  int mySubimage;
519  int myPtexChannel;
520  TIL_BUMP_COMPUTE_TYPE myBumpType;
521  TIL_PtexOrient myPtexOrient;
522  bool myInterpRequired;
523  bool myAlphaRequired;
524 
525  friend class ut_Map;
526 };
527 
528 #endif
TIL_TextureOptions(const UT_Options *options)
int getVersion(int version)
Definition: ImfVersion.h:116
bool isPtex() const
GT_API const UT_StringHolder filename
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
GLuint const GLchar * name
Definition: glew.h:1814
bool isOIIO() const
UT_FilterWrap getVWrap() const
virtual void convertToRat(const char *filename) const
Callback invoked when a texture is converted internally to a .rat.
TIL_BUMP_COMPUTE_TYPE getBumpCompute() const
const til_PtexData * ptexData() const
const UT_Filter * getYFilter() const
GLuint index
Definition: glew.h:1814
void colorLookup(float u, float v, UT_Vector4T< T > &clr) const
void colorLookup(const float u[4], const float v[4], UT_Vector4T< T > &clr) const
float getXFilterWidth() const
TIL_PtexOrient ptexOrient() const
int64 exint
Definition: SYS_Types.h:125
TIL_MissingTexture()
Callback for missing texture.
UT_FilterWrap
Definition: UT_FilterType.h:40
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
int getFaceIndex() const
int getPtexChannel() const
const GLdouble * m
Definition: glew.h:9124
Tto convert(const Tfrom &source)
const GLdouble * v
Definition: glew.h:1391
3D Vector class.
GLdouble angle
Definition: glew.h:9135
UT_FilterType
Definition: UT_FilterType.h:16
float fpreal32
Definition: SYS_Types.h:200
fpreal depthLookup(float u, float v, float farvalue=1E6) const
void setPtexOrient(TIL_PtexOrient o)
int getSubimage() const
double fpreal64
Definition: SYS_Types.h:201
GLuint interp
Definition: glew.h:8277
const IMG_TextureFilter & getFilter() const
Query filter information.
IMG_DeepInterp
void setOpCaller(UT_OpCaller *c)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:2981
IMG_TextureFilter & getFilter()
float getYFilterWidth() const
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
TIL_BUMP_COMPUTE_TYPE
GLint GLint GLsizei GLsizei GLsizei GLint border
Definition: glew.h:1254
TIL_TextureOptions(const TIL_TextureOptions &opts)
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
TIL_TextureOptions(const UT_Options *options, UT_SharedPtr< UT_Options > shared)
TIL_PtexOrient
void setColorLookup(bool interp=true, bool needalpha=true)
const GLfloat * c
Definition: glew.h:16296
const UT_Vector4 & getBorder() const
long long int64
Definition: SYS_Types.h:116
const TIL_TextureOptions & operator=(const TIL_TextureOptions &src)
Generic interface for reading and writing image files. This class handles the reading and writing of ...
Definition: IMG_File.h:55
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
UT_FilterWrap getUWrap() const
const char * myFilename
void setSubimage(int i)
void getResolution(int &x, int &y)
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
Modes for filtering.
GLfloat GLfloat p
Definition: glew.h:16321
A map of string to various well defined value types.
Definition: UT_Options.h:84
bool getInterpRequired() const
const UT_Filter * getXFilter() const
fpreal64 fpreal
Definition: SYS_Types.h:277
void envLookup(const UT_Vector3T< T > &r00, const UT_Vector3T< T > &r10, const UT_Vector3T< T > &r11, const UT_Vector3T< T > &r01, UT_Vector4T< T > &clr) const
const til_OIIOData * oiioData() const
virtual void missingTexture(const char *filename) const
void envLookup(const UT_Vector3T< T > &R, fpreal angle, UT_Vector4T< T > &clr) const
IMG_ColorSpace
Modes for how to translate colors in images for texture lookups.
void bumpLookup(float u, float v, T &du, T &dv) const
#define TIL_API
Definition: TIL_API.h:10
void setBumpLookup(bool interp=false, TIL_BUMP_COMPUTE_TYPE type=TIL_BUMP_PRECOMPUTE)
bool getAlphaRequired() const
GLint level
Definition: glew.h:1252
type
Definition: core.h:528
const til_Lookup * lookup() const