[osg-users] How to properly update a Geometry

Daniel Neos daniel.rd at hotmail.de
Fri Apr 29 08:55:25 PDT 2016


gwaldron wrote:
> If you're just updating an existing array, you don't need to call setVertexArray (etc); but you need to mark it dirty by calling
> 
>   m_vertices->dirty();
> 
> 
> That applies also to your other buffer objects (color array, elements, etc.)
> 
> 
> 
> Glenn Waldron
> 
> 


Hi Glenn, 

thanks for your solution, I somehow came up with a solution which calls setVertexArray() anyway,
because I am not feeling confident when updating a geometry with dynamically changing 
size of vertices each frame.

In my numbercrunching class, I create a geometry which I pass to
the visiualization class.


Code:

        osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array());
        osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array());

        for (int i = 0; i < nPixel; i++)
        {
          // Fill in vertices and colors...
          // ....
          //
        }

        m_geometry->setVertexArray(vertices.get());
        m_geometry->setColorArray(colors.get());
        m_geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
        if (firstTimecall)
        {
            m_geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size()));
            this->attachGeometry(m_geometry);
        }
        else
        {
            this->updateGeometry(m_geometry, nPixel);
        }




Then I update the geometry of my visualization class like this


Code:

void OSGWidget::updateGeometry(osg::ref_ptr<osg::Geometry> geometry, const int nPixel)
{
        m_geometry->setVertexArray(geometry->getVertexArray());
        m_geometry->setColorArray(geometry->getColorArray());

        osg::DrawArrays* drawArrays = static_cast<osg::DrawArrays*>(m_geometry->getPrimitiveSet(0));
        drawArrays->setCount(nPixel);
        drawArrays->dirty();
    
        // ensures update of scene
        this->update();
}




This seems to work, but eventually there is a more efficient and elegant solution to exchange/ update vertice and colors.
I hope my issue is clear and small piece of samplecode will be appreciated. Thanks!

Cheers,
Daniel Neos

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=67028#67028








More information about the osg-users mailing list