opennurbs_texture.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 ////////////////////////////////////////////////////////////////
18 //
19 // defines ON_Color and ON_Material
20 //
21 ////////////////////////////////////////////////////////////////
22 
23 #if !defined(OPENNURBS_TEXTURE_INC_)
24 #define OPENNURBS_TEXTURE_INC_
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // Class ON_Texture
29 //
30 
31 class ON_CLASS ON_Texture : public ON_Object
32 {
33  ON_OBJECT_DECLARE(ON_Texture);
34 
35 public:
36  ON_Texture() = default;
37  ~ON_Texture() = default;
38  ON_Texture(const ON_Texture&) = default;
39  ON_Texture& operator=(const ON_Texture&) = default;
40 
41 public:
42  static const ON_Texture Default;
43 
44  /*
45  Parameters:
46  original - [in]
47  A brep used as a picture object (1 face with a bitmap texture applied)
48  shrunk - [in]
49  A copy of original with the face's surface shrunk in some way.
50  The domain of the shrunk surface must be contained in the domain
51  of the original surface.
52  error_return - [in]
53  value to return if the input parameters are not valid. If error_return
54  is nullptr, then ON_Xform::Nan is returned when input parameters are not valid.
55 
56  Returns:
57  Texture transformation that will map a [0,1]x[0,1] to the appropriate
58  sub rectangle of the picture image.
59 
60  Example:
61  ON_Brep original_picture = ...;
62  ON_Brep shrunk_picture = original_picture;
63  shrunk_picture.ShrinkSurfaces();
64  ON_Material original_material = ...;
65  ON_Material new_material = original_material;
66  ON_Texture& new_texture = new_material.m_textures[0];
67  new_texture.m_uvw
68  = ON_Texture::GetPictureShrinkSurfaceTransformation(
69  &original_picture,
70  &shrunk_picture,
71  &ON_Xform::IdentityXform)*new_texture.m_uvw;
72  */
73  static ON_Xform GetPictureShrinkSurfaceTransformation(
74  const class ON_Brep* original,
75  const class ON_Brep* shrunk,
76  const ON_Xform* error_return
77  );
78 
79  /*
80  Parameters:
81  original - [in]
82  A surface used as a picture object (1 face with a bitmap texture applied)
83  shrunk - [in]
84  A surface trimmed/split from the original
85  error_return - [in]
86  value to return if the input parameters are not valid. If error_return
87  is nullptr, then ON_Xform::Nan is returned when input parameters are not valid.
88 
89  Returns:
90  Texture transformation that will map a [0,1]x[0,1] to the appropriate
91  sub rectangle of the picture image.
92 
93  Example:
94  ON_Brep original_picture = ...;
95  ON_Brep shrunk_picture = original_picture;
96  shrunk_picture.ShrinkSurfaces();
97  ON_Material original_material = ...;
98  ON_Material new_material = original_material;
99  ON_Texture& new_texture = new_material.m_textures[0];
100  new_texture.m_uvw
101  = ON_Texture::GetPictureShrinkSurfaceTransformation(
102  &original_picture,
103  &shrunk_picture,
104  &ON_Xform::IdentityXform)*new_texture.m_uvw;
105  */
106  static ON_Xform GetPictureShrinkSurfaceTransformation(
107  const class ON_Surface* original,
108  const class ON_Surface* shrunk,
109  const ON_Xform* error_return
110  );
111 
112  /*
113  Parameters:
114  original_udomain - [in]
115  original_vdomain - [in]
116  original picture surface domain
117  shrunk_udomain - [in]
118  shrunk_vdomain - [in]
119  sub-domain of the original picture.
120  error_return - [in]
121  value to return if the input parameters are not valid. If error_return
122  is nullptr, then ON_Xform::Nan is returned when input parameters are not valid.
123 
124  Returns:
125  Texture transformation that will map a [0,1]x[0,1] to the appropriate
126  sub rectangle of the picture image.
127 
128  Example:
129  ON_Brep original_picture = ...;
130  ON_Brep shrunk_picture = original_picture;
131  shrunk_picture.ShrinkSurfaces();
132  ON_Material original_material = ...;
133  ON_Material new_material = original_material;
134  ON_Texture& new_texture = new_material.m_textures[0];
135  new_texture.m_uvw
136  = ON_Texture::GetPictureShrinkSurfaceTransformation(
137  &original_picture,
138  &shrunk_picture,
139  &ON_Xform::IdentityXform)*new_texture.m_uvw;
140  */
141  static ON_Xform GetPictureShrinkSurfaceTransformation(
142  const class ON_Interval& original_udomain,
143  const class ON_Interval& original_vdomain,
144  const class ON_Interval& shrunk_udomain,
145  const class ON_Interval& shrunk_vdomain,
146  const ON_Xform* error_return
147  );
148 
149 public:
150  /////////////////////////////////////////////////////////////////
151  // ON_Object overrides
152  bool IsValid( class ON_TextLog* text_log = nullptr ) const override;
153 
154  void Dump( ON_TextLog& ) const override;
155 
156  unsigned int SizeOf() const override;
157 
158  bool Write(
159  ON_BinaryArchive& binary_archive
160  ) const override;
161 
162  bool Read(
163  ON_BinaryArchive& binary_archive
164  ) override;
165 
166  /////////////////////////////////////////////////////////////////
167  //
168 
169  static int Compare(
170  const ON_Texture& a,
171  const ON_Texture& b
172  );
173 
174  /*
175  Description:
176  Reverses the texture in the specified direction.
177  Parameters:
178  dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
179  Remarks:
180  Modifes m_uvw so that the spedified direction transforms
181  the texture coordinate t to 1-t.
182  Returns:
183  True if input is valid.
184  */
185  bool ReverseTextureCoordinate( int dir );
186 
187  /*
188  Description:
189  Swaps the specified texture coordinates.
190  Parameters:
191  i - [in]
192  j - [in] (0 <= i, j <= 3 and i != j)
193  Remarks:
194  Modifes m_uvw so that the specified texture coordinates are swapped.
195  Returns:
196  True if input is valid.
197  */
198  bool SwapTextureCoordinate( int i, int j );
199 
200  /*
201  Description:
202  Tiles the specified texture coordinates.
203  Parameters:
204  dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
205  count - [in] number of tiles (can be negative)
206  offset - [in] offset of the tile (can be any number)
207  Remarks:
208  Modifes m_uvw so that the specified texture coordinate is
209  tiled.
210  Returns:
211  True if input is valid.
212  */
213  bool TileTextureCoordinate( int dir, double count, double offset );
214 
215  /*
216  Description:
217  Examines the m_uvw matrix and harvests tiling constants.
218  Parameters:
219  dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
220  count - [out] number of tiles
221  offset - [out] offset of the tile
222  Returns:
223  True if if the m_uvw matrix had entries that were compatible
224  with tiling.
225  */
226  bool IsTiled( int dir, double* count, double* offset ) const;
227 
228 
229  ON_UUID m_texture_id = ON_nil_uuid;
230 
231  // list of pre-defined channel ids
232  enum class MAPPING_CHANNEL : unsigned int
233  {
234  tc_channel = 0U, // Use the texture coordinate values
235  // currently on the geometric object.
236  default_channel = 1U, // Use either default mappingU, or the "Custom" mapping applied to the object
237 
238  screen_based_channel = 0xFFFFFFF1U,
239  wcs_channel = 0xFFFFFFF2U,
240  wcs_box_channel = 0xFFFFFFF3U,
241  environment_map_box_channel = 0xFFFFFFF4U,
242  environment_map_light_probe_channel = 0xFFFFFFF5U,
243  environment_map_spherical_channel = 0xFFFFFFF6U,
244  environment_map_cube_map_channel = 0xFFFFFFF7U,
245  environment_map_vcross_cube_map_channel = 0xFFFFFFF8U,
246  environment_map_hcross_cube_map_channel = 0xFFFFFFF9U,
247  environment_map_hemispherical_channel = 0xFFFFFFFAU,
248  environment_map_emap_channel = 0xFFFFFFFFU,
249 
250  srfp_channel = 0xFFFFFFFEU, // Use surface parameterization.
251  emap_channel = 0xFFFFFFFFU // Environment map the geometric object - deprecated. Use environment_map_emap_channel instead
252  };
253 
254  static ON_Texture::MAPPING_CHANNEL BuiltInMappingChannelFromUnsigned(
255  unsigned int mapping_channel_as_unsigned
256  );
257  static bool IsBuiltInMappingChannel(
258  unsigned int mapping_channel_id
259  );
260 
261  const ON_SHA1_Hash ContentHash() const;
262 
263  // If the m_mapping_channel_id value is one of the built-in
264  // mappings listed in the MAPPING_CHANNEL enum, then that
265  // mapping is used. Otherwise, if an object has rendering
266  // attributes with an ON_MappingChannel entry that has a
267  // matching m_mapping_channel_id value, then the mapping
268  // identified by ON_MappingChannel::m_mapping_id is used.
269  // A value of zero means no mapping is supplied
270  // and the texture coordinates on the mesh are
271  // used.
272  void SetBuiltInMappingChannel(
273  ON_Texture::MAPPING_CHANNEL built_in_mapping_channel_as_unsigned
274  );
275  void SetMappingChannel(
276  unsigned int mapping_channel_id
277  );
278  unsigned int m_mapping_channel_id = 0;
279 
280  // Image file
281  // If m_image_file_reference is set and m_image_file_reference.FullPath()
282  // exists, it is the image file being used. This is the common situation
283  // during runtime.
284  ON_FileReference m_image_file_reference = ON_FileReference::Unset;
285 
286  // If false, texture is off and should be ignored.
287  // The intended use is to allow people to turn textures
288  // on and off without have to create/destroy or change
289  // other texture settings.
290  bool m_bOn = true;
291 
292  // do not change TYPE enum values - they are saved in 3dm files.
293  // The "TYPE" setting controls how the pixels in the bitmap
294  // are interpreted.
295  enum class TYPE : unsigned int
296  {
297  no_texture_type = 0U,
298 
299  bitmap_texture = 1U, // "standard" image texture.
300  bump_texture = 2U, // bump map - see m_bump_scale comment
301  transparency_texture = 3U, // value = alpha (see m_tranparancy_id)
302 
303  // emap_texture is OBSOLETE - set m_mapping_channel_id = ON_MappingChannel::emap_mapping
304  emap_texture = 86U // spherical environment mapping.
305  };
306 
307  static ON_Texture::TYPE TypeFromUnsigned(
308  unsigned int type_as_unsigned
309  );
310 
312 
313  // m_mode determines how the texture is
314  // do not change MODE enum values - they are saved in 3dm files.
315  enum class MODE : unsigned int
316  {
317  no_texture_mode = 0,
318  modulate_texture = 1, // modulate with material diffuse color
319  decal_texture = 2, // decal
320  blend_texture = 3, // blend texture with others in the material
321  // To "add" a texture, set m_blend_amount = +1
322  // To "subtract" a texture, set m_blend_amount = -1
323  };
324 
325  static ON_Texture::MODE ModeFromUnsigned(
326  unsigned int mode_as_unsigned
327  );
328 
330 
331  enum class FILTER : unsigned int
332  {
333  nearest_filter = 0, // nearest texture pixel is used
334  linear_filter = 1 // weighted average of corresponding texture pixels
335  };
336 
337  static ON_Texture::FILTER FilterFromUnsigned(
338  unsigned int filter_as_unsigned
339  );
340 
341  // The value of m_minfilter determines how the color
342  // of the image pixel is calculated when the image pixel
343  // corresponds to multiple texture bitmap pixels.
345 
346  // The magfilter setting controls how the color
347  // of the image pixel is calculated when the image pixel
348  // corresponds to a fraction of a texture bitmap pixel.
350 
351  enum class WRAP : unsigned int
352  {
353  repeat_wrap = 0,
354  clamp_wrap = 1
355  };
356 
357  static ON_Texture::WRAP WrapFromUnsigned(
358  unsigned int wrap_as_unsigned
359  );
360 
365  // Texture coordinate transformation.
367 
368  // If ON_UNSET_COLOR != m_border_color, then this color
369  // is used when the texture coordinates are <=0 or >=1
370  // and the m_wrap* value is clamp_wrap.
371  ON_Color m_border_color = ON_UNSET_COLOR;
372 
373  // This field is used for textures with type
374  // bitmap_texture that reference bitmap files that do
375  // not have an alpha channel and is used to set
376  // runtime alpha values. It needs to be parsed when the
377  // texture is loaded and can be ignored at runtime.
378  //
379  // If ON_UNSET_COLOR != m_transparent_color, then
380  // a pixel in the bitmap file with a matching RGB
381  // value is assigned the alpha value (ON_Color::Alpha)
382  // in m_transparent_color. The intended use is
383  // for non-rectangular decals defined by RGB bitmaps in
384  // files that don't save an alpha channel.
385  //
386  // For example if the decal is a red number 7 with a
387  // white background, then you would set m_transparent_color's
388  // RGB to white and its A to zero.
389  ON_Color m_transparent_color = ON_UNSET_COLOR;
390 
391  // This field is used for textures with type
392  // bitmap_texture that reference bitmap files that do
393  // not have an alpha channel and is used to set
394  // runtime alpha values. It needs to be parsed when the
395  // texture is loaded and can be ignored at runtime.
396  //
397  // If m_transparency_id is not nil, it is the id of another
398  // texture in the ON_Material.m_textures[] array that has
399  // type m_transparency_texture. The runtime bitmap_texture's
400  // alpha is set to (255-max(R,G,B)) (the "value" in the hue,
401  // saturation,value sense) of the correspondeing
402  // transparency_texture pixel.
403  //
404  // For example, if you had a bitmap texuture that was green
405  // with purple dots saved in a RGB .bmp file and you wanted
406  // the purple dots to be semi-transparent, you could create
407  // another bitmap that was black, where the original was green,
408  // and gray, where the original was purple, have an
409  // transparency_texture reference the white/gray bitmap,
410  // and have the bitmap_texture's m_transparency_id
411  // reference the transparency map.
412  ON_UUID m_transparency_texture_id = ON_nil_uuid;
413 
414  // If the m_type is bump_texture, the height of the
415  // bump is m_bump_scale.ParameterAt(value), where
416  // value is in the HSV sense and normalized
417  // (black=0, white=1). The interval can be
418  // decreasing.
419  ON_Interval m_bump_scale = ON_Interval::ZeroToOne;
420 
421  // If the m_mode is blend_texture, then m_blend_A[]
422  // and m_blend_RGB[] determine the blending function.
423  //
424  // new alpha = m_blend_constant_A
425  // + m_blend_A[0]*(current alpha)
426  // + m_blend_A[1]*(texture alpha)
427  // + m_blend_A[2]*min(current alpha,texture alpha)
428  // + m_blend_A[3]*max(current alpha,texture alpha)
429  // new rgb = m_blend_constant_RGB
430  // + m_blend_RGB[0]*(current RGB)
431  // + m_blend_RGB[1]*(texture RGB)
432  // + m_blend_RGB[2]*min(current RGB,texture RGB)
433  // + m_blend_RGB[3]*max(current RGB,texture RGB)
434  //
435  // Results are clamped to handle underflow or overflow.
436 
437  double m_blend_constant_A = 1.0;
438 
439  double m_blend_A0 = 1.0;
440  double m_blend_A1 = 1.0;
441  double m_blend_A2 = 0.0;
442  double m_blend_A3 = 0.0;
443 
444  ON_Color m_blend_constant_RGB = ON_Color::Black;
445 
446  double m_blend_RGB0 = 1.0;
447  double m_blend_RGB1 = 1.0;
448  double m_blend_RGB2 = 0.0;
449  double m_blend_RGB3 = 0.0;
451  // If an ON_Material m_textures[] array has more than
452  // one texture, the textures are blended, and the textures
453  // have different m_blend_order values, the the texture
454  // with the smaller m_blend_order is first.
455  int m_blend_order = 0;
456 };
458 #if defined(ON_DLL_TEMPLATE)
459 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_Texture>;
460 ON_DLL_TEMPLATE template class ON_CLASS ON_ObjectArray<ON_Texture>;
461 #endif
462 
463 #endif
464 
MODE
Definition: opennurbs_texture.h:317
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
MAPPING_CHANNEL
list of pre-defined channel ids
Definition: opennurbs_texture.h:234
static const ON_Interval ZeroToOne
Definition: opennurbs_point.h:50
Definition: opennurbs_sha1.h:19
Definition: opennurbs_color.h:24
virtual unsigned int SizeOf() const
static const ON_Xform IdentityTransformation
ON_Xform IdentityTransformation diagonal = (1,1,1,1)
Definition: opennurbs_xform.h:32
FILTER
Definition: opennurbs_texture.h:333
Definition: opennurbs_xform.h:28
virtual void Dump(ON_TextLog &) const
Creates a text dump of the object.
ON_Object & operator=(const ON_Object &)
WRAP
Definition: opennurbs_texture.h:353
virtual bool IsValid(class ON_TextLog *text_log=nullptr) const
Tests an object to see if its data members are correctly initialized.
Definition: opennurbs_brep.h:1472
Pure virtual base class for all classes that must provide runtime class id or support object level 3D...
Definition: opennurbs_object.h:460
TYPE
Definition: opennurbs_texture.h:297
Definition: opennurbs_textlog.h:20
Definition: opennurbs_archive.h:1783
virtual bool Read(ON_BinaryArchive &binary_archive)
Low level archive writing tool used by ON_BinaryArchive::ReadObject().
static const ON_FileReference Unset
Definition: opennurbs_file_utilities.h:1128
virtual bool Write(ON_BinaryArchive &binary_archive) const
Low level archive writing tool used by ON_BinaryArchive::WriteObject().
Definition: opennurbs_texture.h:31
Definition: opennurbs_surface.h:57
static const ON_Texture Default
Definition: opennurbs_texture.h:42
Definition: opennurbs_file_utilities.h:1125
Definition: opennurbs_point.h:46
static const ON_Color Black
Definition: opennurbs_color.h:33