2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_NODE
|
|
|
|
#define OSG_NODE 1
|
|
|
|
|
|
|
|
#include <osg/Object>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/StateSet>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/BoundingSphere>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/MemoryAdapter>
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <osg/NodeCallback>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/ref_ptr>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
class NodeVisitor;
|
|
|
|
class Group;
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
/** META_Node macro define the standard clone, isSameKindAs, className
|
|
|
|
* and accept methods. Use when subclassing from Node to make it
|
|
|
|
* more convinient to define the required pure virtual methods.*/
|
|
|
|
#define META_Node(name) \
|
|
|
|
virtual Object* clone() const { return new name (); } \
|
|
|
|
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
|
|
|
|
virtual const char* className() const { return #name; } \
|
|
|
|
virtual void accept(NodeVisitor& nv) { if (nv.validNodeMask(*this)) nv.apply(*this); } \
|
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Base class for all internal nodes in the scene graph.
|
|
|
|
Provides interface for most common node operations (Composite Pattern).
|
|
|
|
*/
|
|
|
|
class SG_EXPORT Node : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** Construct a node.
|
|
|
|
Initialize the parent list to empty, node name to "" and
|
|
|
|
bounding sphere dirty flag to true.*/
|
|
|
|
Node();
|
|
|
|
|
|
|
|
/** return a shallow copy of a node, with Object* return type.*/
|
|
|
|
virtual Object* clone() const { return new Node(); }
|
|
|
|
|
|
|
|
/** return true if this and obj are of the same kind of object.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Node*>(obj)!=NULL; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** return the name of the node's class type.*/
|
|
|
|
virtual const char* className() const { return "Node"; }
|
|
|
|
|
|
|
|
/** Visitor Pattern : calls the apply method of a NodeVisitor with this node's type.*/
|
|
|
|
virtual void accept(NodeVisitor& nv);
|
|
|
|
/** Traverse upwards : calls parents' accept method with NodeVisitor.*/
|
|
|
|
virtual void ascend(NodeVisitor& nv);
|
|
|
|
/** Traverse downwards : calls children's accept method with NodeVisitor.*/
|
|
|
|
virtual void traverse(NodeVisitor& /*nv*/) {}
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the name of node using C++ style string.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void setName( const std::string& name ) { _name = name; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Set the name of node using a C style string.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void setName( const char* name ) { _name = name; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get the name of node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const std::string& getName() const { return _name; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** A vector of osg::Group pointers which is used to store the parent(s) of node.*/
|
|
|
|
typedef std::vector<Group*> ParentList;
|
|
|
|
|
|
|
|
/** Get the parent list of node. */
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const ParentList& getParents() const { return _parents; }
|
|
|
|
|
|
|
|
/** Get the a copy of parent list of node. A copy is returned to
|
2001-09-22 10:42:08 +08:00
|
|
|
* prevent modification of the parent list.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
// inline ParentList getParents() { return _parents; }
|
|
|
|
|
|
|
|
inline Group* getParent(const int i) { return _parents[i]; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/**
|
2001-09-20 05:08:56 +08:00
|
|
|
* Get a single const parent of node.
|
2001-01-11 00:32:10 +08:00
|
|
|
* @param i index of the parent to get.
|
|
|
|
* @return the parent i.
|
|
|
|
*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const Group* getParent(const int i) const { return _parents[i]; }
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/**
|
|
|
|
* Get the number of parents of node.
|
|
|
|
* @return the number of parents of this node.
|
|
|
|
*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const int getNumParents() const { return _parents.size(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
/** Set app node callback, called during app traversal. */
|
|
|
|
void setAppCallback(NodeCallback* nc);
|
|
|
|
|
|
|
|
/** Get app node callback, called during app traversal. */
|
|
|
|
inline NodeCallback* getAppCallback() { return _appCallback.get(); }
|
|
|
|
|
|
|
|
/** Get const app node callback, called during app traversal. */
|
|
|
|
inline const NodeCallback* getAppCallback() const { return _appCallback.get(); }
|
|
|
|
|
|
|
|
/** Get the number of Children of this node which require App traversal,
|
|
|
|
* since they have an AppCallback attached to them or their children.*/
|
|
|
|
inline const int getNumChildrenRequiringAppTraversal() const { return _numChildrenRequiringAppTraversal; }
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/**
|
|
|
|
* Set user data. See MemoryAdapter documention for details
|
2001-09-22 10:42:08 +08:00
|
|
|
* of how to specify memory management of _userData.
|
2001-01-11 00:32:10 +08:00
|
|
|
*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void setUserData(void* data,MemoryAdapter* ma=0L)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_userData && _memoryAdapter.valid()) _memoryAdapter->unref_data(_userData);
|
2001-01-11 00:32:10 +08:00
|
|
|
_userData = data;
|
|
|
|
_memoryAdapter = ma;
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_userData && _memoryAdapter.valid()) _memoryAdapter->ref_data(_userData);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
/** Get user data.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void* getUserData() { return _userData; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** Get const user data.*/
|
|
|
|
inline const void* getUserData() const { return _userData; }
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get the memory adapter associated with _userData.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline MemoryAdapter* getMemoryAdapter() { return _memoryAdapter.get(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** Get the const memory adapter associated with _userData.*/
|
|
|
|
inline const MemoryAdapter* getMemoryAdapter() const { return _memoryAdapter.get(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
typedef unsigned int NodeMask;
|
|
|
|
/** Set the node mask. Note, node mask is will be replaced by TraversalMask.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline void setNodeMask(const NodeMask nm) { _nodeMask = nm; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get the node Mask. Note, node mask is will be replaced by TraversalMask.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const NodeMask getNodeMask() const { return _nodeMask; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** A vector of std::string's which are used to describe the object.*/
|
|
|
|
typedef std::vector<std::string> DescriptionList;
|
|
|
|
|
|
|
|
/** Get the description list of the const node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const DescriptionList& getDescriptions() const { return _descriptions; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get the description list of the const node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline DescriptionList& getDescriptions() { return _descriptions; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get a single const description of the const node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const std::string& getDescription(const int i) const { return _descriptions[i]; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get a single description of the node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline std::string& getDescription(const int i) { return _descriptions[i]; }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Get the number of descriptions of the node.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const int getNumDescriptions() const { return _descriptions.size(); }
|
2001-01-11 00:32:10 +08:00
|
|
|
/** Add a description string to the node.*/
|
|
|
|
void addDescription(const std::string& desc) { _descriptions.push_back(desc); }
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
/** set the node's StateSet.*/
|
|
|
|
inline void setStateSet(osg::StateSet* dstate) { _dstate = dstate; }
|
|
|
|
|
|
|
|
/** return the node's StateSet.*/
|
|
|
|
inline osg::StateSet* getStateSet() { return _dstate.get(); }
|
|
|
|
|
|
|
|
/** return the node's const StateSet.*/
|
|
|
|
inline const osg::StateSet* getStateSet() const { return _dstate.get(); }
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/** get the bounding sphere of node.
|
|
|
|
Using lazy evaluation computes the bounding sphere if it is 'dirty'.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
inline const BoundingSphere& getBound() const
|
|
|
|
{
|
|
|
|
if(!_bsphere_computed) computeBound();
|
|
|
|
return _bsphere;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
/** Mark this node's bounding sphere dirty.
|
|
|
|
Forcing it to be computed on the next call to getBound().*/
|
|
|
|
void dirtyBound();
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/** Node destructor. Note, is protected so that Nodes cannot
|
|
|
|
be deleted other than by being dereferenced and the reference
|
|
|
|
count being zero (see osg::Referenced), preventing the deletion
|
|
|
|
of nodes which are still in use. This also means that
|
|
|
|
Node's cannot be created on stack i.e Node node will not compile,
|
|
|
|
forcing all nodes to be created on the heap i.e Node* node
|
|
|
|
= new Node().*/
|
|
|
|
virtual ~Node();
|
|
|
|
|
|
|
|
|
|
|
|
/** Compute the bounding sphere around Node's geometry or children.
|
|
|
|
This method is automatically called by getBound() when the bounding
|
|
|
|
sphere has been marked dirty via dirtyBound().*/
|
2001-09-20 05:08:56 +08:00
|
|
|
virtual const bool computeBound() const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
mutable BoundingSphere _bsphere;
|
|
|
|
mutable bool _bsphere_computed;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
std::string _name;
|
|
|
|
|
|
|
|
ParentList _parents;
|
|
|
|
friend Group;
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
ref_ptr<NodeCallback> _appCallback;
|
|
|
|
int _numChildrenRequiringAppTraversal;
|
|
|
|
|
|
|
|
void setNumChildrenRequiringAppTraversal(const int num);
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void* _userData;
|
|
|
|
ref_ptr<MemoryAdapter> _memoryAdapter;
|
|
|
|
|
|
|
|
NodeMask _nodeMask;
|
|
|
|
|
|
|
|
DescriptionList _descriptions;
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
ref_ptr<StateSet> _dstate;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A vector of Nodes pointers which is used to describe the path from a root node to a descendant.*/
|
|
|
|
typedef std::vector<Node*> NodePath;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|