osg动态修改坐标

class CoordUpdateCallback : public osg::NodeCallback
{
public:

    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
    {
        if (nv->getFrameStamp())
        {

            osg::Geode* geode = node->asGeode();
            osg::Geometry* geom = geode->getDrawable(0)->asGeometry();
            // 获取坐标数组
            osg::Array* tmp = geom->getVertexArray();
            osg::Vec2Array* coorArray = (osg::Vec2Array*) tmp;
            auto it = coorArray->begin();
            for (; it < coorArray->end(); it++)
            {
                // 动起来
                it->x() += 100;
            }
            geom->setVertexArray(coorArray);

        }
    }

};

int main
{
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
    v->push_back(osg::Vec3(0, -6378137, 0));
    v->push_back(osg::Vec3(0, -2 * 6378137, 0));
    geom->setVertexArray(v.get());
    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
    c->push_back(osg::Vec4(.92f, .92f, .71f, 1.f));
    geom->setColorArray(c.get());
    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
    osg::ref_ptr <osg::Geode> geode = new osg::Geode;
    geode->addDrawable(geom.get());
    geode->setUpdateCallback(new CoordUpdateCallback());//设置更新

    root->addChild(geode.get());
    osgViewer::Viewer viewer;
    viewer.setSceneData(root);
    viewer.run()
}

通过设置public osg::NodeCallback,重写operate()达到修改点的坐标直接修改位置。

你可能感兴趣的:(osg学习)