Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/sequenceNode.cxx

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

Generated on Fri May 2 00:42:22 2003 for Panda by doxygen1.3