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