Panda3D
|
00001 // Filename: materialAttrib.cxx 00002 // Created by: drose (04Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "materialAttrib.h" 00016 #include "graphicsStateGuardianBase.h" 00017 #include "bamReader.h" 00018 #include "bamWriter.h" 00019 #include "datagram.h" 00020 #include "datagramIterator.h" 00021 00022 TypeHandle MaterialAttrib::_type_handle; 00023 int MaterialAttrib::_attrib_slot; 00024 00025 //////////////////////////////////////////////////////////////////// 00026 // Function: MaterialAttrib::make 00027 // Access: Published, Static 00028 // Description: Constructs a new MaterialAttrib object suitable for 00029 // rendering the indicated material onto geometry. 00030 //////////////////////////////////////////////////////////////////// 00031 CPT(RenderAttrib) MaterialAttrib:: 00032 make(Material *material) { 00033 MaterialAttrib *attrib = new MaterialAttrib; 00034 attrib->_material = material; 00035 material->set_attrib_lock(); 00036 return return_new(attrib); 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: MaterialAttrib::make_off 00041 // Access: Published, Static 00042 // Description: Constructs a new MaterialAttrib object suitable for 00043 // rendering unmateriald geometry. 00044 //////////////////////////////////////////////////////////////////// 00045 CPT(RenderAttrib) MaterialAttrib:: 00046 make_off() { 00047 MaterialAttrib *attrib = new MaterialAttrib; 00048 return return_new(attrib); 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: MaterialAttrib::make_default 00053 // Access: Published, Static 00054 // Description: Returns a RenderAttrib that corresponds to whatever 00055 // the standard default properties for render attributes 00056 // of this type ought to be. 00057 //////////////////////////////////////////////////////////////////// 00058 CPT(RenderAttrib) MaterialAttrib:: 00059 make_default() { 00060 return return_new(new MaterialAttrib); 00061 } 00062 00063 //////////////////////////////////////////////////////////////////// 00064 // Function: MaterialAttrib::output 00065 // Access: Public, Virtual 00066 // Description: 00067 //////////////////////////////////////////////////////////////////// 00068 void MaterialAttrib:: 00069 output(ostream &out) const { 00070 out << get_type() << ":"; 00071 if (is_off()) { 00072 out << "(off)"; 00073 } else { 00074 out << *_material; 00075 } 00076 } 00077 00078 //////////////////////////////////////////////////////////////////// 00079 // Function: MaterialAttrib::compare_to_impl 00080 // Access: Protected, Virtual 00081 // Description: Intended to be overridden by derived MaterialAttrib 00082 // types to return a unique number indicating whether 00083 // this MaterialAttrib is equivalent to the other one. 00084 // 00085 // This should return 0 if the two MaterialAttrib objects 00086 // are equivalent, a number less than zero if this one 00087 // should be sorted before the other one, and a number 00088 // greater than zero otherwise. 00089 // 00090 // This will only be called with two MaterialAttrib 00091 // objects whose get_type() functions return the same. 00092 //////////////////////////////////////////////////////////////////// 00093 int MaterialAttrib:: 00094 compare_to_impl(const RenderAttrib *other) const { 00095 const MaterialAttrib *ta; 00096 DCAST_INTO_R(ta, other, 0); 00097 00098 // Comparing pointers by subtraction is problematic. Instead of 00099 // doing this, we'll just depend on the built-in != and < operators 00100 // for comparing pointers. 00101 if (_material != ta->_material) { 00102 return _material < ta->_material ? -1 : 1; 00103 } 00104 return 0; 00105 } 00106 00107 //////////////////////////////////////////////////////////////////// 00108 // Function: MaterialAttrib::get_hash_impl 00109 // Access: Protected, Virtual 00110 // Description: Intended to be overridden by derived RenderAttrib 00111 // types to return a unique hash for these particular 00112 // properties. RenderAttribs that compare the same with 00113 // compare_to_impl(), above, should return the same 00114 // hash; RenderAttribs that compare differently should 00115 // return a different hash. 00116 //////////////////////////////////////////////////////////////////// 00117 size_t MaterialAttrib:: 00118 get_hash_impl() const { 00119 size_t hash = 0; 00120 hash = pointer_hash::add_hash(hash, _material); 00121 return hash; 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: MaterialAttrib::get_auto_shader_attrib_impl 00126 // Access: Protected, Virtual 00127 // Description: 00128 //////////////////////////////////////////////////////////////////// 00129 CPT(RenderAttrib) MaterialAttrib:: 00130 get_auto_shader_attrib_impl(const RenderState *state) const { 00131 return this; 00132 } 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function: MaterialAttrib::register_with_read_factory 00136 // Access: Public, Static 00137 // Description: Tells the BamReader how to create objects of type 00138 // MaterialAttrib. 00139 //////////////////////////////////////////////////////////////////// 00140 void MaterialAttrib:: 00141 register_with_read_factory() { 00142 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00143 } 00144 00145 //////////////////////////////////////////////////////////////////// 00146 // Function: MaterialAttrib::write_datagram 00147 // Access: Public, Virtual 00148 // Description: Writes the contents of this object to the datagram 00149 // for shipping out to a Bam file. 00150 //////////////////////////////////////////////////////////////////// 00151 void MaterialAttrib:: 00152 write_datagram(BamWriter *manager, Datagram &dg) { 00153 RenderAttrib::write_datagram(manager, dg); 00154 00155 manager->write_pointer(dg, _material); 00156 } 00157 00158 //////////////////////////////////////////////////////////////////// 00159 // Function: MaterialAttrib::complete_pointers 00160 // Access: Public, Virtual 00161 // Description: Receives an array of pointers, one for each time 00162 // manager->read_pointer() was called in fillin(). 00163 // Returns the number of pointers processed. 00164 //////////////////////////////////////////////////////////////////// 00165 int MaterialAttrib:: 00166 complete_pointers(TypedWritable **p_list, BamReader *manager) { 00167 int pi = RenderAttrib::complete_pointers(p_list, manager); 00168 00169 TypedWritable *material = p_list[pi++]; 00170 if (material != (TypedWritable *)NULL) { 00171 _material = DCAST(Material, material); 00172 } 00173 00174 return pi; 00175 } 00176 00177 //////////////////////////////////////////////////////////////////// 00178 // Function: MaterialAttrib::make_from_bam 00179 // Access: Protected, Static 00180 // Description: This function is called by the BamReader's factory 00181 // when a new object of type MaterialAttrib is encountered 00182 // in the Bam file. It should create the MaterialAttrib 00183 // and extract its information from the file. 00184 //////////////////////////////////////////////////////////////////// 00185 TypedWritable *MaterialAttrib:: 00186 make_from_bam(const FactoryParams ¶ms) { 00187 MaterialAttrib *attrib = new MaterialAttrib; 00188 DatagramIterator scan; 00189 BamReader *manager; 00190 00191 parse_params(params, scan, manager); 00192 attrib->fillin(scan, manager); 00193 return attrib; 00194 } 00195 00196 //////////////////////////////////////////////////////////////////// 00197 // Function: MaterialAttrib::fillin 00198 // Access: Protected 00199 // Description: This internal function is called by make_from_bam to 00200 // read in all of the relevant data from the BamFile for 00201 // the new MaterialAttrib. 00202 //////////////////////////////////////////////////////////////////// 00203 void MaterialAttrib:: 00204 fillin(DatagramIterator &scan, BamReader *manager) { 00205 RenderAttrib::fillin(scan, manager); 00206 00207 // Read the _material pointer. 00208 manager->read_pointer(scan); 00209 }