OSG(OpenSceneGraph)基础学习矩阵变换节点

矩阵变换节点osg::MatrixTransform该节点与位置变换节点osg::PositionAttitudeTransform一样都继承自osg::Transform。矩阵变换节点可以用来进行矩阵的运算和坐标系的变换。在矩阵运算的基础上来完成一些变换。其实我感觉位置变换节点osg::PositionAttitudeTransform可以完成的工作矩阵变换节点都可以完成。
矩阵osg::Matrix可以实现
平移makeTranslate( const Vec3d& );
旋转makeRotate( value_type angle, const Vec3d& axis );
缩放makeScale( const Vec3d& );
矩阵变换节点osg::MatrixTransform常用的一些函数:
设置变换矩阵:setMatrix(const Matrix& mat);
矩阵相乘:preMult(const Matrix& mat)
postMult(const Matrix& mat)
得到逆矩阵:inline const Matrix& getInverseMatrix() const
osg::ref_ptr<</span>osg::Node> MatrixTransformTest::CreateNode() { osg::ref_ptr<</span>osg::Group> _root = new osg::Group;//依然创建节点,咱继续把牛弄进来玩 osg::ref_ptr<</span>osg::Node> _node = osgDB::readNodeFile("cow.osg"); //构造一个变换矩阵节点 osg::ref_ptr<</span>osg::MatrixTransform> _mt = new osg::MatrixTransform; //创建一个矩阵对象,下面这个经过实践,只能保持最后一次的效果,也就是说先平移,再旋转的话,只能保留旋转而不能平移 osg::Matrix _m; _m.makeTranslate(osg::Vec3(10.0f,0.0f,0.0f));//X方向平移10个单位 _m.makeRotate(45.0f, osg::Vec3(1.0f,0.0f,0.0f));//绕着后面这个轴转45度 _mt->setMatrix(_m); _mt->addChild(_node.get()); //构造一个变换矩阵节点 osg::ref_ptr<</span>osg::MatrixTransform> _mt1 = new osg::MatrixTransform; //创建一个矩阵对象 //要达到叠加效果,需要矩阵相乘,一下两种方式可以得到 // osg::Matrix _m1 = osg::Matrix::translate(osg::Vec3(-10.0f, 0.0f, 0.0f)) // *osg::Matrix::rotate(-45.0f, osg::Vec3(1.0f, 0.0f, 0.0f));//X方向平移-10个单位//_m1.makeRotate();//绕着后面这个轴转45度 osg::Matrix _m1,_m2; _m1.makeTranslate(osg::Vec3(-10.0f, 0.0f, 0.0f));//X方向平移10个单位 _m2.makeRotate(-45.0f, osg::Vec3(1.0f, 0.0f, 0.0f));//绕着后面这个轴转-45度 _m1 = _m1*_m2; _mt1->setMatrix(_m1); _mt1->addChild(_node.get()); _root->addChild(_mt.get()); _root->addChild(_mt1.get()); _root->addChild(_node.get());//增加未变换的点,用来对比一下 return _root.get(); }
http://s12/mw690/002o0ovtzy7pvbuXN1x0b&690
http://s14/mw690/002o0ovtzy7pvbuMsoJad&690
本文摘自:https://www.qxqzx.com/contents/272.html