类 osg.DefaultIndirectCommandDrawArrays
实现了绘制基元的间接指令功能,支持绘制三角形、线条和点等基元类型。该类的实例使用指针所指向的 osg.DrawArraysIndirectCommand
结构体中的参数来执行绘制操作。
osg::DefaultIndirectCommandDrawArrays::DefaultIndirectCommandDrawArrays();
void osg::DefaultIndirectCommandDrawArrays::drawImplementation(osg::RenderInfo &renderInfo, const osg::PrimitiveSet *primitiveSet) const;
使用给定的渲染信息 renderInfo
和基元集合 primitiveSet
,执行间接指令绘制操作。在绘制过程中,该类的实例使用从 primitiveSet
中获取的 osg::DrawArraysIndirectCommand
结构体参数进行绘制。
osg::DefaultIndirectCommandDrawArrays
类的实例。osg::DrawArraysIndirectCommand
结构体并填充相关参数。osg::GLBufferObject
类创建一个缓冲区并将填充好的 osg::DrawArraysIndirectCommand
结构体数据写入到缓冲区中。osg::StateSet
类设置好相关状态后,调用添加了 osg::DefaultIndirectCommandDrawArrays
类实例的可绘制节点的 draw()
函数来执行绘制操作。osg::ref_ptr<osg::DefaultIndirectCommandDrawArrays> cmd_draw_arrays = new osg::DefaultIndirectCommandDrawArrays();
// ...
// 将 cmd_draw_arrays 添加到需要绘制的节点可绘制子节点集合中
// ...
// 创建缓冲区保存间接指令绘制所需的参数
osg::ref_ptr<osg::GLBufferObject> cmd_buffer = new osg::GLBufferObject();
unsigned int numCommands = 1;
osg::DrawArraysIndirectCommand cmd;
cmd.count = 4;
cmd.primCount = 1;
cmd.first = 0;
cmd.baseInstance = 0;
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, cmd_buffer->getOrCreateGLObject()->getHandle());
glBufferData(GL_DRAW_INDIRECT_BUFFER, numCommands * sizeof(cmd), &cmd, GL_STATIC_DRAW);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
// ...
// 设置渲染状态、视口等
// ...
// 执行绘制
osg::StateSet* stateSet = node->getOrCreateStateSet();
stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
stateSet->setAttributeAndModes(shader, osg::StateAttribute::ON);
stateSet->addUniform(new osg::Uniform("modelViewProjMatrix", viewProj));
stateSet->addUniform(new osg::Uniform("modelMatrix", model));
stateSet->addUniform(new osg::Uniform("modelViewMatrix", view * model));
cmd_draw_arrays->draw(renderInfo, GL_TRIANGLES, cmd_buffer->getOrCreateGLObject(), 0, numCommands, sizeof(osg::DrawArraysIndirectCommand));
DefaultIndirectCommandDrawArrays
类进行绘制操作时,必须首先调用 setNumInstances()
函数设置实例数量,否则会发生异常。osg::DrawArraysIndirectCommand
结构体中的参数与 glDrawArraysIndirect()
函数中的参数意义相同,具体可参考 glDrawArraysIndirect()
的相关文档和示例代码。DefaultIndirectCommandDrawArrays
类,OpenSceneGraph 中还提供了许多其他类型的间接指令绘制类,如 osg::DefaultIndirectCommandDrawElementsUint
、osg::DefaultIndirectCommandDrawElementsUshort
、osg::MultiDrawIndirect
等,用户可以根据自己的需要选择不同的实现方式。