OSG漫游到指定坐标点位置

OSG中从当前场景位置漫游到指定点坐标位置,osg中场景的视口状态包括如下参数:

1、视点的位置

2、参考点的位置,该点通常为场景中的中心轴上的点

3、视点向上的方向向量

( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up );

通过这三个参数就可以确定视口中模型的状态位置

实现的漫游代码如下:

 1 //是否漫游
 2 //视角移动的 效果
 3 void LargeScaleManipulator::setIsTrans(bool b)
 4 {
 5     isTrans = b;
 6 }
 7 void LargeScaleManipulator::setcurrentviewmatrix(osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up)
 8 {
 9     currenteye = eye;
10     currentcenter = center;
11     currentup = up;
12 }
13 
14 void LargeScaleManipulator::settargetviewmatrix(osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up)
15 {
16     targeteye = eye;
17     targetcenter = center;
18     targetup = up;
19 }
20 
21 void LargeScaleManipulator::calintervalviewmatrix()
22 {
23     intervalcenter = (targetcenter - currentcenter) / 60;
24     intervaleye = (targeteye - currenteye) / 60;
25     intervalup = (targetup - currentup) / 60;
26 }
27 
28 
29 bool LargeScaleManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
30 {    
31     if (ea.getEventType() == osgGA::GUIEventAdapter::FRAME)
32     {
33         if (isTrans)
34         {
35             if (fabs(intervaleye[0]) < 1e-6 && fabs(intervaleye[1]) < 1e-6)
36             {
37                 setTransformation(targeteye, targetcenter, targetup);
38                 isTrans = false;
39             }
40             if (((fabs(currenteye[0] - targeteye[0]) >= fabs(intervaleye[0])) || (fabs(currentcenter[0] - targetcenter[0]) >= fabs(intervalcenter[0])))
41                 &&((fabs(currenteye[1] - targeteye[1]) >= fabs(intervaleye[1])) || (fabs(currentcenter[1] - targetcenter[1]) >= fabs(intervalcenter[1]))))
42             {
43                 if (fabs(currenteye[0] - targeteye[0]) >= fabs(intervaleye[0]))
44                 {
45                     currenteye += intervaleye;
46                 }
47                 if (fabs(currentcenter[0] - targetcenter[0]) >= fabs(intervalcenter[0]))
48                 {
49                     currentcenter += intervalcenter;
50                 }
51                 currentup += intervalup;
52                 setTransformation(currenteye, currentcenter, currentup);
53             }
54             else
55             {
56                 setTransformation(targeteye, targetcenter, targetup);
57                 isTrans = false;
58             }
59         }
60     }
61     return OrbitManipulator::handle(ea, us);
62 }

以上就是通过传入一个当前位置状态,然后给定一个目标位置状态,通过设置漫游速度,场景就会一直朝着目标位置变换,也就有了漫游效果

调用代码如下:

 1   osg::Vec3d target;
 2     target.set(x, y, z);
 3     osg::Vec3d currenteye;
 4     osg::Vec3d currentcenter;
 5     osg::Vec3d currentup;
 6     osg::Vec3d targeteye;
 7     osg::Vec3d targetcenter;
 8     osg::Vec3d targetup;
 9 
10     m_pOsgView->getCamera()->getViewMatrixAsLookAt(currenteye, currentcenter, currentup);
11 
12     targeteye = target + currenteye - currentcenter;
13     targetcenter = target;
14     targetup = currentup;
15 
16     KeySwitchMatrixManipulator* keyswitchManipulator = dynamic_cast<KeySwitchMatrixManipulator*>(m_pDataManagerSingleton->getViewer()->getCameraManipulator());
17     if (keyswitchManipulator)
18     {
19         keyswitchManipulator->selectMatrixManipulator(0);
20         LargeScaleManipulator* mp = dynamic_cast<LargeScaleManipulator*>(keyswitchManipulator->getCurrentMatrixManipulator());
21         if (mp)
22         {
23             mp->setcurrentviewmatrix(currenteye, currentcenter, currentup);
24             mp->settargetviewmatrix(targeteye, targetcenter, targetup);
25             mp->calintervalviewmatrix();
26             mp->isTrans = true;
27         }
28     }

 

posted @ 2019-01-03 16:05  marblemm  阅读(1648)  评论(0编辑  收藏  举报