Panda3D
geomNode.I
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 geomNode.I
10  * @author drose
11  * @date 2002-02-23
12  */
13 
14 /**
15  * Sets the "preserved" flag. When this is true, the GeomNode will be left
16  * untouched by any flatten operations.
17  */
18 INLINE void GeomNode::
19 set_preserved(bool value) {
20  _preserved = value;
21 }
22 
23 /**
24  * Returns the "preserved" flag. When this is true, the GeomNode will be left
25  * untouched by any flatten operations.
26  */
27 INLINE bool GeomNode::
28 get_preserved() const {
29  return _preserved;
30 }
31 
32 /**
33  * Returns the number of geoms in the node.
34  */
35 INLINE int GeomNode::
36 get_num_geoms() const {
37  CDReader cdata(_cycler);
38  return cdata->get_geoms()->size();
39 }
40 
41 
42 /**
43  * Returns the nth geom of the node. This object should not be modified,
44  * since the same object might be shared between multiple different GeomNodes,
45  * but see modify_geom().
46  */
47 INLINE CPT(Geom) GeomNode::
48 get_geom(int n) const {
49  CDReader cdata(_cycler);
50  CPT(GeomList) geoms = cdata->get_geoms();
51  nassertr(n >= 0 && n < (int)geoms->size(), nullptr);
52  return (*geoms)[n]._geom.get_read_pointer();
53 }
54 
55 /**
56  * Returns the nth geom of the node, suitable for modifying it. If the nth
57  * Geom has multiple reference counts to it, reassigns it to an identical copy
58  * first, and returns the new copy--this provides a "copy on write" that
59  * ensures that the Geom that is returned is unique to this GeomNode and is
60  * not shared with any other GeomNodes.
61  *
62  * Note that if this method is called in a downstream stage (for instance,
63  * during cull or draw), then it will propagate the new list of Geoms upstream
64  * all the way to pipeline stage 0, which may step on changes that were made
65  * independently in pipeline stage 0. Use with caution.
66  */
67 INLINE PT(Geom) GeomNode::
68 modify_geom(int n) {
69  CDWriter cdata(_cycler, true);
70  PT(GeomList) geoms = cdata->modify_geoms();
71  nassertr(n >= 0 && n < (int)geoms->size(), nullptr);
72  mark_internal_bounds_stale();
73  return (*geoms)[n]._geom.get_write_pointer();
74 }
75 
76 /**
77  * Returns the RenderState associated with the nth geom of the node. This is
78  * just the RenderState directly associated with the Geom; the actual state in
79  * which the Geom is rendered will also be affected by RenderStates that
80  * appear on the scene graph in nodes above this GeomNode.
81  */
82 INLINE const RenderState *GeomNode::
83 get_geom_state(int n) const {
84  CDReader cdata(_cycler);
85  CPT(GeomList) geoms = cdata->get_geoms();
86  nassertr(n >= 0 && n < (int)geoms->size(), nullptr);
87  return (*geoms)[n]._state;
88 }
89 
90 /**
91  * Changes the RenderState associated with the nth geom of the node. This is
92  * just the RenderState directly associated with the Geom; the actual state in
93  * which the Geom is rendered will also be affected by RenderStates that
94  * appear on the scene graph in nodes above this GeomNode.
95  *
96  * Note that if this method is called in a downstream stage (for instance,
97  * during cull or draw), then it will propagate the new list of Geoms upstream
98  * all the way to pipeline stage 0, which may step on changes that were made
99  * independently in pipeline stage 0. Use with caution.
100  */
101 INLINE void GeomNode::
102 set_geom_state(int n, const RenderState *state) {
103  CDWriter cdata(_cycler, true);
104  PT(GeomList) geoms = cdata->modify_geoms();
105  nassertv(n >= 0 && n < (int)geoms->size());
106  (*geoms)[n]._state = state;
107 }
108 
109 /**
110  * Removes the nth geom from the node.
111  */
112 INLINE void GeomNode::
113 remove_geom(int n) {
114  CDWriter cdata(_cycler);
115  PT(GeomList) geoms = cdata->modify_geoms();
116  nassertv(n >= 0 && n < (int)geoms->size());
117 
118  geoms->erase(geoms->begin() + n);
119  mark_internal_bounds_stale();
120 }
121 
122 /**
123  * Removes all the geoms from the node at once.
124  */
125 INLINE void GeomNode::
127  CDWriter cdata(_cycler);
128  cdata->set_geoms(new GeomList);
129  mark_internal_bounds_stale();
130 }
131 
132 /**
133  * Returns the default into_collide_mask assigned to new GeomNodes.
134  */
135 INLINE CollideMask GeomNode::
136 get_default_collide_mask() {
137  return default_geom_node_collide_mask;
138 }
139 
140 /**
141  * Increments the count for the indicated InternalName.
142  */
143 INLINE void GeomNode::
144 count_name(GeomNode::NameCount &name_count, const InternalName *name) {
145  std::pair<NameCount::iterator, bool> result =
146  name_count.insert(NameCount::value_type(name, 1));
147  if (!result.second) {
148  (*result.first).second++;
149  }
150 }
151 
152 /**
153  * Returns the count for the indicated InternalName.
154  */
155 INLINE int GeomNode::
156 get_name_count(const GeomNode::NameCount &name_count, const InternalName *name) {
157  NameCount::const_iterator ni;
158  ni = name_count.find(name);
159  if (ni != name_count.end()) {
160  return (*ni).second;
161  }
162  return 0;
163 }
164 
165 /**
166  * Returns an object that can be used to walk through the list of geoms of the
167  * node. When you intend to visit multiple geoms, using this is slightly
168  * faster than calling get_geom() directly on the GeomNode, since this object
169  * avoids reopening the PipelineCycler each time.
170  *
171  * This object also protects you from self-modifying loops (e.g. adding or
172  * removing geoms during traversal), since a virtual copy of the geoms is made
173  * ahead of time. The virtual copy is fast--it is a form of copy-on-write, so
174  * the list is not actually copied unless it is modified during the traversal.
175  */
177 get_geoms(Thread *current_thread) const {
178  CDReader cdata(_cycler, current_thread);
179  return Geoms(cdata);
180 }
181 
182 /**
183  *
184  */
185 INLINE GeomNode::GeomEntry::
186 GeomEntry(Geom *geom, const RenderState *state) :
187  _geom(geom),
188  _state(state)
189 {
190 }
191 
192 /**
193  *
194  */
195 INLINE GeomNode::CData::
196 CData() :
197  _geoms(new GeomNode::GeomList)
198 {
199 }
200 
201 /**
202  * Returns a read-only pointer to the _geoms list.
203  */
204 INLINE CPT(GeomNode::GeomList) GeomNode::CData::
205 get_geoms() const {
206  return _geoms.get_read_pointer();
207 }
208 
209 /**
210  * Returns a modifiable, unique pointer to the _geoms list.
211  */
212 INLINE PT(GeomNode::GeomList) GeomNode::CData::
213 modify_geoms() {
214  return _geoms.get_write_pointer();
215 }
216 
217 /**
218  * Replaces the _geoms list with a new list.
219  */
220 INLINE void GeomNode::CData::
221 set_geoms(GeomNode::GeomList *geoms) {
222  _geoms = geoms;
223 }
224 
225 /**
226  *
227  */
228 INLINE GeomNode::Geoms::
229 Geoms() {
230 }
231 
232 /**
233  *
234  */
235 INLINE GeomNode::Geoms::
236 Geoms(const GeomNode::CData *cdata) :
237  _geoms(cdata->get_geoms())
238 {
239 }
240 
241 /**
242  *
243  */
244 INLINE GeomNode::Geoms::
245 Geoms(const GeomNode::Geoms &copy) :
246  _geoms(copy._geoms)
247 {
248 }
249 
250 /**
251  *
252  */
253 INLINE void GeomNode::Geoms::
254 operator = (const GeomNode::Geoms &copy) {
255  _geoms = copy._geoms;
256 }
257 
258 /**
259  *
260  */
261 INLINE GeomNode::Geoms::
262 Geoms(GeomNode::Geoms &&from) noexcept :
263  _geoms(std::move(from._geoms))
264 {
265 }
266 
267 /**
268  *
269  */
270 INLINE void GeomNode::Geoms::
271 operator = (GeomNode::Geoms &&from) noexcept {
272  _geoms = std::move(from._geoms);
273 }
274 
275 /**
276  * Returns the number of geoms of the node.
277  */
278 INLINE int GeomNode::Geoms::
279 get_num_geoms() const {
280  nassertr(!_geoms.is_null(), 0);
281  return _geoms->size();
282 }
283 
284 /**
285  * Returns the nth geom of the node. This object should not be modified,
286  * since the same object might be shared between multiple different GeomNodes.
287  */
288 INLINE CPT(Geom) GeomNode::Geoms::
289 get_geom(int n) const {
290  nassertr(!_geoms.is_null(), nullptr);
291  nassertr(n >= 0 && n < (int)_geoms->size(), nullptr);
292  return (*_geoms)[n]._geom.get_read_pointer();
293 }
294 
295 /**
296  * Returns the RenderState associated with the nth geom of the node. This is
297  * just the RenderState directly associated with the Geom; the actual state in
298  * which the Geom is rendered will also be affected by RenderStates that
299  * appear on the scene graph in nodes above this GeomNode.
300  */
301 INLINE const RenderState *GeomNode::Geoms::
302 get_geom_state(int n) const {
303  nassertr(!_geoms.is_null(), nullptr);
304  nassertr(n >= 0 && n < (int)_geoms->size(), nullptr);
305  return (*_geoms)[n]._state;
306 }
get_geom_state
Returns the RenderState associated with the nth geom of the node.
Definition: geomNode.h:75
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
int get_num_geoms() const
Returns the number of geoms of the node.
Definition: geomNode.I:279
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
bool get_preserved() const
Returns the "preserved" flag.
Definition: geomNode.I:28
void remove_geom(int n)
Removes the nth geom from the node.
Definition: geomNode.I:113
A container for geometry primitives.
Definition: geom.h:54
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
CPT(Geom) GeomNode
Returns the nth geom of the node.
Definition: geomNode.I:47
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
A thread; that is, a lightweight process.
Definition: thread.h:46
This is similar to RefCountObj, but it implements a CopyOnWriteObject inheritance instead of a Refere...
Geoms get_geoms(Thread *current_thread=Thread::get_current_thread()) const
Returns an object that can be used to walk through the list of geoms of the node.
Definition: geomNode.I:177
void set_geom_state(int n, const RenderState *state)
Changes the RenderState associated with the nth geom of the node.
Definition: geomNode.I:102
A general bitmask class.
Definition: bitMask.h:32
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
void set_preserved(bool value)
Sets the "preserved" flag.
Definition: geomNode.I:19
void remove_all_geoms()
Removes all the geoms from the node at once.
Definition: geomNode.I:126