Panda3D
|
00001 // Filename: switchNode.cxx 00002 // Created by: drose (31Jul02) 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 "pandabase.h" 00016 #include "switchNode.h" 00017 #include "cullTraverser.h" 00018 00019 TypeHandle SwitchNode::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: SwitchNode::CData::make_copy 00023 // Access: Public, Virtual 00024 // Description: 00025 //////////////////////////////////////////////////////////////////// 00026 CycleData *SwitchNode::CData:: 00027 make_copy() const { 00028 return new CData(*this); 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: SwitchNode::safe_to_combine 00033 // Access: Public, Virtual 00034 // Description: Returns true if it is generally safe to combine this 00035 // particular kind of PandaNode with other kinds of 00036 // PandaNodes of compatible type, adding children or 00037 // whatever. For instance, an LODNode should not be 00038 // combined with any other PandaNode, because its set of 00039 // children is meaningful. 00040 //////////////////////////////////////////////////////////////////// 00041 bool SwitchNode:: 00042 safe_to_combine() const { 00043 return false; 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: SwitchNode::safe_to_combine_children 00048 // Access: Public, Virtual 00049 // Description: Returns true if it is generally safe to combine the 00050 // children of this PandaNode with each other. For 00051 // instance, an LODNode's children should not be 00052 // combined with each other, because the set of children 00053 // is meaningful. 00054 //////////////////////////////////////////////////////////////////// 00055 bool SwitchNode:: 00056 safe_to_combine_children() const { 00057 return false; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: SwitchNode::CData::write_datagram 00062 // Access: Public, Virtual 00063 // Description: Writes the contents of this object to the datagram 00064 // for shipping out to a Bam file. 00065 //////////////////////////////////////////////////////////////////// 00066 void SwitchNode::CData:: 00067 write_datagram(BamWriter *manager, Datagram &dg) const { 00068 dg.add_int32(_visible_child); 00069 } 00070 00071 //////////////////////////////////////////////////////////////////// 00072 // Function: SwitchNode::CData::fillin 00073 // Access: Public, Virtual 00074 // Description: This internal function is called by make_from_bam to 00075 // read in all of the relevant data from the BamFile for 00076 // the new SwitchNode. 00077 //////////////////////////////////////////////////////////////////// 00078 void SwitchNode::CData:: 00079 fillin(DatagramIterator &scan, BamReader *manager) { 00080 _visible_child = scan.get_int32(); 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: SwitchNode::Copy Constructor 00085 // Access: Protected 00086 // Description: 00087 //////////////////////////////////////////////////////////////////// 00088 SwitchNode:: 00089 SwitchNode(const SwitchNode ©) : 00090 SelectiveChildNode(copy), 00091 _cycler(copy._cycler) 00092 { 00093 } 00094 00095 //////////////////////////////////////////////////////////////////// 00096 // Function: SwitchNode::make_copy 00097 // Access: Public, Virtual 00098 // Description: Returns a newly-allocated Node that is a shallow copy 00099 // of this one. It will be a different Node pointer, 00100 // but its internal data may or may not be shared with 00101 // that of the original Node. 00102 //////////////////////////////////////////////////////////////////// 00103 PandaNode *SwitchNode:: 00104 make_copy() const { 00105 return new SwitchNode(*this); 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: SwitchNode::cull_callback 00110 // Access: Public, Virtual 00111 // Description: This function will be called during the cull 00112 // traversal to perform any additional operations that 00113 // should be performed at cull time. This may include 00114 // additional manipulation of render state or additional 00115 // visible/invisible decisions, or any other arbitrary 00116 // operation. 00117 // 00118 // Note that this function will *not* be called unless 00119 // set_cull_callback() is called in the constructor of 00120 // the derived class. It is necessary to call 00121 // set_cull_callback() to indicated that we require 00122 // cull_callback() to be called. 00123 // 00124 // By the time this function is called, the node has 00125 // already passed the bounding-volume test for the 00126 // viewing frustum, and the node's transform and state 00127 // have already been applied to the indicated 00128 // CullTraverserData object. 00129 // 00130 // The return value is true if this node should be 00131 // visible, or false if it should be culled. 00132 //////////////////////////////////////////////////////////////////// 00133 bool SwitchNode:: 00134 cull_callback(CullTraverser *, CullTraverserData &) { 00135 select_child(get_visible_child()); 00136 return true; 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: SwitchNode::get_first_visible_child 00141 // Access: Public, Virtual 00142 // Description: Returns the index number of the first visible child 00143 // of this node, or a number >= get_num_children() if 00144 // there are no visible children of this node. This is 00145 // called during the cull traversal, but only if 00146 // has_selective_visibility() has already returned true. 00147 // See has_selective_visibility(). 00148 //////////////////////////////////////////////////////////////////// 00149 int SwitchNode:: 00150 get_first_visible_child() const { 00151 return get_visible_child(); 00152 } 00153 00154 //////////////////////////////////////////////////////////////////// 00155 // Function: SwitchNode::has_single_child_visibility 00156 // Access: Public, Virtual 00157 // Description: Should be overridden by derived classes to return 00158 // true if this kind of node has the special property 00159 // that just one of its children is visible at any given 00160 // time, and furthermore that the particular visible 00161 // child can be determined without reference to any 00162 // external information (such as a camera). At present, 00163 // only SequenceNodes and SwitchNodes fall into this 00164 // category. 00165 // 00166 // If this function returns true, get_visible_child() 00167 // can be called to return the index of the 00168 // currently-visible child. 00169 //////////////////////////////////////////////////////////////////// 00170 bool SwitchNode:: 00171 has_single_child_visibility() const { 00172 return true; 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function: SwitchNode::get_visible_child 00177 // Access: Published, Virtual 00178 // Description: Returns the index of the child that should be visible. 00179 //////////////////////////////////////////////////////////////////// 00180 int SwitchNode:: 00181 get_visible_child() const { 00182 CDReader cdata(_cycler); 00183 return cdata->_visible_child; 00184 } 00185 00186 //////////////////////////////////////////////////////////////////// 00187 // Function: SwitchNode::register_with_read_factory 00188 // Access: Public, Static 00189 // Description: Tells the BamReader how to create objects of type 00190 // SwitchNode. 00191 //////////////////////////////////////////////////////////////////// 00192 void SwitchNode:: 00193 register_with_read_factory() { 00194 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: SwitchNode::write_datagram 00199 // Access: Public, Virtual 00200 // Description: Writes the contents of this object to the datagram 00201 // for shipping out to a Bam file. 00202 //////////////////////////////////////////////////////////////////// 00203 void SwitchNode:: 00204 write_datagram(BamWriter *manager, Datagram &dg) { 00205 SelectiveChildNode::write_datagram(manager, dg); 00206 manager->write_cdata(dg, _cycler); 00207 } 00208 00209 //////////////////////////////////////////////////////////////////// 00210 // Function: SwitchNode::make_from_bam 00211 // Access: Protected, Static 00212 // Description: This function is called by the BamReader's factory 00213 // when a new object of type SwitchNode is encountered 00214 // in the Bam file. It should create the SwitchNode 00215 // and extract its information from the file. 00216 //////////////////////////////////////////////////////////////////// 00217 TypedWritable *SwitchNode:: 00218 make_from_bam(const FactoryParams ¶ms) { 00219 SwitchNode *node = new SwitchNode(""); 00220 DatagramIterator scan; 00221 BamReader *manager; 00222 00223 parse_params(params, scan, manager); 00224 node->fillin(scan, manager); 00225 00226 return node; 00227 } 00228 00229 //////////////////////////////////////////////////////////////////// 00230 // Function: SwitchNode::fillin 00231 // Access: Protected 00232 // Description: This internal function is called by make_from_bam to 00233 // read in all of the relevant data from the BamFile for 00234 // the new SwitchNode. 00235 //////////////////////////////////////////////////////////////////// 00236 void SwitchNode:: 00237 fillin(DatagramIterator &scan, BamReader *manager) { 00238 SelectiveChildNode::fillin(scan, manager); 00239 manager->read_cdata(scan, _cycler); 00240 }