00001 // Filename: cullBinAttrib.cxx 00002 // Created by: drose (01Mar02) 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 "cullBinAttrib.h" 00016 #include "bamReader.h" 00017 #include "bamWriter.h" 00018 #include "datagram.h" 00019 #include "datagramIterator.h" 00020 00021 TypeHandle CullBinAttrib::_type_handle; 00022 int CullBinAttrib::_attrib_slot; 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Function: CullBinAttrib::make 00026 // Access: Published, Static 00027 // Description: Constructs a new CullBinAttrib assigning geometry 00028 // into the named bin. If the bin name is the empty 00029 // string, the default bin is used. 00030 // 00031 // The draw_order specifies further ordering information 00032 // which is relevant only to certain kinds of bins (in 00033 // particular CullBinFixed type bins). 00034 //////////////////////////////////////////////////////////////////// 00035 CPT(RenderAttrib) CullBinAttrib:: 00036 make(const string &bin_name, int draw_order) { 00037 CullBinAttrib *attrib = new CullBinAttrib; 00038 attrib->_bin_name = bin_name; 00039 attrib->_draw_order = draw_order; 00040 return return_new(attrib); 00041 } 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: CullBinAttrib::make_default 00045 // Access: Published, Static 00046 // Description: Returns a RenderAttrib that corresponds to whatever 00047 // the standard default properties for render attributes 00048 // of this type ought to be. 00049 //////////////////////////////////////////////////////////////////// 00050 CPT(RenderAttrib) CullBinAttrib:: 00051 make_default() { 00052 return return_new(new CullBinAttrib); 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: CullBinAttrib::output 00057 // Access: Public, Virtual 00058 // Description: 00059 //////////////////////////////////////////////////////////////////// 00060 void CullBinAttrib:: 00061 output(ostream &out) const { 00062 out << get_type() << ":"; 00063 if (_bin_name.empty()) { 00064 out << "(default)"; 00065 } else { 00066 out << _bin_name << "," << _draw_order; 00067 } 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: CullBinAttrib::compare_to_impl 00072 // Access: Protected, Virtual 00073 // Description: Intended to be overridden by derived CullBinAttrib 00074 // types to return a unique number indicating whether 00075 // this CullBinAttrib is equivalent to the other one. 00076 // 00077 // This should return 0 if the two CullBinAttrib objects 00078 // are equivalent, a number less than zero if this one 00079 // should be sorted before the other one, and a number 00080 // greater than zero otherwise. 00081 // 00082 // This will only be called with two CullBinAttrib 00083 // objects whose get_type() functions return the same. 00084 //////////////////////////////////////////////////////////////////// 00085 int CullBinAttrib:: 00086 compare_to_impl(const RenderAttrib *other) const { 00087 const CullBinAttrib *ta; 00088 DCAST_INTO_R(ta, other, 0); 00089 if (_draw_order != ta->_draw_order) { 00090 return _draw_order - ta->_draw_order; 00091 } 00092 return strcmp(_bin_name.c_str(), ta->_bin_name.c_str()); 00093 } 00094 00095 //////////////////////////////////////////////////////////////////// 00096 // Function: CullBinAttrib::get_hash_impl 00097 // Access: Protected, Virtual 00098 // Description: Intended to be overridden by derived RenderAttrib 00099 // types to return a unique hash for these particular 00100 // properties. RenderAttribs that compare the same with 00101 // compare_to_impl(), above, should return the same 00102 // hash; RenderAttribs that compare differently should 00103 // return a different hash. 00104 //////////////////////////////////////////////////////////////////// 00105 size_t CullBinAttrib:: 00106 get_hash_impl() const { 00107 size_t hash = 0; 00108 hash = int_hash::add_hash(hash, _draw_order); 00109 hash = string_hash::add_hash(hash, _bin_name); 00110 return hash; 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: CullBinAttrib::register_with_read_factory 00115 // Access: Public, Static 00116 // Description: Tells the BamReader how to create objects of type 00117 // CullBinAttrib. 00118 //////////////////////////////////////////////////////////////////// 00119 void CullBinAttrib:: 00120 register_with_read_factory() { 00121 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: CullBinAttrib::write_datagram 00126 // Access: Public, Virtual 00127 // Description: Writes the contents of this object to the datagram 00128 // for shipping out to a Bam file. 00129 //////////////////////////////////////////////////////////////////// 00130 void CullBinAttrib:: 00131 write_datagram(BamWriter *manager, Datagram &dg) { 00132 RenderAttrib::write_datagram(manager, dg); 00133 00134 dg.add_string(_bin_name); 00135 dg.add_int32(_draw_order); 00136 } 00137 00138 //////////////////////////////////////////////////////////////////// 00139 // Function: CullBinAttrib::make_from_bam 00140 // Access: Protected, Static 00141 // Description: This function is called by the BamReader's factory 00142 // when a new object of type CullBinAttrib is encountered 00143 // in the Bam file. It should create the CullBinAttrib 00144 // and extract its information from the file. 00145 //////////////////////////////////////////////////////////////////// 00146 TypedWritable *CullBinAttrib:: 00147 make_from_bam(const FactoryParams ¶ms) { 00148 CullBinAttrib *attrib = new CullBinAttrib; 00149 DatagramIterator scan; 00150 BamReader *manager; 00151 00152 parse_params(params, scan, manager); 00153 attrib->fillin(scan, manager); 00154 00155 return attrib; 00156 } 00157 00158 //////////////////////////////////////////////////////////////////// 00159 // Function: CullBinAttrib::fillin 00160 // Access: Protected 00161 // Description: This internal function is called by make_from_bam to 00162 // read in all of the relevant data from the BamFile for 00163 // the new CullBinAttrib. 00164 //////////////////////////////////////////////////////////////////// 00165 void CullBinAttrib:: 00166 fillin(DatagramIterator &scan, BamReader *manager) { 00167 RenderAttrib::fillin(scan, manager); 00168 00169 _bin_name = scan.get_string(); 00170 _draw_order = scan.get_int32(); 00171 }