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