00001 // Filename: depthOffsetAttrib.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 "depthOffsetAttrib.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 DepthOffsetAttrib::_type_handle; 00024 int DepthOffsetAttrib::_attrib_slot; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: DepthOffsetAttrib::make 00028 // Access: Published, Static 00029 // Description: Constructs a new DepthOffsetAttrib object that 00030 // indicates the relative amount of bias to write to the 00031 // depth buffer for subsequent geometry. 00032 //////////////////////////////////////////////////////////////////// 00033 CPT(RenderAttrib) DepthOffsetAttrib:: 00034 make(int offset) { 00035 DepthOffsetAttrib *attrib = new DepthOffsetAttrib(offset); 00036 return return_new(attrib); 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: DepthOffsetAttrib::make_default 00041 // Access: Published, Static 00042 // Description: Returns a RenderAttrib that corresponds to whatever 00043 // the standard default properties for render attributes 00044 // of this type ought to be. 00045 //////////////////////////////////////////////////////////////////// 00046 CPT(RenderAttrib) DepthOffsetAttrib:: 00047 make_default() { 00048 return return_new(new DepthOffsetAttrib(0)); 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: DepthOffsetAttrib::output 00053 // Access: Public, Virtual 00054 // Description: 00055 //////////////////////////////////////////////////////////////////// 00056 void DepthOffsetAttrib:: 00057 output(ostream &out) const { 00058 out << get_type() << ":(" << get_offset() << ")"; 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: DepthOffsetAttrib::compare_to_impl 00063 // Access: Protected, Virtual 00064 // Description: Intended to be overridden by derived DepthOffsetAttrib 00065 // types to return a unique number indicating whether 00066 // this DepthOffsetAttrib is equivalent to the other one. 00067 // 00068 // This should return 0 if the two DepthOffsetAttrib objects 00069 // are equivalent, a number less than zero if this one 00070 // should be sorted before the other one, and a number 00071 // greater than zero otherwise. 00072 // 00073 // This will only be called with two DepthOffsetAttrib 00074 // objects whose get_type() functions return the same. 00075 //////////////////////////////////////////////////////////////////// 00076 int DepthOffsetAttrib:: 00077 compare_to_impl(const RenderAttrib *other) const { 00078 const DepthOffsetAttrib *ta; 00079 DCAST_INTO_R(ta, other, 0); 00080 return _offset - ta->_offset; 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: DepthOffsetAttrib::get_hash_impl 00085 // Access: Protected, Virtual 00086 // Description: Intended to be overridden by derived RenderAttrib 00087 // types to return a unique hash for these particular 00088 // properties. RenderAttribs that compare the same with 00089 // compare_to_impl(), above, should return the same 00090 // hash; RenderAttribs that compare differently should 00091 // return a different hash. 00092 //////////////////////////////////////////////////////////////////// 00093 size_t DepthOffsetAttrib:: 00094 get_hash_impl() const { 00095 size_t hash = 0; 00096 hash = int_hash::add_hash(hash, _offset); 00097 return hash; 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function: DepthOffsetAttrib::compose_impl 00102 // Access: Protected, Virtual 00103 // Description: Intended to be overridden by derived RenderAttrib 00104 // types to specify how two consecutive RenderAttrib 00105 // objects of the same type interact. 00106 // 00107 // This should return the result of applying the other 00108 // RenderAttrib to a node in the scene graph below this 00109 // RenderAttrib, which was already applied. In most 00110 // cases, the result is the same as the other 00111 // RenderAttrib (that is, a subsequent RenderAttrib 00112 // completely replaces the preceding one). On the other 00113 // hand, some kinds of RenderAttrib (for instance, 00114 // ColorTransformAttrib) might combine in meaningful 00115 // ways. 00116 //////////////////////////////////////////////////////////////////// 00117 CPT(RenderAttrib) DepthOffsetAttrib:: 00118 compose_impl(const RenderAttrib *other) const { 00119 const DepthOffsetAttrib *ta; 00120 DCAST_INTO_R(ta, other, 0); 00121 int new_offset = ta->_offset + _offset; 00122 00123 DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset); 00124 return return_new(attrib); 00125 } 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: DepthOffsetAttrib::invert_compose_impl 00129 // Access: Protected, Virtual 00130 // Description: Intended to be overridden by derived RenderAttrib 00131 // types to specify how two consecutive RenderAttrib 00132 // objects of the same type interact. 00133 // 00134 // See invert_compose() and compose_impl(). 00135 //////////////////////////////////////////////////////////////////// 00136 CPT(RenderAttrib) DepthOffsetAttrib:: 00137 invert_compose_impl(const RenderAttrib *other) const { 00138 const DepthOffsetAttrib *ta; 00139 DCAST_INTO_R(ta, other, 0); 00140 int new_offset = ta->_offset - _offset; 00141 00142 DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset); 00143 return return_new(attrib); 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function: DepthOffsetAttrib::register_with_read_factory 00148 // Access: Public, Static 00149 // Description: Tells the BamReader how to create objects of type 00150 // DepthOffsetAttrib. 00151 //////////////////////////////////////////////////////////////////// 00152 void DepthOffsetAttrib:: 00153 register_with_read_factory() { 00154 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00155 } 00156 00157 //////////////////////////////////////////////////////////////////// 00158 // Function: DepthOffsetAttrib::write_datagram 00159 // Access: Public, Virtual 00160 // Description: Writes the contents of this object to the datagram 00161 // for shipping out to a Bam file. 00162 //////////////////////////////////////////////////////////////////// 00163 void DepthOffsetAttrib:: 00164 write_datagram(BamWriter *manager, Datagram &dg) { 00165 RenderAttrib::write_datagram(manager, dg); 00166 00167 dg.add_int32(_offset); 00168 } 00169 00170 //////////////////////////////////////////////////////////////////// 00171 // Function: DepthOffsetAttrib::make_from_bam 00172 // Access: Protected, Static 00173 // Description: This function is called by the BamReader's factory 00174 // when a new object of type DepthOffsetAttrib is encountered 00175 // in the Bam file. It should create the DepthOffsetAttrib 00176 // and extract its information from the file. 00177 //////////////////////////////////////////////////////////////////// 00178 TypedWritable *DepthOffsetAttrib:: 00179 make_from_bam(const FactoryParams ¶ms) { 00180 DepthOffsetAttrib *attrib = new DepthOffsetAttrib(0); 00181 DatagramIterator scan; 00182 BamReader *manager; 00183 00184 parse_params(params, scan, manager); 00185 attrib->fillin(scan, manager); 00186 00187 return attrib; 00188 } 00189 00190 //////////////////////////////////////////////////////////////////// 00191 // Function: DepthOffsetAttrib::fillin 00192 // Access: Protected 00193 // Description: This internal function is called by make_from_bam to 00194 // read in all of the relevant data from the BamFile for 00195 // the new DepthOffsetAttrib. 00196 //////////////////////////////////////////////////////////////////// 00197 void DepthOffsetAttrib:: 00198 fillin(DatagramIterator &scan, BamReader *manager) { 00199 RenderAttrib::fillin(scan, manager); 00200 00201 _offset = scan.get_int32(); 00202 }