Panda3D
modelNode.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 modelNode.cxx
10  * @author drose
11  * @date 2002-03-16
12  */
13 
14 #include "modelNode.h"
15 
16 #include "bamReader.h"
17 #include "datagram.h"
18 #include "datagramIterator.h"
19 
20 TypeHandle ModelNode::_type_handle;
21 
22 
23 /**
24  * Returns a newly-allocated Node that is a shallow copy of this one. It will
25  * be a different Node pointer, but its internal data may or may not be shared
26  * with that of the original Node.
27  */
29 make_copy() const {
30  return new ModelNode(*this);
31 }
32 
33 /**
34  * Collapses this PandaNode with the other PandaNode, if possible, and returns
35  * a pointer to the combined PandaNode, or NULL if the two PandaNodes cannot
36  * safely be combined.
37  *
38  * The return value may be this, other, or a new PandaNode altogether.
39  *
40  * This function is called from GraphReducer::flatten(), and need not deal
41  * with children; its job is just to decide whether to collapse the two
42  * PandaNodes and what the collapsed PandaNode should look like.
43  */
46  if (_preserve_transform == PT_drop_node) {
47  // If we have PT_drop_node set, we always yield to the other node.
48  return other;
49  }
50 
51  return PandaNode::combine_with(other);
52 }
53 
54 /**
55  * Returns true if it is generally safe to flatten out this particular kind of
56  * Node by duplicating instances, false otherwise (for instance, a Camera
57  * cannot be safely flattened, because the Camera pointer itself is
58  * meaningful).
59  */
60 bool ModelNode::
61 safe_to_flatten() const {
62  return _preserve_transform == PT_drop_node;
63 }
64 
65 /**
66  * Returns true if a flatten operation may safely continue past this node, or
67  * false if nodes below this node may not be molested.
68  */
69 bool ModelNode::
71  return _preserve_transform != PT_no_touch;
72 }
73 
74 /**
75  * Returns true if it is generally safe to transform this particular kind of
76  * Node by calling the xform() method, false otherwise. For instance, it's
77  * usually a bad idea to attempt to xform a Character.
78  */
79 bool ModelNode::
81  return _preserve_transform == PT_none || _preserve_transform == PT_drop_node;
82 }
83 
84 /**
85  * Returns true if it is safe to automatically adjust the transform on this
86  * kind of node. Usually, this is only a bad idea if the user expects to find
87  * a particular transform on the node.
88  *
89  * ModelNodes with the preserve_transform flag set are presently the only
90  * kinds of nodes that should not have their transform even adjusted.
91  */
92 bool ModelNode::
94  return _preserve_transform != PT_local && _preserve_transform != PT_no_touch;
95 }
96 
97 /**
98  * Returns true if it is generally safe to combine this particular kind of
99  * PandaNode with other kinds of PandaNodes of compatible type, adding
100  * children or whatever. For instance, an LODNode should not be combined with
101  * any other PandaNode, because its set of children is meaningful.
102  */
103 bool ModelNode::
105  return _preserve_transform == PT_drop_node;
106 }
107 
108 /**
109  * Returns true if the node's name has extrinsic meaning and must be preserved
110  * across a flatten operation, false otherwise.
111  */
112 bool ModelNode::
113 preserve_name() const {
114  return _preserve_transform != PT_drop_node && _preserve_transform != PT_no_touch;
115 }
116 
117 /**
118  * Returns the union of all attributes from SceneGraphReducer::AttribTypes
119  * that may not safely be applied to the vertices of this node. If this is
120  * nonzero, these attributes must be dropped at this node as a state change.
121  *
122  * This is a generalization of safe_to_transform().
123  */
124 int ModelNode::
126  return _preserve_attributes;
127 }
128 
129 /**
130  * Tells the BamReader how to create objects of type ModelNode.
131  */
132 void ModelNode::
134  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
135 }
136 
137 /**
138  * This tests the transform to make sure it's within the specified limits.
139  * It's done so we can assert to see when an invalid transform is being
140  * applied.
141  */
142 void ModelNode::
143 test_transform(const TransformState *ts) const {
144  LPoint3 pos(ts->get_pos());
145  nassertv(pos[0] < _transform_limit);
146  nassertv(pos[0] > -_transform_limit);
147  nassertv(pos[1] < _transform_limit);
148  nassertv(pos[1] > -_transform_limit);
149  nassertv(pos[2] < _transform_limit);
150  nassertv(pos[2] > -_transform_limit);
151 }
152 
153 /**
154  * node hook. This function handles outside (non-physics) actions on the
155  * actor and updates the internal representation of the node. i.e. copy from
156  * PandaNode to PhysicsObject
157  */
158 void ModelNode::
159 transform_changed() {
160  PandaNode::transform_changed();
161  // get the transform
162  CPT(TransformState) transform = get_transform();
163 
164  if (_transform_limit > 0.0) {
165  test_transform(transform);
166  }
167 }
168 
169 
170 /**
171  * Writes the contents of this object to the datagram for shipping out to a
172  * Bam file.
173  */
174 void ModelNode::
176  PandaNode::write_datagram(manager, dg);
177  dg.add_uint8((int)_preserve_transform);
178  dg.add_uint16(_preserve_attributes);
179 }
180 
181 /**
182  * This function is called by the BamReader's factory when a new object of
183  * type ModelNode is encountered in the Bam file. It should create the
184  * ModelNode and extract its information from the file.
185  */
186 TypedWritable *ModelNode::
187 make_from_bam(const FactoryParams &params) {
188  ModelNode *node = new ModelNode("");
189  DatagramIterator scan;
190  BamReader *manager;
191 
192  parse_params(params, scan, manager);
193  node->fillin(scan, manager);
194 
195  return node;
196 }
197 
198 /**
199  * This internal function is called by make_from_bam to read in all of the
200  * relevant data from the BamFile for the new ModelNode.
201  */
202 void ModelNode::
203 fillin(DatagramIterator &scan, BamReader *manager) {
204  PandaNode::fillin(scan, manager);
205 
206  _preserve_transform = (PreserveTransform)scan.get_uint8();
207  _preserve_attributes = scan.get_uint16();
208 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
static void register_with_read_factory()
Tells the BamReader how to create objects of type ModelNode.
Definition: modelNode.cxx:133
Indicates a coordinate-system transform on vertices.
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
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
virtual bool preserve_name() const
Returns true if the node's name has extrinsic meaning and must be preserved across a flatten operatio...
Definition: modelNode.cxx:113
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual PandaNode * combine_with(PandaNode *other)
Collapses this PandaNode with the other PandaNode, if possible, and returns a pointer to the combined...
Definition: pandaNode.cxx:325
virtual bool safe_to_transform() const
Returns true if it is generally safe to transform this particular kind of Node by calling the xform()...
Definition: modelNode.cxx:80
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: modelNode.cxx:175
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
virtual bool safe_to_flatten() const
Returns true if it is generally safe to flatten out this particular kind of Node by duplicating insta...
Definition: modelNode.cxx:61
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
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
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
This node is placed at key points within the scene graph to indicate the roots of "models": subtrees ...
Definition: modelNode.h:31
virtual bool safe_to_modify_transform() const
Returns true if it is safe to automatically adjust the transform on this kind of node.
Definition: modelNode.cxx:93
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: modelNode.cxx:29
get_pos
Returns the pos component of the transform.
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.
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
virtual PandaNode * combine_with(PandaNode *other)
Collapses this PandaNode with the other PandaNode, if possible, and returns a pointer to the combined...
Definition: modelNode.cxx:45
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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: modelNode.cxx:104
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
A class to retrieve the individual data elements previously stored in a Datagram.
virtual int get_unsafe_to_apply_attribs() const
Returns the union of all attributes from SceneGraphReducer::AttribTypes that may not safely be applie...
Definition: modelNode.cxx:125
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
virtual bool safe_to_flatten_below() const
Returns true if a flatten operation may safely continue past this node, or false if nodes below this ...
Definition: modelNode.cxx:70