openscenegraph
Node
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14
15#ifndef OSG_NODE
16#define OSG_NODE 1
17
18#include <osg/Object>
19#include <osg/StateSet>
20#include <osg/BoundingSphere>
21#include <osg/BoundingBox>
22#include <osg/Callback>
23
24#include <string>
25#include <vector>
26
27
28// forward declare osgTerrrain::Terrain to enable declaration of asTerrain() method.
29namespace osgTerrain {
30class Terrain;
31}
32
33namespace osg {
34
35// forcing declare classes to enable declaration of as*() methods.
36class NodeVisitor;
37class Drawable;
38class Geometry;
39class Group;
40class Transform;
41class Node;
42class Switch;
43class Geode;
44class Camera;
45
47typedef std::vector< Node* > NodePath;
48
50typedef std::vector< NodePath > NodePathList;
51
53typedef std::vector< Matrix > MatrixList;
54
55
59#define META_Node(library,name) \
60 virtual osg::Object* cloneType() const { return new name (); } \
61 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
62 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
63 virtual const char* className() const { return #name; } \
64 virtual const char* libraryName() const { return #library; } \
65 virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } } \
66
67
71class OSG_EXPORT Node : public Object
72{
73 public:
74
79
81 Node(const Node&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
82
84 virtual Object* cloneType() const { return new Node(); }
85
87 virtual Object* clone(const CopyOp& copyop) const { return new Node(*this,copyop); }
88
90 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Node*>(obj)!=NULL; }
91
93 virtual const char* libraryName() const { return "osg"; }
94
96 virtual const char* className() const { return "Node"; }
97
100 virtual Node* asNode() { return this; }
101
104 virtual const Node* asNode() const { return this; }
105
108 virtual Drawable* asDrawable() { return 0; }
111 virtual const Drawable* asDrawable() const { return 0; }
112
115 virtual Geometry* asGeometry() { return 0; }
118 virtual const Geometry* asGeometry() const { return 0; }
119
122 virtual Group* asGroup() { return 0; }
125 virtual const Group* asGroup() const { return 0; }
126
129 virtual Transform* asTransform() { return 0; }
130
133 virtual const Transform* asTransform() const { return 0; }
134
135
136
139 virtual Switch* asSwitch() { return 0; }
140
143 virtual const Switch* asSwitch() const { return 0; }
144
147 virtual Geode* asGeode() { return 0; }
148
151 virtual const Geode* asGeode() const { return 0; }
152
155 virtual osgTerrain::Terrain* asTerrain() { return 0; }
156
159 virtual const osgTerrain::Terrain* asTerrain() const { return 0; }
160
161
163 virtual void accept(NodeVisitor& nv);
165 virtual void ascend(NodeVisitor& nv);
167 virtual void traverse(NodeVisitor& /*nv*/) {}
168
170 typedef std::vector<Group*> ParentList;
171
173 inline const ParentList& getParents() const { return _parents; }
174
177 inline ParentList getParents() { return _parents; }
178
179 inline Group* getParent(unsigned int i) { return _parents[i]; }
180
186 inline const Group* getParent(unsigned int i) const { return _parents[i]; }
187
192 inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
193
194
195
198 NodePathList getParentalNodePaths(osg::Node* haltTraversalAtNode=0) const;
199
202 MatrixList getWorldMatrices(const osg::Node* haltTraversalAtNode=0) const;
203
204
207
208 template<class T> void setUpdateCallback(const ref_ptr<T>& nc) { setUpdateCallback(nc.get()); }
209
211 inline Callback* getUpdateCallback() { return _updateCallback.get(); }
212
214 inline const Callback* getUpdateCallback() const { return _updateCallback.get(); }
215
217 inline void addUpdateCallback(Callback* nc) {
218 if (nc != NULL) {
219 if (_updateCallback.valid()) _updateCallback->addNestedCallback(nc);
220 else setUpdateCallback(nc);
221 }
222 }
223
224 template<class T> void addUpdateCallback(const ref_ptr<T>& nc) { addUpdateCallback(nc.get()); }
225
228 if (nc != NULL && _updateCallback.valid()) {
229 if (_updateCallback == nc)
230 {
231 ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
232 nc->setNestedCallback(0);
233 setUpdateCallback(new_nested_callback.get());
234 }
235 else _updateCallback->removeNestedCallback(nc);
236 }
237 }
238
239 template<class T> void removeUpdateCallback(const ref_ptr<T>& nc) { removeUpdateCallback(nc.get()); }
240
243 inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; }
244
245
248
249 template<class T> void setEventCallback(const ref_ptr<T>& nc) { setEventCallback(nc.get()); }
250
252 inline Callback* getEventCallback() { return _eventCallback.get(); }
253
255 inline const Callback* getEventCallback() const { return _eventCallback.get(); }
256
258 inline void addEventCallback(Callback* nc) {
259 if (nc != NULL) {
260 if (_eventCallback.valid()) _eventCallback->addNestedCallback(nc);
261 else setEventCallback(nc);
262 }
263 }
264
265 template<class T> void addEventCallback(const ref_ptr<T>& nc) { addEventCallback(nc.get()); }
266
268 inline void removeEventCallback(Callback* nc) {
269 if (nc != NULL && _eventCallback.valid()) {
270 if (_eventCallback == nc)
271 {
272 ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
273 nc->setNestedCallback(0);
274 setEventCallback(new_nested_callback.get()); // replace the callback by the nested one
275 }
276 else _eventCallback->removeNestedCallback(nc);
277 }
278 }
279
280 template<class T> void removeEventCallback(const ref_ptr<T>& nc) { removeEventCallback(nc.get()); }
281
284 inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; }
285
286
288 void setCullCallback(Callback* nc) { _cullCallback = nc; }
289
290 template<class T> void setCullCallback(const ref_ptr<T>& nc) { setCullCallback(nc.get()); }
291
293 inline Callback* getCullCallback() { return _cullCallback.get(); }
294
296 inline const Callback* getCullCallback() const { return _cullCallback.get(); }
297
299 inline void addCullCallback(Callback* nc) {
300 if (nc != NULL) {
301 if (_cullCallback.valid()) _cullCallback->addNestedCallback(nc);
302 else setCullCallback(nc);
303 }
304 }
305
306 template<class T> void addCullCallback(const ref_ptr<T>& nc) { addCullCallback(nc.get()); }
307
309 inline void removeCullCallback(Callback* nc) {
310 if (nc != NULL && _cullCallback.valid()) {
311 if (_cullCallback == nc)
312 {
313 ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
314 nc->setNestedCallback(0);
315 setCullCallback(new_nested_callback.get()); // replace the callback by the nested one
316 }
317 else _cullCallback->removeNestedCallback(nc);
318 }
319 }
320
321 template<class T> void removeCullCallback(const ref_ptr<T>& nc) { removeCullCallback(nc.get()); }
322
326 void setCullingActive(bool active);
327
330 inline bool getCullingActive() const { return _cullingActive; }
331
333 inline unsigned int getNumChildrenWithCullingDisabled() const { return _numChildrenWithCullingDisabled; }
334
337 inline bool isCullingActive() const { return _numChildrenWithCullingDisabled==0 && _cullingActive && getBound().valid(); }
338
340 inline unsigned int getNumChildrenWithOccluderNodes() const { return _numChildrenWithOccluderNodes; }
341
342
345
346
363 typedef unsigned int NodeMask;
365 inline void setNodeMask(NodeMask nm) { _nodeMask = nm; }
367 inline NodeMask getNodeMask() const { return _nodeMask; }
368
369
370
372 void setStateSet(osg::StateSet* stateset);
373
374 template<class T> void setStateSet(const osg::ref_ptr<T>& stateset) { setStateSet(stateset.get()); }
375
380
382 inline osg::StateSet* getStateSet() { return _stateset.get(); }
383
385 inline const osg::StateSet* getStateSet() const { return _stateset.get(); }
386
387
389 typedef std::vector<std::string> DescriptionList;
390
392 void setDescriptions(const DescriptionList& descriptions);
393
396
399
400
402 const std::string& getDescription(unsigned int i) const;
403
405 std::string& getDescription(unsigned int i);
406
408 unsigned int getNumDescriptions() const;
409
411 void addDescription(const std::string& desc);
412
413
415 void setInitialBound(const osg::BoundingSphere& bsphere) { _initialBound = bsphere; dirtyBound(); }
416
418 const BoundingSphere& getInitialBound() const { return _initialBound; }
419
423
424
425 inline const BoundingSphere& getBound() const
426 {
427 if(!_boundingSphereComputed)
428 {
429 _boundingSphere = _initialBound;
430 if (_computeBoundCallback.valid())
431 _boundingSphere.expandBy(_computeBoundCallback->computeBound(*this));
432 else
433 _boundingSphere.expandBy(computeBound());
434
435 _boundingSphereComputed = true;
436 }
437 return _boundingSphere;
438 }
439
444
457
459 void setComputeBoundingSphereCallback(ComputeBoundingSphereCallback* callback) { _computeBoundCallback = callback; }
460
461 template<class T> void setComputeBoundingSphereCallback(const ref_ptr<T>& callback) { setComputeBoundingSphereCallback(callback.get()); }
462
464 ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() { return _computeBoundCallback.get(); }
465
467 const ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() const { return _computeBoundCallback.get(); }
468
470 virtual void setThreadSafeRefUnref(bool threadSafe);
471
473 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);
474
478 virtual void releaseGLObjects(osg::State* = 0) const;
479
480
481 protected:
482
490 virtual ~Node();
491
492
493
498
499 void addParent(osg::Group* parent);
501
503 friend class osg::Group;
504 friend class osg::Drawable;
505 friend class osg::StateSet;
506
510
514
516
519 void setNumChildrenWithCullingDisabled(unsigned int num);
520
522 void setNumChildrenWithOccluderNodes(unsigned int num);
523
525
527
528};
529
530}
531
532#endif
Definition Terrain:28
Definition BoundingSphere:35
void expandBy(const vector_type &v)
Definition BoundingSphere:149
Definition Callback:34
osg::Callback * getNestedCallback()
Definition Callback:89
void setNestedCallback(osg::Callback *cb)
Definition Callback:88
Definition CopyOp:41
Definition Drawable:89
Definition Geode:29
Definition Geometry:31
Definition Group:29
Definition NodeVisitor:82
Definition Node:72
virtual const osgTerrain::Terrain * asTerrain() const
Definition Node:159
void setEventCallback(const ref_ptr< T > &nc)
Definition Node:249
virtual void releaseGLObjects(osg::State *=0) const
void setCullCallback(Callback *nc)
Definition Node:288
void setUpdateCallback(const ref_ptr< T > &nc)
Definition Node:208
void setUpdateCallback(Callback *nc)
ref_ptr< Callback > _eventCallback
Definition Node:511
const Group * getParent(unsigned int i) const
Definition Node:186
std::string & getDescription(unsigned int i)
virtual const Geode * asGeode() const
Definition Node:151
bool _cullingActive
Definition Node:517
void addDescription(const std::string &desc)
unsigned int _numChildrenWithOccluderNodes
Definition Node:521
void setEventCallback(Callback *nc)
virtual void traverse(NodeVisitor &)
Definition Node:167
ref_ptr< ComputeBoundingSphereCallback > _computeBoundCallback
Definition Node:495
unsigned int getNumParents() const
Definition Node:192
bool containsOccluderNodes() const
DescriptionList & getDescriptions()
ref_ptr< Callback > _updateCallback
Definition Node:507
void setNumChildrenRequiringUpdateTraversal(unsigned int num)
NodeMask _nodeMask
Definition Node:524
void setNumChildrenWithOccluderNodes(unsigned int num)
virtual const char * libraryName() const
Definition Node:93
void removeEventCallback(Callback *nc)
Definition Node:268
bool isCullingActive() const
Definition Node:337
bool getCullingActive() const
Definition Node:330
unsigned int getNumDescriptions() const
virtual Geometry * asGeometry()
Definition Node:115
NodePathList getParentalNodePaths(osg::Node *haltTraversalAtNode=0) const
unsigned int getNumChildrenRequiringEventTraversal() const
Definition Node:284
virtual Group * asGroup()
Definition Node:122
ref_ptr< StateSet > _stateset
Definition Node:526
NodeMask getNodeMask() const
Definition Node:367
void setNumChildrenRequiringEventTraversal(unsigned int num)
std::vector< Group * > ParentList
Definition Node:170
void removeCullCallback(Callback *nc)
Definition Node:309
void addParent(osg::Group *parent)
virtual Geode * asGeode()
Definition Node:147
const osg::StateSet * getStateSet() const
Definition Node:385
osg::StateSet * getStateSet()
Definition Node:382
unsigned int getNumChildrenWithCullingDisabled() const
Definition Node:333
virtual Drawable * asDrawable()
Definition Node:108
virtual void resizeGLObjectBuffers(unsigned int)
virtual const Switch * asSwitch() const
Definition Node:143
void removeUpdateCallback(const ref_ptr< T > &nc)
Definition Node:239
void addEventCallback(const ref_ptr< T > &nc)
Definition Node:265
ref_ptr< Callback > _cullCallback
Definition Node:515
void setStateSet(const osg::ref_ptr< T > &stateset)
Definition Node:374
unsigned int getNumChildrenWithOccluderNodes() const
Definition Node:340
osg::StateSet * getOrCreateStateSet()
void setInitialBound(const osg::BoundingSphere &bsphere)
Definition Node:415
Group * getParent(unsigned int i)
Definition Node:179
virtual ~Node()
void setCullingActive(bool active)
virtual const Group * asGroup() const
Definition Node:125
void setComputeBoundingSphereCallback(ComputeBoundingSphereCallback *callback)
Definition Node:459
virtual osgTerrain::Terrain * asTerrain()
Definition Node:155
std::vector< std::string > DescriptionList
Definition Node:389
void addEventCallback(Callback *nc)
Definition Node:258
Callback * getUpdateCallback()
Definition Node:211
virtual BoundingSphere computeBound() const
void addUpdateCallback(const ref_ptr< T > &nc)
Definition Node:224
virtual bool isSameKindAs(const Object *obj) const
Definition Node:90
const BoundingSphere & getInitialBound() const
Definition Node:418
virtual const char * className() const
Definition Node:96
virtual Transform * asTransform()
Definition Node:129
void setNodeMask(NodeMask nm)
Definition Node:365
Callback * getCullCallback()
Definition Node:293
unsigned int getNumChildrenRequiringUpdateTraversal() const
Definition Node:243
const Callback * getUpdateCallback() const
Definition Node:214
virtual const Transform * asTransform() const
Definition Node:133
unsigned int _numChildrenRequiringUpdateTraversal
Definition Node:508
const Callback * getCullCallback() const
Definition Node:296
void setCullCallback(const ref_ptr< T > &nc)
Definition Node:290
MatrixList getWorldMatrices(const osg::Node *haltTraversalAtNode=0) const
virtual const Node * asNode() const
Definition Node:104
virtual const Drawable * asDrawable() const
Definition Node:111
void addCullCallback(Callback *nc)
Definition Node:299
void addCullCallback(const ref_ptr< T > &nc)
Definition Node:306
BoundingSphere _initialBound
Definition Node:494
void removeUpdateCallback(Callback *nc)
Definition Node:227
void setDescriptions(const DescriptionList &descriptions)
virtual void setThreadSafeRefUnref(bool threadSafe)
ParentList getParents()
Definition Node:177
void removeEventCallback(const ref_ptr< T > &nc)
Definition Node:280
const std::string & getDescription(unsigned int i) const
void setNumChildrenWithCullingDisabled(unsigned int num)
virtual Object * cloneType() const
Definition Node:84
void setComputeBoundingSphereCallback(const ref_ptr< T > &callback)
Definition Node:461
const ParentList & getParents() const
Definition Node:173
unsigned int NodeMask
Definition Node:363
const DescriptionList & getDescriptions() const
virtual Node * asNode()
Definition Node:100
virtual Switch * asSwitch()
Definition Node:139
void removeCullCallback(const ref_ptr< T > &nc)
Definition Node:321
void setStateSet(osg::StateSet *stateset)
virtual Object * clone(const CopyOp &copyop) const
Definition Node:87
virtual void accept(NodeVisitor &nv)
void addUpdateCallback(Callback *nc)
Definition Node:217
virtual const Geometry * asGeometry() const
Definition Node:118
void removeParent(osg::Group *parent)
void dirtyBound()
virtual void ascend(NodeVisitor &nv)
Callback * getEventCallback()
Definition Node:252
bool _boundingSphereComputed
Definition Node:497
Node(const Node &, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
ParentList _parents
Definition Node:502
const ComputeBoundingSphereCallback * getComputeBoundingSphereCallback() const
Definition Node:467
const BoundingSphere & getBound() const
Definition Node:425
BoundingSphere _boundingSphere
Definition Node:496
ComputeBoundingSphereCallback * getComputeBoundingSphereCallback()
Definition Node:464
const Callback * getEventCallback() const
Definition Node:255
unsigned int _numChildrenRequiringEventTraversal
Definition Node:512
unsigned int _numChildrenWithCullingDisabled
Definition Node:518
Definition Object:61
Definition StateSet:46
Definition State:80
Definition Switch:27
Definition Transform:75
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
Definition Node:29
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
std::vector< Matrix > MatrixList
Definition Node:53
std::vector< NodePath > NodePathList
Definition Node:50
std::vector< Node * > NodePath
Definition Node:47
#define NULL
Definition osg/Export:55
#define OSG_EXPORT
Definition osg/Export:39
ComputeBoundingSphereCallback()
Definition Node:448
META_Object(osg, ComputeBoundingSphereCallback)
ComputeBoundingSphereCallback(const ComputeBoundingSphereCallback &org, const CopyOp &copyop)
Definition Node:450
virtual BoundingSphere computeBound(const osg::Node &) const
Definition Node:455