Panda3D
 All Classes Functions Variables Enumerations
switchNode.cxx
1 // Filename: switchNode.cxx
2 // Created by: drose (31Jul02)
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 "switchNode.h"
17 #include "cullTraverser.h"
18 
19 TypeHandle SwitchNode::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: SwitchNode::CData::make_copy
23 // Access: Public, Virtual
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 CycleData *SwitchNode::CData::
27 make_copy() const {
28  return new CData(*this);
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: SwitchNode::safe_to_combine
33 // Access: Public, Virtual
34 // Description: Returns true if it is generally safe to combine this
35 // particular kind of PandaNode with other kinds of
36 // PandaNodes of compatible type, adding children or
37 // whatever. For instance, an LODNode should not be
38 // combined with any other PandaNode, because its set of
39 // children is meaningful.
40 ////////////////////////////////////////////////////////////////////
41 bool SwitchNode::
42 safe_to_combine() const {
43  return false;
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: SwitchNode::safe_to_combine_children
48 // Access: Public, Virtual
49 // Description: Returns true if it is generally safe to combine the
50 // children of this PandaNode with each other. For
51 // instance, an LODNode's children should not be
52 // combined with each other, because the set of children
53 // is meaningful.
54 ////////////////////////////////////////////////////////////////////
55 bool SwitchNode::
57  return false;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: SwitchNode::CData::write_datagram
62 // Access: Public, Virtual
63 // Description: Writes the contents of this object to the datagram
64 // for shipping out to a Bam file.
65 ////////////////////////////////////////////////////////////////////
66 void SwitchNode::CData::
67 write_datagram(BamWriter *manager, Datagram &dg) const {
68  dg.add_int32(_visible_child);
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: SwitchNode::CData::fillin
73 // Access: Public, Virtual
74 // Description: This internal function is called by make_from_bam to
75 // read in all of the relevant data from the BamFile for
76 // the new SwitchNode.
77 ////////////////////////////////////////////////////////////////////
78 void SwitchNode::CData::
79 fillin(DatagramIterator &scan, BamReader *manager) {
80  _visible_child = scan.get_int32();
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: SwitchNode::Copy Constructor
85 // Access: Protected
86 // Description:
87 ////////////////////////////////////////////////////////////////////
88 SwitchNode::
89 SwitchNode(const SwitchNode &copy) :
90  SelectiveChildNode(copy),
91  _cycler(copy._cycler)
92 {
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: SwitchNode::make_copy
97 // Access: Public, Virtual
98 // Description: Returns a newly-allocated Node that is a shallow copy
99 // of this one. It will be a different Node pointer,
100 // but its internal data may or may not be shared with
101 // that of the original Node.
102 ////////////////////////////////////////////////////////////////////
104 make_copy() const {
105  return new SwitchNode(*this);
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: SwitchNode::cull_callback
110 // Access: Public, Virtual
111 // Description: This function will be called during the cull
112 // traversal to perform any additional operations that
113 // should be performed at cull time. This may include
114 // additional manipulation of render state or additional
115 // visible/invisible decisions, or any other arbitrary
116 // operation.
117 //
118 // Note that this function will *not* be called unless
119 // set_cull_callback() is called in the constructor of
120 // the derived class. It is necessary to call
121 // set_cull_callback() to indicated that we require
122 // cull_callback() to be called.
123 //
124 // By the time this function is called, the node has
125 // already passed the bounding-volume test for the
126 // viewing frustum, and the node's transform and state
127 // have already been applied to the indicated
128 // CullTraverserData object.
129 //
130 // The return value is true if this node should be
131 // visible, or false if it should be culled.
132 ////////////////////////////////////////////////////////////////////
133 bool SwitchNode::
135  select_child(get_visible_child());
136  return true;
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: SwitchNode::get_first_visible_child
141 // Access: Public, Virtual
142 // Description: Returns the index number of the first visible child
143 // of this node, or a number >= get_num_children() if
144 // there are no visible children of this node. This is
145 // called during the cull traversal, but only if
146 // has_selective_visibility() has already returned true.
147 // See has_selective_visibility().
148 ////////////////////////////////////////////////////////////////////
149 int SwitchNode::
151  return get_visible_child();
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: SwitchNode::has_single_child_visibility
156 // Access: Public, Virtual
157 // Description: Should be overridden by derived classes to return
158 // true if this kind of node has the special property
159 // that just one of its children is visible at any given
160 // time, and furthermore that the particular visible
161 // child can be determined without reference to any
162 // external information (such as a camera). At present,
163 // only SequenceNodes and SwitchNodes fall into this
164 // category.
165 //
166 // If this function returns true, get_visible_child()
167 // can be called to return the index of the
168 // currently-visible child.
169 ////////////////////////////////////////////////////////////////////
170 bool SwitchNode::
172  return true;
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: SwitchNode::get_visible_child
177 // Access: Published, Virtual
178 // Description: Returns the index of the child that should be visible.
179 ////////////////////////////////////////////////////////////////////
180 int SwitchNode::
182  CDReader cdata(_cycler);
183  return cdata->_visible_child;
184 }
185 
186 ////////////////////////////////////////////////////////////////////
187 // Function: SwitchNode::register_with_read_factory
188 // Access: Public, Static
189 // Description: Tells the BamReader how to create objects of type
190 // SwitchNode.
191 ////////////////////////////////////////////////////////////////////
192 void SwitchNode::
194  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: SwitchNode::write_datagram
199 // Access: Public, Virtual
200 // Description: Writes the contents of this object to the datagram
201 // for shipping out to a Bam file.
202 ////////////////////////////////////////////////////////////////////
203 void SwitchNode::
206  manager->write_cdata(dg, _cycler);
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: SwitchNode::make_from_bam
211 // Access: Protected, Static
212 // Description: This function is called by the BamReader's factory
213 // when a new object of type SwitchNode is encountered
214 // in the Bam file. It should create the SwitchNode
215 // and extract its information from the file.
216 ////////////////////////////////////////////////////////////////////
217 TypedWritable *SwitchNode::
218 make_from_bam(const FactoryParams &params) {
219  SwitchNode *node = new SwitchNode("");
220  DatagramIterator scan;
221  BamReader *manager;
222 
223  parse_params(params, scan, manager);
224  node->fillin(scan, manager);
225 
226  return node;
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: SwitchNode::fillin
231 // Access: Protected
232 // Description: This internal function is called by make_from_bam to
233 // read in all of the relevant data from the BamFile for
234 // the new SwitchNode.
235 ////////////////////////////////////////////////////////////////////
236 void SwitchNode::
237 fillin(DatagramIterator &scan, BamReader *manager) {
238  SelectiveChildNode::fillin(scan, manager);
239  manager->read_cdata(scan, _cycler);
240 }
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
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 bool cull_callback(CullTraverser *trav, CullTraverserData &data)
This function will be called during the cull traversal to perform any additional operations that shou...
Definition: switchNode.cxx:134
A base class for nodes like LODNode and SequenceNode that select only one visible child at a time...
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
PN_int32 get_int32()
Extracts a signed 32-bit integer.
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
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: switchNode.cxx:204
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: switchNode.cxx:104
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual bool safe_to_combine() const
Returns true if it is generally safe to combine this particular kind of PandaNode with other kinds of...
Definition: switchNode.cxx:42
virtual int get_first_visible_child() const
Returns the index number of the first visible child of this node, or a number >= get_num_children() i...
Definition: switchNode.cxx:150
virtual bool safe_to_combine_children() const
Returns true if it is generally safe to combine the children of this PandaNode with each other...
Definition: switchNode.cxx:56
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
virtual int get_visible_child() const
Returns the index of the child that should be visible.
Definition: switchNode.cxx:181
static void register_with_read_factory()
Tells the BamReader how to create objects of type SwitchNode.
Definition: switchNode.cxx:193
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:159
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
virtual bool has_single_child_visibility() const
Should be overridden by derived classes to return true if this kind of node has the special property ...
Definition: switchNode.cxx:171
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
A node that renders only one of its children, according to the user's indication. ...
Definition: switchNode.h:27