|
#include <osg\Geometry>
#include <osg\Group>
#include <osgViewer\Viewer>
#include <osg\LineWidth>
#include <iostream>
#include <osgViewer/ViewerEventHandlers>
class DynamicLineCallback: public osg:rawable::UpdateCallback
{
public:
DynamicLineCallback()
{
}
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable)
{
osg::Geometry* geom = dynamic_cast<osg::Geometry*>(drawable);
if (!geom) return;
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray() );
if (vertices)
{
if ( vertices->size() > 5 )
{
vertices->pop_back();
std::cout<< vertices->size()<<std::endl;
}
}
vertices->dirty();
geom->setVertexArray( vertices );
geom->dirtyBound();
}
};
int main()
{
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(10);
for (unsigned int i=0; i<10; i++)
(*vertices)[i].set(float(i), 0.0f,0.0f);
osg::ref_ptr<osg::Geometry> lineGeom = new osg::Geometry;
lineGeom->setDataVariance( osg::Object::DYNAMIC );
lineGeom->setVertexArray( vertices.get() );
lineGeom->addPrimitiveSet( new osg::DrawArrays(osg::DrawArrays:OINTS,0,vertices->size() ) );
lineGeom->setInitialBound( osg::BoundingBox( osg::Vec3(-20.0f,-20.0f,-20.0f), osg::Vec3(20.0f,20.0f,20.0f)) );
lineGeom->setUseDisplayList( false );
lineGeom->setUseVertexBufferObjects( true );
lineGeom->setUpdateCallback( new DynamicLineCallback());
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setAttribute( new osg:ineWidth(4.0f));
geode->addDrawable( lineGeom.get() );
osgViewer::Viewer viewer;
viewer.setSceneData( geode.get() );
viewer.addEventHandler( new osgViewer::WindowSizeHandler);
return viewer.run();
}
我在屏幕上画了10个点,给该Geometry设置了一个更新回调,想在回调里面删除5个点。但是无论怎么设置,屏幕上仍然显示有9个点。请问一下是什么原因?哪里没有设置好??? |
|