osg指定路径点漫游器

文章介绍了如何派生自osgGA::CameraManipulator类来创建一个自定义的漫游器。通过设置位置、朝向、矩阵和逆矩阵,实现了对视点的控制。在handle函数中处理帧更新,以实现自由漫游。同时,文章详细讨论了坐标转换和插值方法,特别是从球面坐标到XYZ坐标的转换,以及根据路径点进行朝向计算。

本来打算用现成的路径点漫游器,发现很不好用,限制也多,干脆自己写一个得了。

一,派生于osgGA::CameraManipulator
class TravelManipulator : public osgGA::CameraManipulator
二,设置位置和朝向
//视点
osg::Vec3 m_vPosition;
//朝向
osg::Vec3 m_vRotation;
把初始位置可以放在原点,也可以任意一点。实际上,应该放在路径点的第0个位置。
m_vPosition = osg::Vec3(0, 0, 0);
值得注意的是,朝向要绕X轴转90度,才能和默认漫游器一样,look方向指向Y轴正向,即
m_vRotation = osg::Vec3(osg::PI_2, 0, 0);
这里用到了路径点,从X轴正向开始,所以再减去90度,使look方向的初始方向为X轴正向,便于计算。

m_vRotation = osg::Vec3(osg::PI_2, 0, -osg::PI_2);

三,设置漫游器的矩阵和逆矩阵,这个是固定的。没有它,就不能正确看到场景了。

/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByMatrix(const osg::Matrixd& matrix);

/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByInverseMatrix(const osg::Matrixd& matrix) ;

/** get the position of the manipulator as 4x4 Matrix.*/
virtual osg::Matrixd getMatrix() const;

/** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
virtual osg::Matrixd getInverseMatrix() const;

void TravelManipulator::setByMatrix(const osg::Matrixd& matrix)
{
}

void TravelManipulator::setByInverseMatrix(const osg::Matrixd& matrix)
{
}

osg::Matrixd TravelManipulator::getMatrix() const
{
osg::Matrixd mat1;
mat1.makeTranslate(m_vPosition);
osg::Matrixd mat2;
mat2.makeRotate(m_vRotation[0], osg::X_AXIS, m_vRotation[1], osg::Y_AXIS, m_vRotation[2], osg::Z_AXIS);

return mat2 * mat1;

}

osg::Matrixd TravelManipulator::getInverseMatrix() const
{
osg::Matrixd mat1;
mat1.makeTranslate(m_vPosition);
osg::Matrixd mat2;
mat2.makeRotate(m_vRotation[0], osg::X_AXIS, m_vRotation[1], osg::Y_AXIS, m_vRotation[2], osg::Z_AXIS);

return osg::Matrixd::inverse(mat2 * mat1);

}

四,在handle()中,使用frame,这样就可以自由漫游了
即在virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);

五,漫游器的朝向,不是坐标系的XYZ轴,而是PItch,r

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值