osgViewer::Viewer缩放、平移对单个模型不起作用

    科技2024-06-06  74

     当osgViewer中只有一个模型时,osg::PositionAttitudeTransform 、osg::MatrixTransform对模型缩放、平移不起作用,只有大于1个模型时才起作用,原因不知道为啥,如下代码不会平移、缩放模型

    #include <osgViewer/Viewer> #include <osgViewer/CompositeViewer> #include <osg/Node> #include <osg/Geode> #include <osg/Group> #include <osg/Geometry> #include <osg/Camera> #include <osg/MatrixTransform> #include <osg/PositionAttitudeTransform> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osgUtil/Optimizer> #include <iostream> int main() { osg::ref_ptr<osgViewer::Viewer> spViewer = new osgViewer::Viewer; osg::ref_ptr<osg::Group> spGroup = new osg::Group; osg::ref_ptr<osg::PositionAttitudeTransform> spTranform = new osg::PositionAttitudeTransform; osg::ref_ptr<osg::Node> spNode = osgDB::readNodeFile("glider.osg"); spTranform->setScale(osg::Vec3(3, 2, 4)); // 此句不起作用 spTranform->addChild(spNode); spGroup->addChild(spTranform); spViewer->setSceneData(spGroup); spViewer->realize(); spViewer->run(); return 0; }

    下面的代码也不起作用

    #include <osgViewer/Viewer> #include <osgViewer/CompositeViewer> #include <osg/Node> #include <osg/Geode> #include <osg/Group> #include <osg/Geometry> #include <osg/Camera> #include <osg/MatrixTransform> #include <osg/PositionAttitudeTransform> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osgUtil/Optimizer> #include <iostream> int main() { osg::ref_ptr<osgViewer::Viewer> spViewer = new osgViewer::Viewer; osg::ref_ptr<osg::MatrixTransform> spTranform = new osg::MatrixTransform; osg::ref_ptr<osg::Node> spNode = osgDB::readNodeFile("E:/osg/OpenSceneGraph-Data-3.0.0/cow.osg"); osg::Matrix m1 = spTranform->getMatrix(); // 下面这句不会平移模型 m1 *= osg::Matrix::translate(osg::Vec3(-10.0f, 0.0f, 0.0f)); spTranform->setMatrix(m1); spTranform->addChild(spNode); spViewer->setSceneData(spTranform); spViewer->realize(); spViewer->run(); return 0; }

     

     

    Processed: 0.013, SQL: 8