OSG学习:移动/缩放/旋转模型

 移动和缩放以及旋转都是对矩阵进行操作,这些操作如果要叠加直接矩阵相乘就可以了。

下面的示例代码中,加入了四个bignathan,一个是默认加入在最中间,一个向上移2单位,一个是向下移2单位且缩放0.5倍,另一个是向右4单位,缩放0.5且平躺45度。

#include<osgDB\ReadFile>
#include<osgViewer\Viewer>
#include<osg\Node>
#include<osg\MatrixTransform>

void main()
{
        osgViewer::Viewer viewer;
        osg::ref_ptr<osg::Group> root = new osg::Group();

        osg::ref_ptr<osg::Node> bignathan = osgDB::readNodeFile("bignathan.osg");

        osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
        trans->setMatrix(osg::Matrix::translate(0, 0, 2));
        trans->addChild(bignathan.get());

        osg::ref_ptr<osg::MatrixTransform> scale = new osg::MatrixTransform;
        scale->setMatrix(osg::Matrix::scale(0.5, 0.5, 0.5)*osg::Matrix::translate(0, 0, -2));
        scale->addChild(bignathan.get());

        osg::ref_ptr<osg::MatrixTransform> rot = new osg::MatrixTransform;
        rot->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), 1, 0, 0)*osg::Matrix::scale(0.5, 0.5, 0.5)*osg::Matrix::translate(4, 0, -2));
        rot->addChild(bignathan.get());

        root->addChild(bignathan.get());
        root->addChild(trans.get());
        root->addChild(scale.get());
        root->addChild(rot.get());

        viewer.setSceneData(root.get());
        viewer.realize();
        viewer.run();
}

结果图:


posted @ 2017-07-02 16:28  huahai  阅读(1683)  评论(0编辑  收藏  举报