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