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