osg.DrawArrayLengths是OpenSceneGraph的一个渲染指令,用于绘制一组顶点数组。该命令与osg.DrawArrays的最大区别在于它可以指定每个绘制命令的顶点数量,适用于绘制非常规形状的模型。
osg::ref_ptr<osg::DrawArrayLengths> drawArrayLengths = new osg::DrawArrayLengths(osg::PrimitiveSet::QUAD_STRIP,0);
osg::PrimitiveSet::Mode mode
:指定绘制模式,包括osg::PrimitiveSet::POINTS
(点), osg::PrimitiveSet::LINES
(线), osg::PrimitiveSet::LINE_LOOP
(线段), osg::PrimitiveSet::TRIANGLES
(三角形), osg::PrimitiveSet::TRIANGLE_STRIP
(三角形带), osg::PrimitiveSet::TRIANGLE_FAN
(三角形扇形), osg::PrimitiveSet::QUADS
(四边形), osg::PrimitiveSet::QUAD_STRIP
(四边形带),默认为osg::PrimitiveSet::TRIANGLES
。unsigned int first
:指定从第几个顶点开始绘制,默认为0。const osg::DrawArrayLengths& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY
:复制构造函数。GLubyte colorType, GLfloat normalType, GLenum indexType = GL_UNSIGNED_INT, GLenum vertexArrayType = GL_FLOAT
:扩展参数,可以指定颜色、法向量、顶点索引和顶点数组的类型。osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-0.5f, 0.0f, -0.5f));
vertices->push_back(osg::Vec3(0.5f, 0.0f, -0.5f));
vertices->push_back(osg::Vec3(0.5f, 0.0f, 0.5f));
vertices->push_back(osg::Vec3(-0.5f, 0.0f, 0.5f));
vertices->push_back(osg::Vec3(-0.5f, 0.0f, -0.5f));
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
osg::ref_ptr<osg::DrawArrayLengths> drawArrayLengths = new osg::DrawArrayLengths(osg::PrimitiveSet::QUAD_STRIP, 0);
drawArrayLengths->setFirst(0);
drawArrayLengths->addLength(2); //绘制两个点,绘制线段
drawArrayLengths->addLength(3); //绘制三个点,绘制三角形
drawArrayLengths->addLength(4); //绘制四个点,绘制四边形
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(vertices.get());
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
geom->addPrimitiveSet(drawArrayLengths.get());