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 public:
34   ON_OBJECT_DECLARE(ON_Texture);
35 
36   ON_Texture();
37   ~ON_Texture();
38 
39   // default copy constructor and operator= work fine
40 
41 
42   // overrides virtual ON_Object::IsValid
43   ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
44 
45   // overrides virtual ON_Object::Dump
46   void Dump( ON_TextLog& ) const;
47 
48   // overrides virtual ON_Object::SizeOf
49   unsigned int SizeOf() const;
50 
51   // overrides virtual ON_Object::Write
52   ON_BOOL32 Write(
53          ON_BinaryArchive& binary_archive
54        ) const;
55 
56   // overrides virtual ON_Object::Read
57   ON_BOOL32 Read(
58          ON_BinaryArchive& binary_archive
59        );
60 
61   void Default();
62 
63   int Compare( const ON_Texture& other ) const;
64 
65   /*
66   Description:
67     Reverses the texture in the specified direction.
68   Parameters:
69     dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
70   Remarks:
71     Modifes m_uvw so that the spedified direction transforms
72     the texture coordinate t to 1-t.
73   Returns:
74     True if input is valid.
75   */
76   bool ReverseTextureCoordinate( int dir );
77 
78   /*
79   Description:
80     Swaps the specified texture coordinates.
81   Parameters:
82     i - [in]
83     j - [in]  (0 <= i, j <= 3 and i != j)
84   Remarks:
85     Modifes m_uvw so that the specified texture coordinates are swapped.
86   Returns:
87     True if input is valid.
88   */
89   bool SwapTextureCoordinate( int i, int j );
90 
91   /*
92   Description:
93     Tiles the specified texture coordinates.
94   Parameters:
95     dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
96     count - [in] number of tiles (can be negative)
97     offset - [in] offset of the tile (can be any number)
98   Remarks:
99     Modifes m_uvw so that the specified texture coordinate is
100     tiled.
101   Returns:
102     True if input is valid.
103   */
104   bool TileTextureCoordinate( int dir, double count, double offset );
105 
106   /*
107   Description:
108     Examines the m_uvw matrix and harvests tiling constants.
109   Parameters:
110     dir - [in] 0 = reverse "u", 1 = reverse "v", 2 = reverse "w".
111     count - [out] number of tiles
112     offset - [out] offset of the tile
113   Returns:
114     True if if the m_uvw matrix had entries that were compatible
115     with tiling.
116   */
117   bool IsTiled( int dir, double* count, double* offset ) const;
118 
119 
120   ON_UUID m_texture_id;
121 
122   // list of pre-defined channel ids
123   enum MAPPING_CHANNEL
124   {
125     tc_channel      = 0,     // Use the texture coordinate values
126                              // currently on the geometric object.
127     default_channel = 1,	   // Use either default mapping, or the "Custom"
128 							               // mapping applied to the object
129     srfp_channel = 0xFFFFFFFE, // Use surface parameterization.
130     emap_channel = 0xFFFFFFFF  // Environment map the geometric object.
131   };
132 
133   // If the m_mapping_channel_id value is one of the built-in
134   // mappings listed in the MAPPING_CHANNEL enum, then that
135   // mapping is used.  Otherwise, if an object has rendering
136   // attributes with an ON_MappingChannel entry that has a
137   // matching m_mapping_channel_id value, then the mapping
138   // identified by ON_MappingChannel::m_mapping_id is used.
139   // A value of zero means no mapping is supplied
140   // and the texture coordinates on the mesh are
141   // used.
142   int m_mapping_channel_id;
143 
144   // Bitmap filename
145   //   During runtime, m_filename is the absolute path to the
146   //   file in use.  If m_filename_bRelativePath is true, then
147   //   the value saved in the 3dm archive will be a relative path.
148   //   When m_filename_bRelativePath is true, user interface
149   //   should display a relative path.
150   ON_wString m_filename;
151   bool m_filename_bRelativePath;
152 
153   // If false, texture is off and should be ignored.
154   // The intended use is to allow people to turn textures
155   // on and off without have to create/destroy or change
156   // other texture settings.
157   bool m_bOn;
158 
159   // do not change TYPE enum values - they are saved in 3dm files.
160   // The "TYPE" setting controls how the pixels in the bitmap
161   // are interpreted.
162   enum TYPE
163   {
164     no_texture_type = 0,
165 
166     bitmap_texture       = 1, // "standard" image texture.
167     bump_texture         = 2, // bump map - see m_bump_scale comment
168     transparency_texture = 3, // value = alpha (see m_tranparancy_id)
169 
170     // OBSOLETE - set m_mapping_channel_id = ON_MappingChannel::emap_mapping
171     emap_texture = 86, // spherical environment mapping.
172 
173     force_32bit_texture_type = 0xFFFFFFFF
174   };
175 
176   TYPE m_type;
177 
178   // m_mode determines how the texture is
179   // do not change MODE enum values - they are saved in 3dm files.
180   enum MODE
181   {
182     no_texture_mode  = 0,
183     modulate_texture = 1,  // modulate with material diffuse color
184     decal_texture    = 2,  // decal
185     blend_texture    = 3,  // blend texture with others in the material
186                            // To "add" a texture, set m_blend_amount = +1
187                            // To "subtract" a texture, set m_blend_amount = -1
188 
189     force_32bit_texture_mode = 0xFFFFFFFF
190   };
191 
192   MODE m_mode;
193 
194   enum FILTER
195   {
196     nearest_filter = 0, // nearest texture pixel is used
197     linear_filter  = 1, // weighted average of corresponding texture pixels
198 
199     force_32bit_texture_filter = 0xFFFFFFFF
200   };
201 
202   // The value of m_minfilter determines how the color
203   // of the image pixel is calculated when the image pixel
204   // corresponds to multiple texture bitmap pixels.
205   FILTER m_minfilter;
206 
207   // The magfilter setting controls how the color
208   // of the image pixel is calculated when the image pixel
209   // corresponds to a fraction of a texture bitmap pixel.
210   FILTER m_magfilter;
211 
212   enum WRAP
213   {
214     repeat_wrap      = 0,
215     clamp_wrap       = 1,
216 
217     force_32bit_texture_wrap = 0xFFFFFFFF
218   };
219 
220   WRAP m_wrapu;
221   WRAP m_wrapv;
222   WRAP m_wrapw;
223 
224   // Texture coordinate transformation.
225   bool m_bApply_uvw; // true if m_uvw is active.
226   ON_Xform m_uvw;
227 
228   // If ON_UNSET_COLOR != m_border_color, then this color
229   // is used when the texture coordinates are <=0 or >=1
230   // and the m_wrap* value is clamp_wrap.
231   ON_Color m_border_color;
232 
233   // This field is used for textures with type
234   // bitmap_texture that reference bitmap files that do
235   // not have an alpha channel and is used to set
236   // runtime alpha values.  It needs to be parsed when the
237   // texture is loaded and can be ignored at runtime.
238   //
239   // If ON_UNSET_COLOR != m_transparent_color, then
240   // a pixel in the bitmap file with a matching RGB
241   // value is assigned the alpha value (ON_Color::Alpha)
242   // in m_transparent_color. The intended use is
243   // for non-rectangular decals defined by RGB bitmaps in
244   // files that don't save an alpha channel.
245   //
246   // For example if the decal is a red number 7 with a
247   // white background, then you would set m_transparent_color's
248   // RGB to white and its A to zero.
249   ON_Color m_transparent_color;
250 
251   // This field is used for textures with type
252   // bitmap_texture that reference bitmap files that do
253   // not have an alpha channel and is used to set
254   // runtime alpha values.  It needs to be parsed when the
255   // texture is loaded and can be ignored at runtime.
256   //
257   // If m_transparency_id is not nil, it is the id of another
258   // texture in the ON_Material.m_textures[] array that has
259   // type m_transparency_texture.  The runtime bitmap_texture's
260   // alpha is set to (255-max(R,G,B)) (the "value" in the hue,
261   // saturation,value sense) of the correspondeing
262   // transparency_texture pixel.
263   //
264   // For example, if you had a bitmap texuture that was green
265   // with purple dots saved in a RGB .bmp file and you wanted
266   // the purple dots to be semi-transparent, you could create
267   // another bitmap that was black, where the original was green,
268   // and gray, where the original was purple, have an
269   // transparency_texture reference the white/gray bitmap,
270   // and have the bitmap_texture's m_transparency_id
271   // reference the transparency map.
272   ON_UUID m_transparency_texture_id;
273 
274   // If the m_type is bump_texture, the height of the
275   // bump is m_bump_scale.ParameterAt(value), where
276   // value is in the HSV sense and normalized
277   // (black=0, white=1).  The interval can be
278   // decreasing.
279   ON_Interval m_bump_scale;
280 
281   // If the m_mode is blend_texture, then m_blend_A[]
282   // and m_blend_RGB[] determine the blending function.
283   //  new alpha  = m_blend_constant_A
284   //             + m_blend_A[0]*(current alpha)
285   //             + m_blend_A[1]*(texture alpha)
286   //             + m_blend_A[2]*min(current alpha,texture alpha)
287   //             + m_blend_A[3]*max(current alpha,texture alpha)
288   //  new rgb    = m_blend_constant_RGB
289   //             + m_blend_RGB[0]*(current RGB)
290   //             + m_blend_RGB[1]*(texture RGB)
291   //             + m_blend_RGB[2]*min(current RGB,texture RGB)
292   //             + m_blend_RGB[3]*max(current RGB,texture RGB)
293   // Results are clamped to handle underflow or overflow.
294   double m_blend_constant_A;
295   double m_blend_A[4];
296   ON_Color m_blend_constant_RGB;
297   double m_blend_RGB[4];
298 
299   // If an ON_Material m_textures[] array has more than
300   // one texture, the textures are blended, and the textures
301   // have different m_blend_order values, the the texture
302   // with the smaller m_blend_order is first.
303   int m_blend_order;
304 
305   // Applications use the m_runtime_ptr_id and m_runtime_ptr fields
306   // to cached runtime bitmaps. If either the id or the pointer
307   // are non-zero, then you cannot use them.  If you hang something
308   // on the pointer, then set the id to something unique to
309   // prevent others from messing with it.
310   ON_UUID m_runtime_ptr_id;
311   const void* m_runtime_ptr;
312 
313   static TYPE   TypeFromInt( int i );
314   static MODE   ModeFromInt( int i );
315   static FILTER FilterFromInt( int i );
316   static WRAP   WrapFromInt( int i );
317 };
318 
319 #if defined(ON_DLL_TEMPLATE)
320 // This stuff is here because of a limitation in the way Microsoft
321 // handles templates and DLLs.  See Microsoft's knowledge base
322 // article ID Q168958 for details.
323 #pragma warning( push )
324 #pragma warning( disable : 4231 )
325 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_Texture>;
326 ON_DLL_TEMPLATE template class ON_CLASS ON_ObjectArray<ON_Texture>;
327 #pragma warning( pop )
328 #endif
329 
330 #endif
331 
332