00001 // Filename: alphaTestAttrib.cxx 00002 // Created by: drose (04Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "alphaTestAttrib.h" 00020 #include "graphicsStateGuardianBase.h" 00021 #include "dcast.h" 00022 #include "bamReader.h" 00023 #include "bamWriter.h" 00024 #include "datagram.h" 00025 #include "datagramIterator.h" 00026 00027 TypeHandle AlphaTestAttrib::_type_handle; 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: AlphaTestAttrib::make 00031 // Access: Published, Static 00032 // Description: Constructs a new AlphaTestAttrib object. 00033 //////////////////////////////////////////////////////////////////// 00034 CPT(RenderAttrib) AlphaTestAttrib:: 00035 make(PandaCompareFunc mode, float reference_value) { 00036 assert((reference_value >=0.0f) && (reference_value <=1.0f)); 00037 AlphaTestAttrib *attrib = new AlphaTestAttrib(mode,reference_value); 00038 return return_new(attrib); 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: AlphaTestAttrib::issue 00043 // Access: Public, Virtual 00044 // Description: Calls the appropriate method on the indicated GSG 00045 // to issue the graphics commands appropriate to the 00046 // given attribute. This is normally called 00047 // (indirectly) only from 00048 // GraphicsStateGuardian::set_state() or modify_state(). 00049 //////////////////////////////////////////////////////////////////// 00050 void AlphaTestAttrib:: 00051 issue(GraphicsStateGuardianBase *gsg) const { 00052 gsg->issue_alpha_test(this); 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: AlphaTestAttrib::output 00057 // Access: Public, Virtual 00058 // Description: 00059 //////////////////////////////////////////////////////////////////// 00060 void AlphaTestAttrib:: 00061 output(ostream &out) const { 00062 out << get_type() << ":"; 00063 output_comparefunc(out,_mode); 00064 out << "," << _reference_alpha; 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: AlphaTestAttrib::compare_to_impl 00069 // Access: Protected, Virtual 00070 // Description: Intended to be overridden by derived AlphaTestAttrib 00071 // types to return a unique number indicating whether 00072 // this AlphaTestAttrib is equivalent to the other one. 00073 // 00074 // This should return 0 if the two AlphaTestAttrib objects 00075 // are equivalent, a number less than zero if this one 00076 // should be sorted before the other one, and a number 00077 // greater than zero otherwise. 00078 // 00079 // This will only be called with two AlphaTestAttrib 00080 // objects whose get_type() functions return the same. 00081 //////////////////////////////////////////////////////////////////// 00082 int AlphaTestAttrib:: 00083 compare_to_impl(const RenderAttrib *other) const { 00084 const AlphaTestAttrib *ta; 00085 DCAST_INTO_R(ta, other, 0); 00086 int compare_result = ((int)_mode - (int)ta->_mode) ; 00087 if (compare_result!=0) { 00088 return compare_result; 00089 } else { 00090 return (int) (255.0f*(_reference_alpha - ta->_reference_alpha)); 00091 } 00092 } 00093 00094 //////////////////////////////////////////////////////////////////// 00095 // Function: AlphaTestAttrib::make_default_impl 00096 // Access: Protected, Virtual 00097 // Description: Intended to be overridden by derived AlphaTestAttrib 00098 // types to specify what the default property for a 00099 // AlphaTestAttrib of this type should be. 00100 // 00101 // This should return a newly-allocated AlphaTestAttrib of 00102 // the same type that corresponds to whatever the 00103 // standard default for this kind of AlphaTestAttrib is. 00104 //////////////////////////////////////////////////////////////////// 00105 RenderAttrib *AlphaTestAttrib:: 00106 make_default_impl() const { 00107 return new AlphaTestAttrib; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: AlphaTestAttrib::register_with_read_factory 00112 // Access: Public, Static 00113 // Description: Tells the BamReader how to create objects of type 00114 // AlphaTestAttrib. 00115 //////////////////////////////////////////////////////////////////// 00116 void AlphaTestAttrib:: 00117 register_with_read_factory() { 00118 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00119 } 00120 00121 //////////////////////////////////////////////////////////////////// 00122 // Function: AlphaTestAttrib::write_datagram 00123 // Access: Public, Virtual 00124 // Description: Writes the contents of this object to the datagram 00125 // for shipping out to a Bam file. 00126 //////////////////////////////////////////////////////////////////// 00127 void AlphaTestAttrib:: 00128 write_datagram(BamWriter *manager, Datagram &dg) { 00129 RenderAttrib::write_datagram(manager, dg); 00130 00131 dg.add_int8(_mode); 00132 dg.add_float32(_reference_alpha); 00133 } 00134 00135 //////////////////////////////////////////////////////////////////// 00136 // Function: AlphaTestAttrib::make_from_bam 00137 // Access: Protected, Static 00138 // Description: This function is called by the BamReader's factory 00139 // when a new object of type AlphaTestAttrib is encountered 00140 // in the Bam file. It should create the AlphaTestAttrib 00141 // and extract its information from the file. 00142 //////////////////////////////////////////////////////////////////// 00143 TypedWritable *AlphaTestAttrib:: 00144 make_from_bam(const FactoryParams ¶ms) { 00145 AlphaTestAttrib *attrib = new AlphaTestAttrib; 00146 DatagramIterator scan; 00147 BamReader *manager; 00148 00149 parse_params(params, scan, manager); 00150 attrib->fillin(scan, manager); 00151 00152 return attrib; 00153 } 00154 00155 //////////////////////////////////////////////////////////////////// 00156 // Function: AlphaTestAttrib::fillin 00157 // Access: Protected 00158 // Description: This internal function is called by make_from_bam to 00159 // read in all of the relevant data from the BamFile for 00160 // the new AlphaTestAttrib. 00161 //////////////////////////////////////////////////////////////////// 00162 void AlphaTestAttrib:: 00163 fillin(DatagramIterator &scan, BamReader *manager) { 00164 RenderAttrib::fillin(scan, manager); 00165 00166 _mode = (PandaCompareFunc)scan.get_int8(); 00167 _reference_alpha = scan.get_float32(); 00168 }