查看: 1356|回复: 4

漫游时,如何让模型居于场景中心?

[复制链接]

该用户从未签到

发表于 2014-8-19 10:04:42 | 显示全部楼层 |阅读模式
本帖最后由 abc-osg 于 2014-8-19 10:13 编辑

osg::ref_ptr<osg::Group> mRoot;//根节点
osg::ref_ptr<osg::MatrixTransform> omt;
mRoot  = new osg::Group;
         
omt = new osg::MatrixTransform() ;
mRoot->addChild(omt);
        omt->setDataVariance( osg::Object:: DYNAMIC );
        osg::ref_ptr<osg::Node> mModelnew = osgDB::readNodeFile("car.3ds");
        omt->addChild(mModelnew);//mModelnew);
        osg::Matrix mx;
     mx.makeTranslate(osg::Vec3(4568.5312,-8387.43457,20.0f));
         omt->setMatrix(mx);
漫游器:
class Manipulator:public osgGA::TrackballManipulator
    osg::ref_ptr<osg::AnimationPath> _animationPath;//漫游路径
   osg::Matrix _animationMatrix;
osg::Matrixd Manipulator::getInverseMatrix() const
{
         
        if(_beginAnimation&&_animationPath.valid())
        {
                /*    osg::Vec3d _eye,_center,_up;
                    _animationMatrix.getLookAt(_eye,_center,_up);
                   _center=this->_center;
                   osg::Vec3 dir=_eye-_center;
                   dir.normalize();*/

                return _animationMatrix;
        }
}
bool Manipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&us);
        if(!viewer)
                return false;      
         
            if(_animationPath.valid())
        {
                 
                if(_animationPath->getLastTime()-_animationPath->getFirstTime()>=_animationedTime)
//省略时间 漫游没有任何问题
                {
                        _animationPath->getInverse(_animationedTime,_animationMatrix);
                                             //如何让模型居于场景中心
                               }
                }
              else
                               osgGA::TrackballManipulator::handle(ea,us);
              return false;
}

该用户从未签到

发表于 2014-8-20 14:17:38 | 显示全部楼层
本帖最后由 cenfer 于 2014-8-20 14:18 编辑
abc-osg 发表于 2014-8-20 11:15
animationMatrix是漫游时的时时矩阵,如何把模型设置到场景中心?


再分析下你的需求

1. 视野中心肯定是物体包围盒的中心center

2. up可以保持原来的方向

3. 视角Dir也可以保持

4.视点eye 可以是物体包围盒半径R的K倍
   eye = center - Dir*K*R

5.保持信息的求取

camara->getMatix()->getlookat(centerOri,eyeOri,upOri)
或者camara->getInverseMatix()->getlookat(centerOri,eyeOri,upOri)

up = upOri  
dir = centerOri-eyeOri  单位化

大概描述下,有些接口不一定准确


该用户从未签到

发表于 2014-8-20 09:52:29 | 显示全部楼层
setCenter/setDistanse/设置合适的Eye已经at/up

该用户从未签到

 楼主| 发表于 2014-8-20 11:15:49 | 显示全部楼层
animationMatrix是漫游时的时时矩阵,如何把模型设置到场景中心?

该用户从未签到

发表于 2014-8-20 13:55:48 | 显示全部楼层
本帖最后由 cenfer 于 2014-8-20 13:59 编辑
abc-osg 发表于 2014-8-20 11:15
animationMatrix是漫游时的时时矩阵,如何把模型设置到场景中心?


1. 相机操作器中改变相机姿态的基本接口是
virtual void  setByMatrix (const osg::Matrixd &matrix)
virtual void  setByInverseMatrix (const osg::Matrixd &matrix)

2. 这俩接口需要子类实现。osg中有些现成的相机操作器并没有很好的实现这些接口。
这涉及相机姿态矩阵和构造姿态矩阵相关变量之间的同步问题。

3. 最灵活的方式是根据需求构造center,eye点和up方向向量,生成相机姿态矩阵,然后用上述接口。
具体用哪个和参数的空间有关,不明白交替试一下就可以了。

4. trackball这个操作器就不适合setByMatrix 接口。trackball有自己的姿态矩阵构造变量center,distance等。
如果用setByMatrix ,而setByMatrix 里并没有实现与变量center,distance的同步。所以setByMatrix后,稍微一动鼠标,视野就大乱了。

5. 自己做好调查,看看现有操作器能不能满足需求,不满足只能自己继承CameraManipulator实现一个相机操作器。
当然可以参照现有的进行改进,不需要完全重写。




您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表