osg中使用MatrixTransform来实现模型的平移/旋转/缩放

MatrixTransform是从Transform - Group继承而来,因此可以在它的下面挂接Node对象。

通过设置其矩阵,来实现其下子节点的模型变换。

-- 用局部坐标系来理解(局部坐标系又称惯性坐标系,其与模型的相对位置在变换的过程中始终不变)

如下代码:

 
01// 创建圆柱体
02double r = 0.5;
03double h = 3.0;
04osg::Vec3 orginPt(0.0, 0.0, 0.0);
05osg::ref_ptr<osg::Geode> cylinderGeode = new osg::Geode;
06osg::ref_ptr<osg::Cylinder> geoCylinder = new osg::Cylinder(orginPt, r, h);
07osg::ref_ptr<osg::ShapeDrawable> cylinderDrawable = new osg::ShapeDrawable(geoCylinder.get());
08cylinderDrawable->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
09cylinderGeode->addDrawable(cylinderDrawable.get());
10  
11// -- 以下操作都是针对局部坐标系而言 --
12// 先将圆柱体平移(20.0, -12.0, -35.0)
13// 再将z轴方向旋转至向量n方向  此时局部坐标系的z轴和n向量一致
14// 接着,将旋转后的模型的沿z方向平移0.5*h长度 (全局坐标系,相当于沿n方向平移0.5*h长度)
15// 最后将模型放大2倍
16osg::Vec3 n(1.0, 1.0, -1.0);
17osg::Vec3 z(0.0, 0.0, 1.0);
18n.normalize();
19osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
20mt->setMatrix(osg::Matrix::scale(osg::Vec3(2.0, 2.0, 2.0))* 
21             osg::Matrix::translate(osg::Vec3(0, 0, 0.5*h))*
22        osg::Matrix::rotate(z, n)*
23        osg::Matrix::translate(osg::Vec3(20.0, -12.0, -35.0)));
24mt->addChild(cylinderGeode);

根据上面的特点,可以计算变化后的模型的三维坐标。

对于下图的包含关系(MatrixTransform A中包含一个MatrixTransform B,MatrixTransform B中包含一个模型

那么,模型的新坐标 (X, Y, Z) = (x,y,z)* B.matrix * A.matrix;

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值