OpenSceneGraph的 osg.DrawArraysIndirect
类表示一种利用间接渲染的方法进行绘制的方式。这种间接渲染方式可以增强绘制的效率、降低CPU和GPU的负载。
osg::DrawArraysIndirect(GLenum mode, GLintptr indirect);
其中:
mode
: OpenGL绘制模式,支持的值包括GL_TRIANGLES
、GL_TRIANGLE_STRIP
、GL_TRIANGLE_FAN
、GL_QUADS
、GL_QUAD_STRIP
、GL_POLYGON
等
indirect
: 指向用于间接绘制的存储缓冲区的偏移量
DrawArraysIndirect
使用 DrawArraysIndirectCommand
结构体来指定绘制信息,该结构体包含以下成员:
typedef struct {
GLuint count;
GLuint instanceCount;
GLuint first;
GLuint baseInstance;
} DrawArraysIndirectCommand;
count
: 指定要绘制的元素数量
instanceCount
:指定每个元素的实例数量。该值默认为1
first
: 指定要绘制的第一个元素的起始位置。该值默认为0
baseInstance
: 指定实例渲染的基础实例。该值默认为0
osg::ref_ptr<osg::DrawArraysIndirectCommand> command = new osg::DrawArraysIndirectCommand;
command->count = 36; //绘制36个元素
command->instanceCount = 1; //每个元素1个实例
command->first = 0; //从第一个元素开始绘制
command->baseInstance = 0; //基础实例为0
//创建一个用于间接绘制的存储缓冲区
osg::ref_ptr<osg::VertexBufferObject> vbo = new osg::VertexBufferObject;
vbo->allocate(sizeof(osg::DrawArraysIndirectCommand), command.get(), osg::VertexBufferObject::StaticDraw);
// 创建一个DrawArraysIndirect节点
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setUseVertexBufferObjects(true);
geom->setVertexAttribArray(osg::Drawable::ATTRIBUTE_6, 4); //设置顶点属性
geom->setVertexAttribBinding(osg::Drawable::ATTRIBUTE_6, osg::Geometry::BIND_PER_VERTEX);
geom->setDrawIndirectBuffer(vbo.get());
osg::ref_ptr<osg::DrawArraysIndirect> drawArrays = new osg::DrawArraysIndirect(GL_TRIANGLES, 0);
drawArrays->setCount(command.get()->count);
drawArrays->setBaseInstance(command.get()->baseInstance);
geom->addPrimitiveSet(drawArrays.get());
//创建节点图
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(geom.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.run();
DrawArraysIndirect
需要OpenGL 4.0及以上版本支持