Panda3D
 All Classes Functions Variables Enumerations
callbackNode.cxx
1 // Filename: callbackNode.cxx
2 // Created by: drose (13Mar09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pandabase.h"
16 #include "callbackNode.h"
17 #include "cullTraverser.h"
18 #include "nodeCullCallbackData.h"
19 #include "cullableObject.h"
20 #include "cullHandler.h"
21 #include "omniBoundingVolume.h"
22 #include "config_pgraph.h"
23 
24 TypeHandle CallbackNode::_type_handle;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: CallbackNode::Constructor
28 // Access: Published
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 CallbackNode::
32 CallbackNode(const string &name) :
33  PandaNode(name)
34 {
35  PandaNode::set_cull_callback();
36 
37  // Set up a default, infinite bounding volume, unless the user tells
38  // us otherwise. Not sure if this is a great idea, because it means
39  // a naive user will never set the bounding volume and always
40  // trigger the callback--but that's not altogether a bad default
41  // behavior.
42  set_internal_bounds(new OmniBoundingVolume);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: CallbackNode::Copy Constructor
47 // Access: Protected
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 CallbackNode::
51 CallbackNode(const CallbackNode &copy) :
52  PandaNode(copy),
53  _cycler(copy._cycler)
54 {
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: CallbackNode::make_copy
59 // Access: Public, Virtual
60 // Description: Returns a newly-allocated Node that is a shallow copy
61 // of this one. It will be a different Node pointer,
62 // but its internal data may or may not be shared with
63 // that of the original Node.
64 ////////////////////////////////////////////////////////////////////
66 make_copy() const {
67  return new CallbackNode(*this);
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: CallbackNode::safe_to_combine
72 // Access: Public, Virtual
73 // Description: Returns true if it is generally safe to combine this
74 // particular kind of PandaNode with other kinds of
75 // PandaNodes of compatible type, adding children or
76 // whatever. For instance, an LODNode should not be
77 // combined with any other PandaNode, because its set of
78 // children is meaningful.
79 ////////////////////////////////////////////////////////////////////
80 bool CallbackNode::
81 safe_to_combine() const {
82  return false;
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: CallbackNode::cull_callback
87 // Access: Public, Virtual
88 // Description: This function will be called during the cull
89 // traversal to perform any additional operations that
90 // should be performed at cull time. This may include
91 // additional manipulation of render state or additional
92 // visible/invisible decisions, or any other arbitrary
93 // operation.
94 //
95 // Note that this function will *not* be called unless
96 // set_cull_callback() is called in the constructor of
97 // the derived class. It is necessary to call
98 // set_cull_callback() to indicated that we require
99 // cull_callback() to be called.
100 //
101 // By the time this function is called, the node has
102 // already passed the bounding-volume test for the
103 // viewing frustum, and the node's transform and state
104 // have already been applied to the indicated
105 // CullTraverserData object.
106 //
107 // The return value is true if this node should be
108 // visible, or false if it should be culled.
109 ////////////////////////////////////////////////////////////////////
110 bool CallbackNode::
113  if (cbobj != (CallbackObject *)NULL) {
114  NodeCullCallbackData cbdata(trav, data);
115  cbobj->do_callback(&cbdata);
116 
117  // No further cull: the callback takes care of all of it.
118  return false;
119  }
120 
121  // Recurse onto the node's children.
122  return true;
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: CallbackNode::is_renderable
127 // Access: Public, Virtual
128 // Description: Returns true if there is some value to visiting this
129 // particular node during the cull traversal for any
130 // camera, false otherwise. This will be used to
131 // optimize the result of get_net_draw_show_mask(), so
132 // that any subtrees that contain only nodes for which
133 // is_renderable() is false need not be visited.
134 ////////////////////////////////////////////////////////////////////
135 bool CallbackNode::
136 is_renderable() const {
137  return true;
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: CallbackNode::add_for_draw
142 // Access: Public, Virtual
143 // Description: Adds the node's contents to the CullResult we are
144 // building up during the cull traversal, so that it
145 // will be drawn at render time. For most nodes other
146 // than GeomNodes, this is a do-nothing operation.
147 ////////////////////////////////////////////////////////////////////
148 void CallbackNode::
150  if (pgraph_cat.is_spam()) {
151  pgraph_cat.spam()
152  << "Found " << *this << " in state " << *data._state
153  << " draw_mask = " << data._draw_mask << "\n";
154  }
155 
156  // OK, render this node. Rendering this node means creating a
157  // CullableObject for the draw_callback, if any. We don't need to
158  // pass any Geoms, however.
160  if (cbobj != (CallbackObject *)NULL) {
161  CullableObject *object =
162  new CullableObject(NULL, data._state,
163  data.get_internal_transform(trav));
164  object->set_draw_callback(cbobj);
165  trav->get_cull_handler()->record_object(object, trav);
166  }
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: CallbackNode::output
171 // Access: Public, Virtual
172 // Description: Writes a brief description of the node to the
173 // indicated output stream. This is invoked by the <<
174 // operator. It may be overridden in derived classes to
175 // include some information relevant to the class.
176 ////////////////////////////////////////////////////////////////////
177 void CallbackNode::
178 output(ostream &out) const {
179  PandaNode::output(out);
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: CallbackNode::register_with_read_factory
184 // Access: Public, Static
185 // Description: Tells the BamReader how to create objects of type
186 // CallbackNode.
187 ////////////////////////////////////////////////////////////////////
188 void CallbackNode::
190  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
191 }
192 
193 ////////////////////////////////////////////////////////////////////
194 // Function: CallbackNode::write_datagram
195 // Access: Public, Virtual
196 // Description: Writes the contents of this object to the datagram
197 // for shipping out to a Bam file.
198 ////////////////////////////////////////////////////////////////////
199 void CallbackNode::
201  PandaNode::write_datagram(manager, dg);
202  manager->write_cdata(dg, _cycler);
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: CallbackNode::make_from_bam
207 // Access: Protected, Static
208 // Description: This function is called by the BamReader's factory
209 // when a new object of type CallbackNode is encountered
210 // in the Bam file. It should create the CallbackNode
211 // and extract its information from the file.
212 ////////////////////////////////////////////////////////////////////
213 TypedWritable *CallbackNode::
214 make_from_bam(const FactoryParams &params) {
215  CallbackNode *node = new CallbackNode("");
216  DatagramIterator scan;
217  BamReader *manager;
218 
219  parse_params(params, scan, manager);
220  node->fillin(scan, manager);
221 
222  return node;
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: CallbackNode::fillin
227 // Access: Protected
228 // Description: This internal function is called by make_from_bam to
229 // read in all of the relevant data from the BamFile for
230 // the new CallbackNode.
231 ////////////////////////////////////////////////////////////////////
232 void CallbackNode::
233 fillin(DatagramIterator &scan, BamReader *manager) {
234  PandaNode::fillin(scan, manager);
235  manager->read_cdata(scan, _cycler);
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: CallbackNode::CData::make_copy
240 // Access: Public, Virtual
241 // Description:
242 ////////////////////////////////////////////////////////////////////
243 CycleData *CallbackNode::CData::
244 make_copy() const {
245  return new CData(*this);
246 }
247 
248 ////////////////////////////////////////////////////////////////////
249 // Function: CallbackNode::CData::write_datagram
250 // Access: Public, Virtual
251 // Description: Writes the contents of this object to the datagram
252 // for shipping out to a Bam file.
253 ////////////////////////////////////////////////////////////////////
254 void CallbackNode::CData::
255 write_datagram(BamWriter *manager, Datagram &dg) const {
256 }
257 
258 ////////////////////////////////////////////////////////////////////
259 // Function: CallbackNode::CData::fillin
260 // Access: Public, Virtual
261 // Description: This internal function is called by make_from_bam to
262 // read in all of the relevant data from the BamFile for
263 // the new CallbackNode.
264 ////////////////////////////////////////////////////////////////////
265 void CallbackNode::CData::
266 fillin(DatagramIterator &scan, BamReader *manager) {
267 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: pandaNode.cxx:4164
virtual void add_for_draw(CullTraverser *trav, CullTraverserData &data)
Adds the node&#39;s contents to the CullResult we are building up during the cull traversal, so that it will be drawn at render time.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:747
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:398
This collects together the pieces of data that are accumulated for each node while walking the scene ...
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
A special node that can issue arbitrary callbacks to user code, either during the cull or draw traver...
Definition: callbackNode.h:28
CallbackObject * get_cull_callback() const
Returns the CallbackObject set by set_cull_callback().
Definition: callbackNode.I:69
virtual void do_callback(CallbackData *cbdata)
This method called when the callback is triggered; it replaces* the original function.
CallbackObject * get_draw_callback() const
Returns the CallbackObject set by set_draw_callback().
Definition: callbackNode.I:124
The smallest atom of cull.
virtual void output(ostream &out) const
Writes a brief description of the node to the indicated output stream.
virtual void record_object(CullableObject *object, const CullTraverser *traverser)
This callback function is intended to be overridden by a derived class.
Definition: cullHandler.cxx:52
CullHandler * get_cull_handler() const
Returns the object that will receive the culled Geoms.
virtual bool is_renderable() const
Returns true if there is some value to visiting this particular node during the cull traversal for an...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
This is a generic object that can be assigned to a callback at various points in the rendering proces...
void set_draw_callback(CallbackObject *draw_callback)
Specifies a CallbackObject that will be responsible for drawing this object.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data)
This function will be called during the cull traversal to perform any additional operations that shou...
This is a special kind of GeometricBoundingVolume that fills all of space.
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This kind of CallbackData is passed to the CallbackObject added to CallbackNode:set_cull_callback().
virtual bool safe_to_combine() const
Returns true if it is generally safe to combine this particular kind of PandaNode with other kinds of...
static void register_with_read_factory()
Tells the BamReader how to create objects of type CallbackNode.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48