首页 > 学院 > 开发设计 > 正文

OSG学习笔记17——正确设置物体的缩放矩阵

2019-11-06 07:45:55
字体:
来源:转载
供稿:网友

问题:做了一个触摸屏上的三维模型的缩放功能,发现在对模型进行缩放的时候模型的位置会有偏差。

解决方法:

问了王大大之后,再查了一下资料,记录于此。

参考资料《在OSG中的实现三维物体的标准变换》 许昌学院学报 2008年 一篇久远的文章。

在OSG中进行变换容易出现问题的原因

1、基本变换是默认以世界坐标为变换中心的

2、我们所期望的变换是以物体为变换中心

因此,每次变换时,需要将变换中心和物体中心重合,这样才能得到我们想要的结果。

变换前先将物体平移至世界坐标的原点,实施变换之后再将物体进行反平移。

picked->setMatrix(originPos* osg::Matrix::translate(-center)//先将物体中心平移到世界坐标的原点					*osg::Matrix::scale(0.95, 0.95, 0.95)//缩放					*osg::Matrix::translate(center));//变换后再将物体移回				//	last_distance = distance;				originPos = picked->getMatrix();				center = picked->getBound().center();step1:获取变换物体表面顶点矩阵

originPos = picked -> getMatrix();

step2 获得变换物体的包围盒,从而获得物体的中心

center = picked->getBound().center();

step3:将物体中心平移到世界坐标原点

这里定义的平移矩阵就是osg::Matrix::translate(-center)

originPos* osg::Matrix::translate(-center)

step4:进行变换级联

这里我只做了缩放也就是scale,设置缩放矩阵。如果要同时进行平移旋转缩放操作的话,注意按照先缩放后旋转再平移的顺序。

originPos* osg::Matrix::translate(-center)*osg::Matrix::scale(0.95, 0.95, 0.95)

step5 :变换后再将物体移回

这里定义的平移矩阵就是osg::Matrix::translate(center)

originPos* osg::Matrix::translate(-center)*osg::Matrix::scale(0.95, 0.95, 0.95)*osg::Matrix::translate(center)

这样子物体的变换就没有什么毛病了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表