该类是OpenSceneGraph中的一个绘图类,用于绘制一个由顶点索引定义的几何体元素集合。它利用了GPU特性,直接从显卡中读取顶点数据,可以有效地加速顶点渲染操作。
DrawElementsIndirect(GLenum mode, GLenum type, const GLvoid * indirect, GLsizei count, GLsizei stride);
mode
:定义绘制的方式,可以取值为以下任意一个:
GL_POINTS
GL_LINE_STRIP
GL_LINE_LOOP
GL_LINES
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
GL_TRIANGLES
type
:定义索引数据类型,可以取值为以下任意一个:
GL_UNSIGNED_BYTE
GL_UNSIGNED_SHORT
GL_UNSIGNED_INT
indirect
:指向包含绘图命令的结构体数组的指针。count
:结构体数组的元素数量。stride
:指定结构体数组的字节步长。如果为0,则表示按照结构体大小自动计算步长。DrawElementsIndirect
使用一个结构体数组来描述绘图命令。每个结构体包含以下字段:
unsigned int count
:绘制几何图形所需的顶点数量。unsigned int primCount
:绘制几何图形的实例数。unsigned int first
:第一个顶点的索引值。unsigned int baseInstance
:基础实例。以下是一个使用DrawElementsIndirect
绘制三角形的范例:
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-0.5, -0.5, 0.0));
vertices->push_back(osg::Vec3(0.5, -0.5, 0.0));
vertices->push_back(osg::Vec3(0.0, 0.5, 0.0));
osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
indices->push_back(0);
indices->push_back(1);
indices->push_back(2);
// 定义DrawElementsIndirect的绘图命令结构体数组
GLsizei count = 1;
osg::DrawElementsIndirect::Command commands[1];
commands[0].count = indices->getNumIndices();
commands[0].primCount = 1;
commands[0].first = 0;
commands[0].baseInstance = 0;
// 创建DrawElementsIndirect对象并添加绘图命令
osg::ref_ptr<osg::DrawElementsIndirect> elements = new osg::DrawElementsIndirect(osg::PrimitiveSet::TRIANGLES, GL_UNSIGNED_INT, commands, count);
// 创建几何体节点并设置顶点和索引数据
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geometry->setVertexArray(vertices.get());
geometry->addPrimitiveSet(elements.get());
geometry->setUseVertexBufferObjects(true);
// 创建节点图形对象并设置几何体节点
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geometry.get());
// 创建场景图根节点并设置子节点为节点图形对象
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(geode.get());
// 创建渲染视窗并设置场景图根节点为根节点
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setSceneData(root.get());
// 运行OSG查看器
viewer->run();
[1] OpenSceneGraph. OpenSceneGraph Documentation[M]. https://docs.openscenegraph.org/. 2021.