Panda3D

callbackNode.cxx

00001 // Filename: callbackNode.cxx
00002 // Created by:  drose (13Mar09)
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 "callbackNode.h"
00017 #include "cullTraverser.h"
00018 #include "nodeCullCallbackData.h"
00019 #include "cullableObject.h"
00020 #include "cullHandler.h"
00021 #include "omniBoundingVolume.h"
00022 
00023 TypeHandle CallbackNode::_type_handle;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: CallbackNode::Constructor
00027 //       Access: Published
00028 //  Description:
00029 ////////////////////////////////////////////////////////////////////
00030 CallbackNode::
00031 CallbackNode(const string &name) :
00032   PandaNode(name)
00033 {
00034   PandaNode::set_cull_callback();
00035 
00036   // Set up a default, infinite bounding volume, unless the user tells
00037   // us otherwise.  Not sure if this is a great idea, because it means
00038   // a naive user will never set the bounding volume and always
00039   // trigger the callback--but that's not altogether a bad default
00040   // behavior.
00041   set_internal_bounds(new OmniBoundingVolume);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: CallbackNode::Copy Constructor
00046 //       Access: Protected
00047 //  Description:
00048 ////////////////////////////////////////////////////////////////////
00049 CallbackNode::
00050 CallbackNode(const CallbackNode &copy) :
00051   PandaNode(copy),
00052   _cycler(copy._cycler)
00053 {
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: CallbackNode::make_copy
00058 //       Access: Public, Virtual
00059 //  Description: Returns a newly-allocated Node that is a shallow copy
00060 //               of this one.  It will be a different Node pointer,
00061 //               but its internal data may or may not be shared with
00062 //               that of the original Node.
00063 ////////////////////////////////////////////////////////////////////
00064 PandaNode *CallbackNode::
00065 make_copy() const {
00066   return new CallbackNode(*this);
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: CallbackNode::safe_to_combine
00071 //       Access: Public, Virtual
00072 //  Description: Returns true if it is generally safe to combine this
00073 //               particular kind of PandaNode with other kinds of
00074 //               PandaNodes of compatible type, adding children or
00075 //               whatever.  For instance, an LODNode should not be
00076 //               combined with any other PandaNode, because its set of
00077 //               children is meaningful.
00078 ////////////////////////////////////////////////////////////////////
00079 bool CallbackNode::
00080 safe_to_combine() const {
00081   return false;
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: CallbackNode::cull_callback
00086 //       Access: Public, Virtual
00087 //  Description: This function will be called during the cull
00088 //               traversal to perform any additional operations that
00089 //               should be performed at cull time.  This may include
00090 //               additional manipulation of render state or additional
00091 //               visible/invisible decisions, or any other arbitrary
00092 //               operation.
00093 //
00094 //               Note that this function will *not* be called unless
00095 //               set_cull_callback() is called in the constructor of
00096 //               the derived class.  It is necessary to call
00097 //               set_cull_callback() to indicated that we require
00098 //               cull_callback() to be called.
00099 //
00100 //               By the time this function is called, the node has
00101 //               already passed the bounding-volume test for the
00102 //               viewing frustum, and the node's transform and state
00103 //               have already been applied to the indicated
00104 //               CullTraverserData object.
00105 //
00106 //               The return value is true if this node should be
00107 //               visible, or false if it should be culled.
00108 ////////////////////////////////////////////////////////////////////
00109 bool CallbackNode::
00110 cull_callback(CullTraverser *trav, CullTraverserData &data) {
00111   CallbackObject *cbobj = get_cull_callback();
00112   if (cbobj != (CallbackObject *)NULL) {
00113     NodeCullCallbackData cbdata(trav, data);
00114     cbobj->do_callback(&cbdata);
00115 
00116     // No further cull: the callback takes care of all of it.
00117     return false;
00118   }
00119 
00120   // Recurse onto the node's children.
00121   return true;
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: CallbackNode::is_renderable
00126 //       Access: Public, Virtual
00127 //  Description: Returns true if there is some value to visiting this
00128 //               particular node during the cull traversal for any
00129 //               camera, false otherwise.  This will be used to
00130 //               optimize the result of get_net_draw_show_mask(), so
00131 //               that any subtrees that contain only nodes for which
00132 //               is_renderable() is false need not be visited.
00133 ////////////////////////////////////////////////////////////////////
00134 bool CallbackNode::
00135 is_renderable() const {
00136   return true;
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: CallbackNode::add_for_draw
00141 //       Access: Public, Virtual
00142 //  Description: Adds the node's contents to the CullResult we are
00143 //               building up during the cull traversal, so that it
00144 //               will be drawn at render time.  For most nodes other
00145 //               than GeomNodes, this is a do-nothing operation.
00146 ////////////////////////////////////////////////////////////////////
00147 void CallbackNode::
00148 add_for_draw(CullTraverser *trav, CullTraverserData &data) {
00149   // OK, render this node.  Rendering this node means creating a
00150   // CullableObject for the draw_callback, if any.  We don't need to
00151   // pass any Geoms, however.
00152   CallbackObject *cbobj = get_draw_callback();
00153   if (cbobj != (CallbackObject *)NULL) {
00154     CullableObject *object = 
00155       new CullableObject(NULL, data._state,
00156                          data.get_net_transform(trav),
00157                          data.get_modelview_transform(trav),
00158                          trav->get_gsg());
00159     object->set_draw_callback(cbobj);
00160     trav->get_cull_handler()->record_object(object, trav);
00161   }
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: CallbackNode::output
00166 //       Access: Public, Virtual
00167 //  Description: Writes a brief description of the node to the
00168 //               indicated output stream.  This is invoked by the <<
00169 //               operator.  It may be overridden in derived classes to
00170 //               include some information relevant to the class.
00171 ////////////////////////////////////////////////////////////////////
00172 void CallbackNode::
00173 output(ostream &out) const {
00174   PandaNode::output(out);
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////
00178 //     Function: CallbackNode::register_with_read_factory
00179 //       Access: Public, Static
00180 //  Description: Tells the BamReader how to create objects of type
00181 //               CallbackNode.
00182 ////////////////////////////////////////////////////////////////////
00183 void CallbackNode::
00184 register_with_read_factory() {
00185   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00186 }
00187 
00188 ////////////////////////////////////////////////////////////////////
00189 //     Function: CallbackNode::write_datagram
00190 //       Access: Public, Virtual
00191 //  Description: Writes the contents of this object to the datagram
00192 //               for shipping out to a Bam file.
00193 ////////////////////////////////////////////////////////////////////
00194 void CallbackNode::
00195 write_datagram(BamWriter *manager, Datagram &dg) {
00196   PandaNode::write_datagram(manager, dg);
00197   manager->write_cdata(dg, _cycler);
00198 }
00199 
00200 ////////////////////////////////////////////////////////////////////
00201 //     Function: CallbackNode::make_from_bam
00202 //       Access: Protected, Static
00203 //  Description: This function is called by the BamReader's factory
00204 //               when a new object of type CallbackNode is encountered
00205 //               in the Bam file.  It should create the CallbackNode
00206 //               and extract its information from the file.
00207 ////////////////////////////////////////////////////////////////////
00208 TypedWritable *CallbackNode::
00209 make_from_bam(const FactoryParams &params) {
00210   CallbackNode *node = new CallbackNode("");
00211   DatagramIterator scan;
00212   BamReader *manager;
00213 
00214   parse_params(params, scan, manager);
00215   node->fillin(scan, manager);
00216 
00217   return node;
00218 }
00219 
00220 ////////////////////////////////////////////////////////////////////
00221 //     Function: CallbackNode::fillin
00222 //       Access: Protected
00223 //  Description: This internal function is called by make_from_bam to
00224 //               read in all of the relevant data from the BamFile for
00225 //               the new CallbackNode.
00226 ////////////////////////////////////////////////////////////////////
00227 void CallbackNode::
00228 fillin(DatagramIterator &scan, BamReader *manager) {
00229   PandaNode::fillin(scan, manager);
00230   manager->read_cdata(scan, _cycler);
00231 }
00232 
00233 ////////////////////////////////////////////////////////////////////
00234 //     Function: CallbackNode::CData::make_copy
00235 //       Access: Public, Virtual
00236 //  Description:
00237 ////////////////////////////////////////////////////////////////////
00238 CycleData *CallbackNode::CData::
00239 make_copy() const {
00240   return new CData(*this);
00241 }
00242 
00243 ////////////////////////////////////////////////////////////////////
00244 //     Function: CallbackNode::CData::write_datagram
00245 //       Access: Public, Virtual
00246 //  Description: Writes the contents of this object to the datagram
00247 //               for shipping out to a Bam file.
00248 ////////////////////////////////////////////////////////////////////
00249 void CallbackNode::CData::
00250 write_datagram(BamWriter *manager, Datagram &dg) const {
00251 }
00252 
00253 ////////////////////////////////////////////////////////////////////
00254 //     Function: CallbackNode::CData::fillin
00255 //       Access: Public, Virtual
00256 //  Description: This internal function is called by make_from_bam to
00257 //               read in all of the relevant data from the BamFile for
00258 //               the new CallbackNode.
00259 ////////////////////////////////////////////////////////////////////
00260 void CallbackNode::CData::
00261 fillin(DatagramIterator &scan, BamReader *manager) {
00262 }
 All Classes Functions Variables Enumerations