00001 /* $NoKeywords: $ */ 00002 /* 00003 // 00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved. 00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert 00006 // McNeel & Associates. 00007 // 00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED. 00011 // 00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>. 00013 // 00015 */ 00016 00017 #if !defined(OPENNURBS_MATERIAL_INC_) 00018 #define OPENNURBS_MATERIAL_INC_ 00019 00020 00022 // 00023 // Class ON_Material 00024 // 00025 class ON_CLASS ON_Material : public ON_Object 00026 { 00027 ON_OBJECT_DECLARE(ON_Material); 00028 00029 public: 00030 static double MaxShine(); // maximum value of shine exponent 00031 00032 ON_Material(); // Default grey color 00033 ~ON_Material(); // destructor 00034 // C++ default copy construction and operator= work fine. 00035 00036 bool operator==(const ON_Material&) const; // ignores m_material_index 00037 bool operator!=(const ON_Material&) const; // ignores m_material_index 00038 00039 void Default(); 00040 00042 // ON_Object overrides 00043 00044 /* 00045 Description: 00046 Tests an object to see if its data members are correctly 00047 initialized. 00048 Parameters: 00049 text_log - [in] if the object is not valid and text_log 00050 is not NULL, then a brief englis description of the 00051 reason the object is not valid is appened to the log. 00052 The information appended to text_log is suitable for 00053 low-level debugging purposes by programmers and is 00054 not intended to be useful as a high level user 00055 interface tool. 00056 Returns: 00057 @untitled table 00058 true object is valid 00059 false object is invalid, uninitialized, etc. 00060 Remarks: 00061 Overrides virtual ON_Object::IsValid 00062 */ 00063 ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const; 00064 00065 void Dump( ON_TextLog& ) const; // for debugging 00066 00067 ON_BOOL32 Write( 00068 ON_BinaryArchive& // open binary file 00069 ) const; 00070 00071 ON_BOOL32 Read( 00072 ON_BinaryArchive& // open binary file 00073 ); 00074 00075 ON::object_type ObjectType() const; 00076 00077 // virtual 00078 ON_UUID ModelObjectId() const; 00079 00080 00082 // Interface 00083 00084 // ignores m_material_index 00085 int Compare( const ON_Material& other ) const; 00086 00087 // OBSOLETE - use m_ambient 00088 ON_Color Ambient() const; 00089 // OBSOLETE - use m_diffuse 00090 ON_Color Diffuse() const; 00091 // OBSOLETE - use m_emission 00092 ON_Color Emission() const; 00093 // OBSOLETE - use m_specular 00094 ON_Color Specular() const; 00095 00096 // OBSOLETE - use m_ambient 00097 void SetAmbient( ON_Color ); 00098 // OBSOLETE - use m_diffuse 00099 void SetDiffuse( ON_Color ); 00100 // OBSOLETE - use m_emission 00101 void SetEmission( ON_Color ); 00102 // OBSOLETE - use m_specular 00103 void SetSpecular( ON_Color ); 00104 00105 // Shine values are in range 0.0 to ON_Material::MaxShine() 00106 double Shine() const; 00107 void SetShine( double ); // 0 to ON_Material::MaxShine() 00108 00109 // Transparency values are in range 0.0 = opaque to 1.0 = transparent 00110 double Transparency() const; 00111 void SetTransparency( double ); // 0.0 = opaque, 1.0 = transparent 00112 00113 // OBSOLETE - use m_material_index 00114 int MaterialIndex() const; 00115 // OBSOLETE - use m_material_index 00116 void SetMaterialIndex( int ); 00117 00118 // OBSOLETE - just use m_plugin_id 00119 ON_UUID MaterialPlugInUuid() const; 00120 00121 // OBSOLETE - just use m_plugin_id 00122 void SetMaterialPlugInUuid( ON_UUID ); 00123 00124 // OBSOLETE - just use m_material_name 00125 const wchar_t* MaterialName() const; 00126 00127 // OBSOLETE - just use m_material_name 00128 void SetMaterialName( const wchar_t* ); 00129 00130 // The only reliable and persistent way to reference 00131 // materials is by the material_id. 00132 ON_UUID m_material_id; 00133 00134 // Runtime material table index. This value is constant 00135 // for each runtim instance of Rhino, but can change 00136 // each time a model is loaded or saved. Once a material 00137 // is in the CRhinoDoc material table, its id and index 00138 // never change in that instance of Rhino. 00139 int m_material_index; 00140 00141 // 00142 ON_wString m_material_name; // For user comfort - duplicates permitted 00143 00144 ON_wString m_flamingo_library; // Legacy information from V3. 00145 // Will vanish in V5. 00146 00147 ON_Color m_ambient; 00148 ON_Color m_diffuse; 00149 ON_Color m_emission; 00150 ON_Color m_specular; 00151 ON_Color m_reflection; 00152 ON_Color m_transparent; 00153 double m_index_of_refraction; // generally >= 1.0 (speed of light in vacum)/(speed of light in material) 00154 double m_reflectivity; // 0.0 = none, 1.0 = 100% 00155 double m_shine; // 0.0 = none to GetMaxShine()=maximum 00156 double m_transparency; // 0.0 = opaque to 1.0 = transparent (1.0-alpha) 00157 00158 bool m_bShared; // default = false. 00159 // True means this material can be shared. When an 00160 // object that uses this material is copied, 00161 // the new object will share the material. 00162 // False means this material is not shared. 00163 // When an object that uses this material is 00164 // duplicated. 00165 00166 bool m_bDisableLighting; // default = false. 00167 // True means render this object without 00168 // applying any modulation based on lights. 00169 // Basically, the diffuse, ambient, specular and 00170 // emissive channels get combined additively, clamped, 00171 // and then get treated as an emissive channel. 00172 // Another way to think about it is when 00173 // m_bDisableLighting is true, render the same way 00174 // OpenGL does when ::glDisable( GL_LIGHTING ) is called. 00175 00176 private: 00177 unsigned char m_reserved1[2]; 00178 #if defined(ON_64BIT_POINTER) 00179 unsigned char m_reserved2[4]; 00180 #endif 00181 public: 00182 00183 /* 00184 Description: 00185 Searches for a texure with matching texture_id. 00186 If more than one texture matches, the first match 00187 is returned. 00188 Parameters: 00189 texture_id - [in] 00190 Returns: 00191 >=0 m_textures[] index of matching texture 00192 -1 if no match is found. 00193 */ 00194 int FindTexture( 00195 ON_UUID texture_id 00196 ) const; 00197 00198 /* 00199 Description: 00200 Searches for a texure with matching filename and type. 00201 If more than one texture matches, the first match 00202 is returned. 00203 Parameters: 00204 filename - [in] If NULL, then any filename matches. 00205 type - [in] If ON_Texture::no_texture_type, then 00206 any texture type matches. 00207 i0 - [in] If i0 is < 0, the search begins at 00208 m_textures[0], if i0 >= m_textures.Count(), 00209 -1 is returnd, otherwise, the search begins 00210 at m_textures[i0+1]. 00211 Example: 00212 Iterate through all the the bitmap textures on 00213 a material. 00214 00215 ON_Material& mat = ...; 00216 int ti = -1; 00217 int bitmap_texture_count = 0; 00218 for(;;) 00219 { 00220 ti = mat.FindTexture( 00221 NULL, 00222 ON_Texture::bitmap_texture, 00223 ti ); 00224 00225 if ( ti < 0 ) 00226 { 00227 // no more bitmap textures 00228 break; 00229 } 00230 00231 // we have a bitmap texture 00232 bitmap_texture_count++; 00233 const ON_Texture& bitmap_texture = mat.m_textures[ti]; 00234 ... 00235 } 00236 00237 Returns: 00238 >=0 m_textures[] index of matching texture 00239 -1 if no match is found. 00240 */ 00241 int FindTexture( 00242 const wchar_t* filename, 00243 ON_Texture::TYPE type, 00244 int i0 = -1 00245 ) const; 00246 00247 /* 00248 Description: 00249 If there is already a texture with the same file name and 00250 type, then that texture is modified, otherwise a new texture 00251 is added. If tx has user data, the user data is copied 00252 to the m_textures[] element. 00253 Parameters: 00254 tx - [in] 00255 Returns: 00256 Index of the added texture in the m_textures[] array. 00257 Remarks: 00258 This is intended to be a quick and simple way to add 00259 textures to the material. If you need to do something 00260 different, then just work on the m_textures[] array. 00261 */ 00262 int AddTexture( 00263 const ON_Texture& tx 00264 ); 00265 00266 /* 00267 Description: 00268 If there is a texture with a matching type, that texture's 00269 filename is modified, otherwise a new texture is added. 00270 Parameters: 00271 filename - [in] new filename 00272 type - [in] 00273 Returns: 00274 Index of the added texture in the m_textures[] array. 00275 Remarks: 00276 This is intended to be a quick and simple way to add 00277 textures to the material. If you need to do something 00278 different, then just work on the m_textures[] array. 00279 */ 00280 int AddTexture( 00281 const wchar_t* filename, 00282 ON_Texture::TYPE type 00283 ); 00284 00285 /* 00286 Description: 00287 Deletes all texures with matching filenames and types. 00288 Parameters: 00289 filename - [in] If NULL, then any filename matches. 00290 type - [in] If ON_Texture::no_texture_type, then 00291 any texture type matches. 00292 Returns: 00293 Number of textures deleted. 00294 */ 00295 int DeleteTexture( 00296 const wchar_t* filename, 00297 ON_Texture::TYPE type 00298 ); 00299 00300 ON_ObjectArray<ON_Texture> m_textures; 00301 00302 /* 00303 Description: 00304 Used to provide per face material support. 00305 The parent object reference a basic material. 00306 When a brep face or mesh facet wants to use 00307 a material besides the base material, it specifies 00308 a channelSupports material channel. The default 00309 material channel is 0 and that indicates the base 00310 material. A channel of n > 0 means that face 00311 used the material with id m_material_channel[n-1]. 00312 If (n-1) >= m_material_channel.Count(), then the base 00313 material is used. The value of 00314 m_material_channel[n].m_id is persistent. The 00315 value of m_material_channel[n].m_i is a runtime 00316 index in the CRhinoDoc::m_material_table[]. If 00317 CRhinoDoc::m_material_table[m_i].m_uuid != m_id, 00318 then m_id is assumed to be correct. 00319 */ 00320 ON_SimpleArray<ON_UuidIndex> m_material_channel; 00321 00322 ON_UUID m_plugin_id; // ID of the last plug-in to modify this material 00323 00324 private: 00325 static double m_max_shine; 00326 bool ReadV3Helper( ON_BinaryArchive& file, int minor_version ); 00327 bool WriteV3Helper( ON_BinaryArchive& file ) const; 00328 }; 00329 00330 #if defined(ON_DLL_TEMPLATE) 00331 // This stuff is here because of a limitation in the way Microsoft 00332 // handles templates and DLLs. See Microsoft's knowledge base 00333 // article ID Q168958 for details. 00334 #pragma warning( push ) 00335 #pragma warning( disable : 4231 ) 00336 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_Material>; 00337 ON_DLL_TEMPLATE template class ON_CLASS ON_ObjectArray<ON_Material>; 00338 #pragma warning( pop ) 00339 #endif 00340 00341 #endif 00342