osg.MultiDrawArrays
类继承自osg::DrawArrays
,可以用于一次绘制多个几何图形。
osg::MultiDrawArrays(unsigned int mode=osg::PrimitiveSet::TRIANGLES, osg::DrawArrays::IntArray * first=nullptr, osg::DrawArrays::IntArray * count=nullptr, unsigned int numInstances=0, osg::DrawArrays::Mode modeModifier=osg::PrimitiveSet::UNDEFINED_MODE);
mode
:绘制模式,默认值为osg::PrimitiveSet::TRIANGLES
。first
:绘制命令组的第一个顶点在顶点数组中的索引列表,使用osg::DrawArrays::IntArray
封装,需要手动释放内存。count
:每个绘制命令组绘制顶点的数量列表,使用osg::DrawArrays::IntArray
封装,需要手动释放内存。numInstances
:指定每个命令组所绘制图形的实例数量,默认为0。modeModifier
:绘制模式修饰符,默认值为osg::PrimitiveSet::UNDEFINED_MODE
。unsigned int getNumIndices() const;
void reserveElements(unsigned int numIndices);
void add(DrawArrays * da);
void addArrays(DrawArrays::IntArray * first, DrawArrays::IntArray * count);
void addArrays(unsigned int first, unsigned int count);
void trim();
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/MultiDrawArrays>
int main(int argc, char ** argv) {
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geom1 = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices1 = new osg::Vec3Array;
vertices1->push_back(osg::Vec3(-1,0,-1));
vertices1->push_back(osg::Vec3(-1,0,1));
vertices1->push_back(osg::Vec3(1,0,1));
vertices1->push_back(osg::Vec3(1,0,-1));
geom1->setVertexArray(vertices1);
geom1->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES,0,4));
geode->addDrawable(geom1);
osg::ref_ptr<osg::Geometry> geom2 = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices2 = new osg::Vec3Array;
vertices2->push_back(osg::Vec3(-1,0,-2));
vertices2->push_back(osg::Vec3(-1,0,0));
vertices2->push_back(osg::Vec3(1,0,0));
vertices2->push_back(osg::Vec3(1,0,-2));
geom2->setVertexArray(vertices2);
geom2->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES,0,4));
geode->addDrawable(geom2);
// 绘制两个矩形
osg::ref_ptr<osg::MultiDrawArrays> rectangles = new osg::MultiDrawArrays();
rectangles->addArrays(0, 4);
rectangles->addArrays(4, 4);
rectangles->setCount(8);
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setUseVertexBufferObjects(true);
geom->setVertexArray(vertices1);
geom->setPrimitiveSet(rectangles);
geode->addDrawable(geom);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setSceneData(geode);
return viewer->run();
}
运行效果: