OSG:幼儿园篇 第一章 节点类
一.简介
二.osg::Node 节点类
#include <osg/Node>
typedef std::vector<Node*> NodePath;
typedef std::vector<NodePath> NodePathList;
typedef std::vector<Matrix> MatrixList;
class OSG_EXPORT Node : public Object{ public:
Node();
Node(const Node&, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
virtual Object* cloneType() const {return new Node();}
virtual Object* clone(const CopyOp& copyop) const {return new Node(*this, copyop);}
virtual bool isSamKindAs(const Object* obj) const {return dynamic_cast<const Node*>(obj) != NULL;}
virtual const char* libraryName() const {return "osg";}
virtual const char* className() const {return "Node";}
virtual Node* asNode() {return this;}
virtual const Node* asNode() const {return this;}
virtual Drawable* asDrawable() {return 0;}
virtual const Drawable* asDrawable() const {return 0;}
virtual Geometry* asGeometry() {return 0;}
virtual const Geometry* asGeometry() const {return 0;}
virtual Group* asGroup() {return 0;}
virtual const Group* asGroup() const {return 0;}
virtual Transform* asTransform() {return 0;}
virtual const Transform* asTransform() const {return 0;}
virtual Camera* asCamera() {return 0;}
virtual const Camera* asCamera() const {return 0;}
virtual Switch* asSwitch() {return 0;}
virtual cosnt Switch* asSwitch() const {return 0;}
virtual Geode* asGeode() {return 0;}
virtual const Geode* asGeode() const {return 0;}
virtual osgTerrain::Terrain* asTerrain() {return 0;}
virtual void accept(NodeVisitor& nv);
virtual void ascend(NodeVisitor& nv);
virtual void traverse(NodeVisitor& /*nv*/){}
typedef std::vector<Group*> ParentList;
inline const ParentList& getParents() const {return _parents;}
inline ParentList getParents() {return _parents;}
inline Group* getParent(unsigned int i) {return _parents[i];}
inline const Group* getParent(unsigned int i) const {return _parents[i];}
inline unsigned int getNumParents() const {return static_cast<unsigned int> (_parents.size());}
NodePathList getParentalNodePaths(osg::Node* haltTraversalAtNode = 0) const;
Matrixlist getWorldMatrices(const osg::Node* haltTraversalAtNode = 0) const;
void setUpdateCallback(Callback* nc);
inline Callback* getUpdateCallback() {return _updateCallback.get();}
inline const Callback* getUpdateCallback() const {return _updateCallback.get();}
inline void addUpdateCallback(Callback* nc){
if(nc != NULL){
if(_updateCallback.valid())
_updateCallback->addNestedCallback(nc);
ele
setUpdateCallback(nc);
}
}
inline void removeUpdateCallback(Callback* nc){
if(nc != NULL && _updateCallback.valid()){
if(_updateCallback == nc)
{
ref_ptr<osg::Callback> new_nested_callback = nc->getNestedCallback();
nc->setNestedCallback(0);
setUpdateCallback(new_nested_callback.get());
}
else
_updateCallback->remvoeNestedCallback(nc);
}
}
inline unsigned int getNumChildrenRequiringUpdateTraversal() const {return _numChildrenRequiringUpdateTraversal;}
void setEvent
typedef std::vector<Group*> ParentList; // 返回父节点的列表 const ParentList& getParents() const; // 返回指定索引处父节点的指针 Group* getParent(unsigned int I); // 返回父节点的数目 unsigned int getNumParents() const; };
三.osg::Group 组节点类
osg::Group 组节点类可以向下继承有多个 osg::Group 子节点 ,也可以有多个 osg::Geode 子节点
#include <osg/Group>
class OSG_EXPORT Group : public Node{ public:
Group();
Group(const Group&, const CopyOp& copyop = CopyOp::SHALLOW_COPY)
META_Node(osg, Group);
virtual Group* asGroup()
virtual const Group* asGroup() consst
virtual void traverse(NodeVisitor& nv)
// 添加一个子节点 bool addChild(Node* child);
//向指定索引位置插入一个子节点
virtual bool insertChild(unsigned int index, Node* child)
// 删除一个子节点
virtual bool removeChild(Node* child);
//从指定索引位置开始删除指定数目子节点
inline bool removeChild(unsigned int pos, unsigned int numChildrenToRemove = 1)
virtual bool removeChildren(unsigned int pos, unsigned int numChildrenToRemove)
// 用新的子节点替换一个子节点
virtual bool replaceChild(Node* origChild, Node* newChild)
// 返回子节点的数目
virtual unsigned int getNumChildren() const
virtual bool setChild(unsigned int i, Node* node)
//获取一个指定位置的子节点
inline Node* getChild(unsigned int i)
inline const Node* getChild(unsigned int i) const
// 如果指定节点是一个子节点,返回true
inline bool containsNode(const Node* node) const
//获取一个子节点索引位置
inline unsigned int getChildIndex(const Node* node) const
virtual void setThreadSafeRefUnref(bool threadSafe)
virtual void resizeGLObjectBuffers(unsigned int maxSize)
virtual void releaseGLObjects(osg::State* = 0) const
virtual BoundingSphere computeBound() const
protected:
virtual ~Group()
virtual void childRemoved(unsigne int /*pos*/, unsigne int /*numChildrenToRemove*/)
virtual void childInserted(unsigned int /*pos*/)
NodeList _children;
};
三.osg::Geode 叶节点类
osg::Geode 叶节点类用于保存几何信息以便渲染
osg::Geode 叶节点无法向下继承子节点,只能向上继承 osg::Group 父节点
#include <osg/Geode>
class OSG_EXPORT Geode : public Group
{
public:
//构造函数
Geode();
Geode(const Geode&, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
virtual Geode* asGeode()
virtual const Geode* asGeode() const
//从叶节点追加一个可绘制体对象
virtual bool addDrawable(Drawable* drawable);
//从叶节点删除一个可绘制体对象
virtual bool removeDrawable(Drawable* drawable);
virtual bool removeDrawables(unsigned int i, unsigned int numDrawablesToRemove = 1)
//将当前节点中包含的一个可绘制体替换为新的可绘制体
virtual bool replaceDrawable(Drawable* origDraw, Drawable* newDraw);
virtual bool setDrawable(unsigned int i, Drawable* drawable);
//获取可绘制体的数目
inline unsigned int getNumDrawables() const
//获取一个指定位置的可绘制体
inline Drawable* getDrawable(unsigned int i)
inline const Drawable* getDrawable(unsigned int i) const
inline bool constainsDrawable(const Drawable* drawable) const
//获取一个可绘制体在叶节点的索引位置
inline unsigned int getDrawableIndex(const Drawable* drawable) const
void compileDrawables(RenderInfo& renderInfo)
inline const BoundingBox& getBoundingBox() const
virtual BoundingSphere computeBound() const;
protected:
virtual ~Geode();
mutable osg::BoundingBox _bbox;
};
四.osg::Geometry 绘图基元类
在osg中,通常有三种生成几何体的方法:1.用封装的OpenGL绘图基元osg::Geometry,2.是用OSG中预定义的基本几何体,3.是从文件中导入场景模型
osg::Geometry 几何体类可以通过指定顶点 颜色和法线的方式,绘制简单的线段 三角形 多边形并将绘图的结果添加到场景的叶节点Geode中
osg::Group* root = new osg::Group(); //组节点 osg::Geode* geode = new osg::Geode(); //叶节点 osg::Geometry* geometry = new osg::Geometry(); //几何图元 root->addChild(geode); geode->addDrawable(geometry);
五.osg::LOD 细节层次节点类
osg::LOD节点可以实现不同细节层次下物体的渲染。通过指定各个子节点的有效范围,该范围包含了最大和最小值。当子节点与观察者的距离在这个节点的有效范围之内时,LOD将显示这个节点。
基本实现方法:在不影响渲染外观的前提下,根据场景对象与观察者的距离,从多个预置方案中选择一种更为简单的方法来表达要渲染的物体,减轻系统绘制场景的负担,模型越靠近观察者时越精细。
LOD() bool addChild(Node* child, float min, float max) //添加一个子节点,并设置对应的观察距离 //设置指定位置的子节点对应的观察范围,或者获取某个子节点对应的观察最小值/最大值 void setRange(unsigned int childNo, float min, float max) float getMinRange(unsigned int childNo) const float getMaxRange(unsigned int childNo) const const RangeList& getRangeList() const //获取所有子节点观察范围的列表
六.osg::Switch 开关节点类
osg::Switch开关节点类可以渲染或者跳过指定的子节点。根据当前渲染的负荷有选择地渲染子图形以实现渲染性能的均衡,或者在界面和层级进行有选择地切换