osg::Geometry* createLine2(const std::vector<osg::Vec3d>& allPTs, osg::Vec4 color)
{
int nCount = allPTs.size();
osg::ref_ptr<osg::Geometry> pGeometry = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array;
pGeometry->setVertexArray(verts.get());
for (int i = 0; i < allPTs.size(); i++)
{
verts->push_back(allPTs[i]);
}
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(color);
pGeometry->setColorArray(colors.get());
pGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
const int kLastIndex = allPTs.size() - 1;
osg::ref_ptr<osg::ElementBufferObject> ebo = new osg::ElementBufferObject;
osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt(osg::PrimitiveSet::LINE_STRIP);
for (unsigned int i = 0; i < allPTs.size(); i++)
{
if (allPTs[i].x() == -1 && allPTs[i].y() == -1)
{
indices->push_back(kLastIndex);
}
else
{
indices->push_back(i);
}
}
indices->setElementBufferObject(ebo);
pGeometry->addPrimitiveSet(indices.get());
pGeometry->setUseVertexBufferObjects(true);
osg::StateSet* stateset = pGeometry->getOrCreateStateSet();
stateset->setAttributeAndModes(new osg::LineWidth(2), osg::StateAttribute::ON);
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
stateset->setMode(GL_PRIMITIVE_RESTART, osg::StateAttribute::ON);
stateset->setAttributeAndModes(new osg::PrimitiveRestartIndex(kLastIndex), osg::StateAttribute::ON);
return pGeometry.release();
}
osg::Node* create_lines()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
vector<osg::Vec3d> PTs;
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < 100; i++)
{
PTs.push_back(osg::Vec3d(i * 10, 0, 0));
if (i % 4 < 2)
PTs.back() += osg::Vec3(0, 50, 0);
PTs.back() += osg::Vec3(0, j * 150, 0);
}
PTs.push_back(osg::Vec3(-1,-1, -1));
}
osg::Geometry* n = createLine2(PTs, osg::Vec4(1, 0, 0, 1));
geode->addDrawable(n);
return geode.release();
}