osg::IndirectCommandDrawElements
是OpenSceneGraph中的一个类,用于描述绘制几何图元的命令。你可以用它来告诉GPU绘制几何图元的方式,并且可以使用osg::DrawIndirect
命令调用,实现间接绘制的效果。这个类通常用在GPU缓存法线、顶点和索引的场景渲染器中。
osg::Referenced -> osg::ObjectBase -> osg::Object -> osg::Drawable -> osg::Geometry -> osg::DrawElements -> osg::IndirectCommandDrawElements
示例代码如下,用于渲染一个三角形:
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-1.0f,-1.0f,0.0f));
vertices->push_back(osg::Vec3(1.0f,-1.0f,0.0f));
vertices->push_back(osg::Vec3(0.0f,1.0f,0.0f));
osg::ref_ptr<osg::DrawArrays> primitive = new osg::DrawArrays(GL_TRIANGLES,0,3);
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geometry->setVertexArray(vertices.get());
geometry->addPrimitiveSet(primitive.get());
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
geometry->setStateSet(stateSet.get());
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geometry.get());
osg::ref_ptr<osg::IndirectCommandDrawElements> indirectCommandDrawElements = new osg::IndirectCommandDrawElements(GL_UNSIGNED_INT);
indirectCommandDrawElements->setCount(3);
indirectCommandDrawElements->setFirstIndex(0);
osg::ref_ptr<osg::DrawIndirect> indirect = new osg::DrawIndirect(GL_TRIANGLES, osg::DrawIndirectCommand(indirectCommandDrawElements.get(), 1, 16));
osgViewer::Viewer viewer;
viewer.setSceneData(indirect.get());
viewer.run();
IndirectCommandDrawElements(GLenum mode = GL_TRIANGLES, GLenum indexType = GL_UNSIGNED_INT, osg::Object *commmandBuffer = NULL, unsigned int offset = 0)
:构造函数。参数mode
表示绘制的方式,可以是GL_POINTS
、GL_LINES
、GL_LINE_STRIP
、GL_LINE_LOOP
、GL_TRIANGLES
、GL_TRIANGLE_STRIP
、GL_TRIANGLE_FAN
。参数indexType
表示索引类型,可以是GL_UNSIGNED_BYTE
、GL_UNSIGNED_SHORT
、GL_UNSIGNED_INT
。参数commandBuffer
是OpenGL命令缓存区对象,默认为空。参数offset
是偏移量。void setCount(unsigned int count)
:设置绘制的点数或顶点数目。unsigned int getCount() const
:获取绘制的点数或顶点数目。void setFirstIndex(unsigned int first)
:设置第一个索引的位置,即第一个点或顶点的位置。unsigned int getFirstIndex() const
:获取第一个索引的位置。virtual osg::Object *cloneType() const = 0
:创建该类型对象的一个新实例。virtual osg::Object *clone(const osg::CopyOp ©op) const = 0
:实现深复制,复制所有的状态属性、模板属性和回调函数。virtual bool isSameKindAs(const osg::Object *obj) const = 0
:是否相同种类的对象。virtual const char *className() const = 0
:返回此对象的名称。virtual const char *libraryName() const = 0
:返回库名称,用于配合className()。virtual void accept(osg::NodeVisitor &nv) = 0
:接收访问器操作。virtual void accept(osg::ConstNodeVisitor &nv) const = 0
:接收常量访问器操作。virtual void setUseVertexBufferObjects(bool flag)
:设置是否使用Vertex Buffer Object。virtual bool getUseVertexBufferObjects() const
:获取是否使用Vertex Buffer Object。virtual osg::Object *cloneType() const
:创建该类型对象的一个新实例。virtual osg::Object *clone(const osg::CopyOp ©op) const
:实现深复制。virtual bool isSameKindAs(const osg::Object *obj) const
:是否相同种类的对象。virtual const char *className() const
:返回此对象的名称。virtual const char *libraryName() const
:返回库名称,用于配合className()。virtual void accept(osg::NodeVisitor &nv)
:接收访问器操作。virtual void accept(osg::ConstNodeVisitor &nv) const
:接收常量访问器操作。virtual osg::BoundingBox computeBoundingBox() const
:计算Geometry的边界框。virtual osg::BoundingSphere computeBound() const
:计算Geometry的边界球。void addPrimitiveSet(osg::PrimitiveSet *primitive)
:添加Geometry的几何图元。osg::PrimitiveSet *getPrimitiveSet(unsigned int i)
:获取第i个几何图元。unsigned int getNumPrimitiveSets() const
:获取Geometry的几何图元数目。virtual osg::Object *cloneType() const
:创建该类型对象的一个新实例。virtual osg::Object *clone(const osg::CopyOp ©op) const
:实现深复制。virtual bool isSameKindAs(const osg::Object *obj) const
:是否相同种类的对象。virtual const char *className() const
:返回此对象的名称。virtual const char *libraryName() const
:返回库名称,用于配合className()。virtual void accept(osg::NodeVisitor &nv)
:接收访问器操作。virtual void accept(osg::ConstNodeVisitor &nv) const
:接收常量访问器操作。void setElementBufferObject(osg::BufferObject *buffer)
:设置Element Buffer Object。osg::BufferObject *getElementBufferObject() const
:获取Element Buffer Object。