OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Node.h
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.
29 namespace osgTerrain {
30 class Terrain;
31 }
32 
33 namespace osg {
34 
35 // forcing declare classes to enable declaration of as*() methods.
36 class NodeVisitor;
37 class Drawable;
38 class Geometry;
39 class Group;
40 class Transform;
41 class Node;
42 class Switch;
43 class Geode;
44 class Camera;
45 
47 typedef std::vector< Node* > NodePath;
48 
50 typedef std::vector< NodePath > NodePathList;
51 
53 typedef 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 
71 class OSG_EXPORT Node : public Object
72 {
73  public:
74 
78  Node();
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 
138  virtual Camera* asCamera() { return 0; }
141  virtual const Camera* asCamera() const { return 0; }
142 
143 
146  virtual Switch* asSwitch() { return 0; }
147 
150  virtual const Switch* asSwitch() const { return 0; }
151 
154  virtual Geode* asGeode() { return 0; }
155 
158  virtual const Geode* asGeode() const { return 0; }
159 
162  virtual osgTerrain::Terrain* asTerrain() { return 0; }
163 
166  virtual const osgTerrain::Terrain* asTerrain() const { return 0; }
167 
168 
170  virtual void accept(NodeVisitor& nv);
172  virtual void ascend(NodeVisitor& nv);
174  virtual void traverse(NodeVisitor& /*nv*/) {}
175 
177  typedef std::vector<Group*> ParentList;
178 
180  inline const ParentList& getParents() const { return _parents; }
181 
184  inline ParentList getParents() { return _parents; }
185 
186  inline Group* getParent(unsigned int i) { return _parents[i]; }
187 
193  inline const Group* getParent(unsigned int i) const { return _parents[i]; }
194 
199  inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
200 
201 
202 
205  NodePathList getParentalNodePaths(osg::Node* haltTraversalAtNode=0) const;
206 
209  MatrixList getWorldMatrices(const osg::Node* haltTraversalAtNode=0) const;
210 
211 
213  void setUpdateCallback(Callback* nc);
214 
216  inline Callback* getUpdateCallback() { return _updateCallback.get(); }
217 
219  inline const Callback* getUpdateCallback() const { return _updateCallback.get(); }
220 
222  inline void addUpdateCallback(Callback* nc) {
223  if (nc != NULL) {
224  if (_updateCallback.valid()) _updateCallback->addNestedCallback(nc);
225  else setUpdateCallback(nc);
226  }
227  }
228 
230  inline void removeUpdateCallback(Callback* nc) {
231  if (nc != NULL && _updateCallback.valid()) {
232  if (_updateCallback == nc)
233  {
234  ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
235  nc->setNestedCallback(0);
236  setUpdateCallback(new_nested_callback.get());
237  }
238  else _updateCallback->removeNestedCallback(nc);
239  }
240  }
241 
244  inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; }
245 
246 
248  void setEventCallback(Callback* nc);
249 
251  inline Callback* getEventCallback() { return _eventCallback.get(); }
252 
254  inline const Callback* getEventCallback() const { return _eventCallback.get(); }
255 
257  inline void addEventCallback(Callback* nc) {
258  if (nc != NULL) {
259  if (_eventCallback.valid()) _eventCallback->addNestedCallback(nc);
260  else setEventCallback(nc);
261  }
262  }
263 
265  inline void removeEventCallback(Callback* nc) {
266  if (nc != NULL && _eventCallback.valid()) {
267  if (_eventCallback == nc)
268  {
269  ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
270  nc->setNestedCallback(0);
271  setEventCallback(new_nested_callback.get()); // replace the callback by the nested one
272  }
273  else _eventCallback->removeNestedCallback(nc);
274  }
275  }
276 
279  inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; }
280 
281 
283  void setCullCallback(Callback* nc) { _cullCallback = nc; }
284 
286  inline Callback* getCullCallback() { return _cullCallback.get(); }
287 
289  inline const Callback* getCullCallback() const { return _cullCallback.get(); }
290 
292  inline void addCullCallback(Callback* nc) {
293  if (nc != NULL) {
294  if (_cullCallback.valid()) _cullCallback->addNestedCallback(nc);
295  else setCullCallback(nc);
296  }
297  }
298 
300  inline void removeCullCallback(Callback* nc) {
301  if (nc != NULL && _cullCallback.valid()) {
302  if (_cullCallback == nc)
303  {
304  ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
305  nc->setNestedCallback(0);
306  setCullCallback(new_nested_callback.get()); // replace the callback by the nested one
307  }
308  else _cullCallback->removeNestedCallback(nc);
309  }
310  }
311 
315  void setCullingActive(bool active);
316 
319  inline bool getCullingActive() const { return _cullingActive; }
320 
322  inline unsigned int getNumChildrenWithCullingDisabled() const { return _numChildrenWithCullingDisabled; }
323 
326  inline bool isCullingActive() const { return _numChildrenWithCullingDisabled==0 && _cullingActive && getBound().valid(); }
327 
329  inline unsigned int getNumChildrenWithOccluderNodes() const { return _numChildrenWithOccluderNodes; }
330 
331 
333  bool containsOccluderNodes() const;
334 
335 
352  typedef unsigned int NodeMask;
354  inline void setNodeMask(NodeMask nm) { _nodeMask = nm; }
356  inline NodeMask getNodeMask() const { return _nodeMask; }
357 
358 
359 
361  void setStateSet(osg::StateSet* stateset);
362 
366  osg::StateSet* getOrCreateStateSet();
367 
369  inline osg::StateSet* getStateSet() { return _stateset.get(); }
370 
372  inline const osg::StateSet* getStateSet() const { return _stateset.get(); }
373 
374 
376  typedef std::vector<std::string> DescriptionList;
377 
379  void setDescriptions(const DescriptionList& descriptions);
380 
382  DescriptionList& getDescriptions();
383 
385  const DescriptionList& getDescriptions() const;
386 
387 
389  const std::string& getDescription(unsigned int i) const;
390 
392  std::string& getDescription(unsigned int i);
393 
395  unsigned int getNumDescriptions() const;
396 
398  void addDescription(const std::string& desc);
399 
400 
402  void setInitialBound(const osg::BoundingSphere& bsphere) { _initialBound = bsphere; dirtyBound(); }
403 
405  const BoundingSphere& getInitialBound() const { return _initialBound; }
406 
409  void dirtyBound();
410 
411 
412  inline const BoundingSphere& getBound() const
413  {
414  if(!_boundingSphereComputed)
415  {
416  _boundingSphere = _initialBound;
417  if (_computeBoundCallback.valid())
418  _boundingSphere.expandBy(_computeBoundCallback->computeBound(*this));
419  else
420  _boundingSphere.expandBy(computeBound());
421 
422  _boundingSphereComputed = true;
423  }
424  return _boundingSphere;
425  }
426 
430  virtual BoundingSphere computeBound() const;
431 
434  {
436 
438 
440 
441  virtual BoundingSphere computeBound(const osg::Node&) const { return BoundingSphere(); }
442  };
443 
445  void setComputeBoundingSphereCallback(ComputeBoundingSphereCallback* callback) { _computeBoundCallback = callback; }
446 
448  ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() { return _computeBoundCallback.get(); }
449 
451  const ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() const { return _computeBoundCallback.get(); }
452 
454  virtual void setThreadSafeRefUnref(bool threadSafe);
455 
457  virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);
458 
462  virtual void releaseGLObjects(osg::State* = 0) const;
463 
464 
465  protected:
466 
474  virtual ~Node();
475 
476 
477 
482 
483  void addParent(osg::Group* parent);
484  void removeParent(osg::Group* parent);
485 
486  ParentList _parents;
487  friend class osg::Group;
488  friend class osg::Drawable;
489  friend class osg::StateSet;
490 
493  void setNumChildrenRequiringUpdateTraversal(unsigned int num);
494 
497  void setNumChildrenRequiringEventTraversal(unsigned int num);
498 
500 
503  void setNumChildrenWithCullingDisabled(unsigned int num);
504 
506  void setNumChildrenWithOccluderNodes(unsigned int num);
507 
508  NodeMask _nodeMask;
509 
511 
512 };
513 
514 }
515 
516 #endif
virtual bool isSameKindAs(const Object *obj) const
Definition: Node.h:90
virtual Object * clone(const CopyOp &copyop) const
Definition: Node.h:87
const ParentList & getParents() const
Definition: Node.h:180
#define OSG_EXPORT
Definition: Export.h:43
ComputeBoundingSphereCallback * getComputeBoundingSphereCallback()
Definition: Node.h:448
virtual const Drawable * asDrawable() const
Definition: Node.h:111
unsigned int getNumChildrenWithCullingDisabled() const
Definition: Node.h:322
virtual Object * cloneType() const
Definition: Node.h:84
#define NULL
Definition: Export.h:59
const Group * getParent(unsigned int i) const
Definition: Node.h:193
Callback * getUpdateCallback()
Definition: Node.h:216
virtual const Camera * asCamera() const
Definition: Node.h:141
unsigned int getNumChildrenWithOccluderNodes() const
Definition: Node.h:329
virtual Node * asNode()
Definition: Node.h:100
void addUpdateCallback(Callback *nc)
Definition: Node.h:222
Callback * getEventCallback()
Definition: Node.h:251
ComputeBoundingSphereCallback(const ComputeBoundingSphereCallback &, const CopyOp &)
Definition: Node.h:437
void removeEventCallback(Callback *nc)
Definition: Node.h:265
NodeMask getNodeMask() const
Definition: Node.h:356
virtual const osgTerrain::Terrain * asTerrain() const
Definition: Node.h:166
NodeMask _nodeMask
Definition: Node.h:508
std::vector< Group * > ParentList
Definition: Node.h:177
BoundingSphere _initialBound
Definition: Node.h:478
virtual osgTerrain::Terrain * asTerrain()
Definition: Node.h:162
ref_ptr< StateSet > _stateset
Definition: Node.h:510
const BoundingSphere & getBound() const
Definition: Node.h:412
ref_ptr< Callback > _eventCallback
Definition: Node.h:495
bool _cullingActive
Definition: Node.h:501
unsigned int _numChildrenRequiringEventTraversal
Definition: Node.h:496
Callback * getCullCallback()
Definition: Node.h:286
void setNumChildrenRequiringEventTraversal(unsigned int num)
void setComputeBoundingSphereCallback(ComputeBoundingSphereCallback *callback)
Definition: Node.h:445
bool _boundingSphereComputed
Definition: Node.h:481
const Callback * getCullCallback() const
Definition: Node.h:289
void addCullCallback(Callback *nc)
Definition: Node.h:292
virtual const Node * asNode() const
Definition: Node.h:104
unsigned int getNumParents() const
Definition: Node.h:199
virtual BoundingSphere computeBound(const osg::Node &) const
Definition: Node.h:441
unsigned int getNumChildrenRequiringUpdateTraversal() const
Definition: Node.h:244
virtual Geode * asGeode()
Definition: Node.h:154
#define META_Object(library, name)
Definition: Object.h:42
std::vector< Matrix > MatrixList
Definition: Node.h:53
ref_ptr< ComputeBoundingSphereCallback > _computeBoundCallback
Definition: Node.h:479
bool getCullingActive() const
Definition: Node.h:319
const BoundingSphere & getInitialBound() const
Definition: Node.h:405
ref_ptr< Callback > _cullCallback
Definition: Node.h:499
unsigned int getNumChildrenRequiringEventTraversal() const
Definition: Node.h:279
const Callback * getEventCallback() const
Definition: Node.h:254
unsigned int _numChildrenRequiringUpdateTraversal
Definition: Node.h:492
const Callback * getUpdateCallback() const
Definition: Node.h:219
virtual const char * className() const
Definition: Node.h:96
virtual const Group * asGroup() const
Definition: Node.h:125
void setNestedCallback(osg::Callback *cb)
Definition: Callback.h:50
void removeCullCallback(Callback *nc)
Definition: Node.h:300
std::vector< std::string > DescriptionList
Definition: Node.h:376
virtual const char * libraryName() const
Definition: Node.h:93
BoundingSphere _boundingSphere
Definition: Node.h:480
virtual Transform * asTransform()
Definition: Node.h:129
T * get() const
Definition: ref_ptr.h:92
unsigned int _numChildrenWithOccluderNodes
Definition: Node.h:505
osg::Callback * getNestedCallback()
Definition: Callback.h:51
virtual Switch * asSwitch()
Definition: Node.h:146
std::vector< NodePath > NodePathList
Definition: Node.h:50
ref_ptr< Callback > _updateCallback
Definition: Node.h:491
void setNodeMask(NodeMask nm)
Definition: Node.h:354
virtual const Geode * asGeode() const
Definition: Node.h:158
virtual Drawable * asDrawable()
Definition: Node.h:108
const ComputeBoundingSphereCallback * getComputeBoundingSphereCallback() const
Definition: Node.h:451
unsigned int _numChildrenWithCullingDisabled
Definition: Node.h:502
void setNumChildrenRequiringUpdateTraversal(unsigned int num)
virtual Camera * asCamera()
Definition: Node.h:138
virtual void traverse(NodeVisitor &)
Definition: Node.h:174
virtual const Geometry * asGeometry() const
Definition: Node.h:118
Definition: Node.h:71
Definition: AlphaFunc.h:19
bool isCullingActive() const
Definition: Node.h:326
BoundingSphered BoundingSphere
virtual Geometry * asGeometry()
Definition: Node.h:115
Group * getParent(unsigned int i)
Definition: Node.h:186
void setCullCallback(Callback *nc)
Definition: Node.h:283
void setInitialBound(const osg::BoundingSphere &bsphere)
Definition: Node.h:402
virtual const Switch * asSwitch() const
Definition: Node.h:150
unsigned int NodeMask
Definition: Node.h:352
const osg::StateSet * getStateSet() const
Definition: Node.h:372
virtual Group * asGroup()
Definition: Node.h:122
void expandBy(const vector_type &v)
void addEventCallback(Callback *nc)
Definition: Node.h:257
osg::StateSet * getStateSet()
Definition: Node.h:369
std::vector< Node * > NodePath
Definition: Node.h:44
ParentList _parents
Definition: Node.h:486
ParentList getParents()
Definition: Node.h:184
virtual const Transform * asTransform() const
Definition: Node.h:133
void removeUpdateCallback(Callback *nc)
Definition: Node.h:230