osg缩放和平移模型

    技术2022-07-10  95

    void OsgCanvas4::init() { int w = width(); int h = height(); _graphicsWindow = new osgViewer::GraphicsWindowEmbedded(0, 0, w, h); _viewer = new osgViewer::Viewer; osg::ref_ptr<osg::Camera> camera = new osg::Camera /*_viewer->getCamera()*/; camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 0.5)); camera->setGraphicsContext(_graphicsWindow); camera->setProjectionMatrixAsPerspective(30.0f, double(w) / h, 1.0f, 10000.0f); camera->setViewport(new osg::Viewport(0, 0, w, h)); _viewer->setCamera(camera); _root = new osg::Group; osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("F:\\OSG_Qt5.10_64\\OpenSceneGraph-Data-3.0.0\\cow.osgt"); osg::ref_ptr<osg::Node> xyz = osgDB::readNodeFile("F:\\OSG_Qt5.10_64\\OpenSceneGraph-Data-3.0.0\\xyz.osgt"); //平移模型 osg::ref_ptr<osg::MatrixTransform> translate = new osg::MatrixTransform; translate->setMatrix(osg::Matrix::translate(-20, 0, 0)); translate->addChild(cow.get()); _root->addChild(translate); _root->addChild(cow); //缩放模型 osg::ref_ptr<osg::MatrixTransform> scale = new osg::MatrixTransform; scale->setMatrix(osg::Matrix::scale(5.0, 5.0, 5.0)); scale->addChild(xyz); _root->addChild(scale); #if 0 osg::ComputeBoundsVisitor visitor; _root->accept(visitor); osg::BoundingBox box = visitor.getBoundingBox(); osg::Vec3 center = box.center(); osg::Vec3 eye = osg::Vec3(20, 0, 0); osg::Vec3 up = osg::Vec3(0, 0, 1.0); camera->setViewMatrixAsLookAt(eye, center, up); #else _viewer->setCameraManipulator(new osgGA::TrackballManipulator); #endif _viewer->addEventHandler(new osgViewer::WindowSizeHandler); _viewer->addEventHandler(new osgViewer::StatsHandler); _viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); _viewer->addEventHandler(new osgViewer::WindowSizeHandler); _viewer->setSceneData(_root); }

    Processed: 0.024, SQL: 9