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