Panda3D
Loading...
Searching...
No Matches
switchNode.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 switchNode.cxx
10 * @author drose
11 * @date 2002-07-31
12 */
13
14#include "pandabase.h"
15#include "switchNode.h"
16#include "cullTraverser.h"
17
18TypeHandle SwitchNode::_type_handle;
19
20/**
21 *
22 */
23CycleData *SwitchNode::CData::
24make_copy() const {
25 return new CData(*this);
26}
27
28/**
29 * Returns true if it is generally safe to combine this particular kind of
30 * PandaNode with other kinds of PandaNodes of compatible type, adding
31 * children or whatever. For instance, an LODNode should not be combined with
32 * any other PandaNode, because its set of children is meaningful.
33 */
35safe_to_combine() const {
36 return false;
37}
38
39/**
40 * Returns true if it is generally safe to combine the children of this
41 * PandaNode with each other. For instance, an LODNode's children should not
42 * be combined with each other, because the set of children is meaningful.
43 */
46 return false;
47}
48
49/**
50 * Writes the contents of this object to the datagram for shipping out to a
51 * Bam file.
52 */
53void SwitchNode::CData::
54write_datagram(BamWriter *manager, Datagram &dg) const {
55 dg.add_int32(_visible_child);
56}
57
58/**
59 * This internal function is called by make_from_bam to read in all of the
60 * relevant data from the BamFile for the new SwitchNode.
61 */
62void SwitchNode::CData::
63fillin(DatagramIterator &scan, BamReader *manager) {
64 _visible_child = scan.get_int32();
65}
66
67/**
68 *
69 */
70SwitchNode::
71SwitchNode(const SwitchNode &copy) :
73 _cycler(copy._cycler)
74{
75}
76
77/**
78 * Returns a newly-allocated Node that is a shallow copy of this one. It will
79 * be a different Node pointer, but its internal data may or may not be shared
80 * with that of the original Node.
81 */
83make_copy() const {
84 return new SwitchNode(*this);
85}
86
87/**
88 * This function will be called during the cull traversal to perform any
89 * additional operations that should be performed at cull time. This may
90 * include additional manipulation of render state or additional
91 * visible/invisible decisions, or any other arbitrary operation.
92 *
93 * Note that this function will *not* be called unless set_cull_callback() is
94 * called in the constructor of the derived class. It is necessary to call
95 * set_cull_callback() to indicated that we require cull_callback() to be
96 * called.
97 *
98 * By the time this function is called, the node has already passed the
99 * bounding-volume test for the viewing frustum, and the node's transform and
100 * state have already been applied to the indicated CullTraverserData object.
101 *
102 * The return value is true if this node should be visible, or false if it
103 * should be culled.
104 */
107 select_child(get_visible_child());
108 return true;
109}
110
111/**
112 * Returns the index number of the first visible child of this node, or a
113 * number >= get_num_children() if there are no visible children of this node.
114 * This is called during the cull traversal, but only if
115 * has_selective_visibility() has already returned true. See
116 * has_selective_visibility().
117 */
120 return get_visible_child();
121}
122
123/**
124 * Should be overridden by derived classes to return true if this kind of node
125 * has the special property that just one of its children is visible at any
126 * given time, and furthermore that the particular visible child can be
127 * determined without reference to any external information (such as a
128 * camera). At present, only SequenceNodes and SwitchNodes fall into this
129 * category.
130 *
131 * If this function returns true, get_visible_child() can be called to return
132 * the index of the currently-visible child.
133 */
136 return true;
137}
138
139/**
140 * Returns the index of the child that should be visible.
141 */
142int SwitchNode::
143get_visible_child() const {
144 CDReader cdata(_cycler);
145 return cdata->_visible_child;
146}
147
148/**
149 * Tells the BamReader how to create objects of type SwitchNode.
150 */
153 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
154}
155
156/**
157 * Writes the contents of this object to the datagram for shipping out to a
158 * Bam file.
159 */
161write_datagram(BamWriter *manager, Datagram &dg) {
163 manager->write_cdata(dg, _cycler);
164}
165
166/**
167 * This function is called by the BamReader's factory when a new object of
168 * type SwitchNode is encountered in the Bam file. It should create the
169 * SwitchNode and extract its information from the file.
170 */
171TypedWritable *SwitchNode::
172make_from_bam(const FactoryParams &params) {
173 SwitchNode *node = new SwitchNode("");
174 DatagramIterator scan;
175 BamReader *manager;
176
177 parse_params(params, scan, manager);
178 node->fillin(scan, manager);
179
180 return node;
181}
182
183/**
184 * This internal function is called by make_from_bam to read in all of the
185 * relevant data from the BamFile for the new SwitchNode.
186 */
187void SwitchNode::
188fillin(DatagramIterator &scan, BamReader *manager) {
189 SelectiveChildNode::fillin(scan, manager);
190 manager->read_cdata(scan, _cycler);
191}
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
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.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
A single page of data maintained by a PipelineCycler.
Definition cycleData.h:50
A class to retrieve the individual data elements previously stored in a Datagram.
int32_t get_int32()
Extracts a signed 32-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition datagram.I:67
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A base class for nodes like LODNode and SequenceNode that select only one visible child at a time.
A node that renders only one of its children, according to the user's indication.
Definition switchNode.h:25
virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data)
This function will be called during the cull traversal to perform any additional operations that shou...
virtual bool safe_to_combine() const
Returns true if it is generally safe to combine this particular kind of PandaNode with other kinds of...
virtual bool safe_to_combine_children() const
Returns true if it is generally safe to combine the children of this PandaNode with each other.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static void register_with_read_factory()
Tells the BamReader how to create objects of type SwitchNode.
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...
get_visible_child
Returns the index of the child that should be visible.
Definition switchNode.h:44
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 ...
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.