|
楼主 |
发表于 2012-7-16 17:01:50
|
显示全部楼层
- #include <Windows.h>
- #include <osg/Math>
- #include <osgDB/ReadFile>
- #include <osg/NodeCallback>
- #include <osg/MatrixTransform>
- #include <osg/PositionAttitudeTransform>
- #include <osg/DrawPixels>
- #include <osg/Texture2D>
- #include <osg/Array>
- #include <osg/Geometry>
- #include<osg/Node>
- #include<osg/Geode>
- #include<osg/Group>
- #include <osg/Point>
- #include<osgViewer/Viewer>
- #include<osgViewer/ViewerEventHandlers>
- #include<osg/MatrixTransform>
- #include<osgGA/CameraManipulator>
- #include<osgDB/ReadFile>
- #include<osgDB/WriteFile>
- #include<osgUtil/Optimizer>
- #include <osgUtil/IntersectVisitor>
- #include <vector>
- #include<iostream>
- #include <osgGA/KeySwitchMatrixManipulator>
- #include <osgGA/TerrainManipulator>
- #include <osgGA/TrackballManipulator>
- #include <osg/Matrixd>
- #include <osg/Quat>
- //osg::ref_ptr<osg::Node>m_node=NULL;
- class TravelManipulator:public osgGA::CameraManipulator
- {
- public:
- TravelManipulator():m_fMoveSpeed(1.5f),
- m_bLeftButtonDown(false),
- m_fpushX(0),
- m_fpushY(0),
- m_fAngle(2.5),
- m_bPeng(true)
- {
- //m_vPosition=osg::Vec3(0.0f,0.0f,100.0f);
- //m_vRotation=osg::Vec3(osg::PI_2,0.0f,0.0f);
-
- }
- ~TravelManipulator()
- {
- }
- /*static TravelManipulator*travelToScence(osg::ref_ptr<osgViewer::Viewer>viewer)
- {
- TravelManipulator*camera=new TravelManipulator;
- viewer->setCameraManipulator(camera);
- camera->m_pHostViewer=viewer;
- return camera;
- }*/
- private:
- //osg::ref_ptr<osgViewer::Viewer>m_pHostViewer;
- osg::ref_ptr<osg::Node>m_node;
- float m_fMoveSpeed;
- osg::Vec3 m_vPosition;
- osg::Vec3 m_vRotation;
- osg::Vec3 m_vScale;
- //float m_vdistance;
- //osg::Matrixd m_Matrix;
- public:
- bool m_bLeftButtonDown;
- float m_fpushX;
- float m_fpushY;
-
- virtual void setByMatrix(const osg::Matrixd& matrix)
- {
- //osg::Quat quat(osg::inDegrees(90.0f),osg::Vec3f(1.0,0.0,0.0));
-
- //m_vRotation=(matrix.getRotate()/**quat*/).asVec3();
- //m_Matrix=matrix;
- double angle0,angle1,angle2;
- osg::Quat quat=matrix.getRotate();
- quat.getRotate(angle0,osg::Vec3f(1.0,0.0,0.0));
- quat.getRotate(angle1,osg::Vec3f(0.0,1.0,0.0));
- quat.getRotate(angle2,osg::Vec3f(0.0,0.0,1.0));
- m_vRotation[0]=osg::inRadians(angle0);
- m_vRotation[1]=osg::DegreesToRadians(angle1)/*-osg::PI_2*/;
- m_vRotation[2]=osg::DegreesToRadians(angle2);
- //m_vRotation=matrix.getRotate().asVec3();
- m_vPosition=matrix.getTrans();
-
- }
- virtual void setByInverseMatrix(const osg::Matrixd& matrix)
- {
-
- osg::Matrixd matrix1= osg::Matrixd::inverse(matrix);
- osg::Quat quat=matrix1.getRotate();
- m_vPosition=matrix1.getTrans();
- double angle0,angle1,angle2;
- quat.getRotate(angle0,osg::Vec3f(1.0,0.0,0.0));
- quat.getRotate(angle1,osg::Vec3f(0.0,1.0,0.0));
- quat.getRotate(angle2,osg::Vec3f(0.0,0.0,1.0));
- m_vRotation[0]=osg::DegreesToRadians(angle0);
- m_vRotation[1]=osg::DegreesToRadians(angle1)/*-osg::PI_2*/;
- m_vRotation[2]=osg::DegreesToRadians(angle2);
- //m_vRotation=matrix1.getRotate().asVec3();
- //m_Matrix=matrix1;
- }
- virtual osg::Matrixd getMatrix(void)const
- {
- //return m_Matrix;
- osg::Matrixd mat;
- mat.makeRotate(m_vRotation._v[0],osg::Vec3f(1.0,0.0,0.0),m_vRotation._v[1],osg::Vec3f(0.0,1.0,0.0),m_vRotation._v[2],osg::Vec3f(0.0,0.0,1.0));
- return mat*osg::Matrixd::translate(m_vPosition);
- }
- virtual osg::Matrixd getInverseMatrix()const
- {
- /*return osg::Matrixd::inverse(m_Matrix);*/
- osg::Matrixd mat;
- mat.makeRotate(m_vRotation._v[0],osg::Vec3f(1.0,0.0,0.0),m_vRotation._v[1],osg::Vec3f(0.0,1.0,0.0),m_vRotation._v[2],osg::Vec3f(0.0,0.0,1.0));
- return osg::Matrixd::inverse(mat*osg::Matrixd::translate(m_vPosition));
- }
- virtual bool handle(const osgGA::GUIEventAdapter&ea,osgGA::GUIActionAdapter&us)
- {
-
- float mouseX=ea.getX();
- float mouseY=ea.getY();
-
- switch(ea.getEventType())
- {
- case(osgGA::GUIEventAdapter::KEYDOWN):
- {
- if (ea.getKey()==0x20)//空格键
- {
- us.requestRedraw();
- us.requestContinuousUpdate(false);
- //m_pHostViewer->getCameraManipulator()->setHomePosition(osg::Vec3d(0.0,-5.0,0.0),osg::Vec3d(0.0,0.0,0.0),osg::Vec3d(0.0,0.0,1.0));
- return true;
- }
- if (ea.getKey()==0xFF50)// KEY_Home = 0xFF50,KEY_Left = 0xFF51,KEY_Up = 0xFF52, KEY_Right = 0xFF53, KEY_Down = 0xFF54,
- {
- ChangePosition(osg::Vec3(0,0,m_fMoveSpeed));//抬高视点
- return true;
- }
- if (ea.getKey()==0xFF57)//KEY_End = 0xFF57
- {
- ChangePosition(osg::Vec3(0,0,-m_fMoveSpeed));//降低视点
- return true;
- }
- if (ea.getKey()==0x2B)//KEY_Plus = 0x2B “+”号键
- {
- m_fMoveSpeed+=1.0f;
- return true;
- }
- if (ea.getKey()==0x2D)//KEY_Minus = 0x2D “-”号键
- {
- m_fMoveSpeed-=1.0f;
- if (m_fMoveSpeed<1.0f)
- {
- m_fMoveSpeed=1.0f;
- }
- return true;
- }
- if (ea.getKey()== 0xFF52||ea.getKey()== 0x57||ea.getKey()== 0x77)//,KEY_Up = 0xFF52
- {
- ChangePosition(osg::Vec3(0,m_fMoveSpeed*sinf(osg::PI_2+m_vRotation._v[2]),0));
- ChangePosition(osg::Vec3(m_fMoveSpeed*cosf(osg::PI_2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()== 0xFF54||ea.getKey()== 0x53||ea.getKey()== 0x73)// KEY_Down = 0xFF54
- {
- ChangePosition(osg::Vec3(0,-m_fMoveSpeed*sinf(osg::PI_2+m_vRotation._v[2]),0));
- ChangePosition(osg::Vec3(-m_fMoveSpeed*cosf(osg::PI_2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()== 0x41||ea.getKey()== 0x61)//A 向左
- {
- ChangePosition(osg::Vec3(-m_fMoveSpeed*sinf((osg::PI)/2+m_vRotation._v[2]),0,0));
- ChangePosition(osg::Vec3(-m_fMoveSpeed*cosf((osg::PI)/2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()== 0x44||ea.getKey()== 0x64)//D 向右
- {
- ChangePosition(osg::Vec3(m_fMoveSpeed*sinf(osg::PI_2+m_vRotation._v[2]),0,0));
- ChangePosition(osg::Vec3(m_fMoveSpeed*cosf(osg::PI_2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()== 0xFF53)//KEY_Right转弯向右
- {
- m_vRotation._v[2]-=osg::DegreesToRadians(m_fAngle);
- return true;
- }
- if (ea.getKey()== 0xFF51)//KEY_Left转弯向左
- {
- m_vRotation._v[2]+=osg::DegreesToRadians(m_fAngle);
- return true;
- }
- if (ea.getKey()== 0x46||ea.getKey()== 0x66)//F 改变屏角
- {
- computeHomePosition();
- m_fAngle-=0.2;
- return true;
- }
- if (ea.getKey()== 0x47||ea.getKey()== 0x67)//G
- {
- m_fAngle+=0.2;
- return true;
- }
- return false;
- }
- case(osgGA::GUIEventAdapter::PUSH)://鼠标按下
- {
- if (ea.getButton()==1)
- {
- m_fpushX=mouseX;
- m_fpushY=mouseY;
- m_bLeftButtonDown=true;
- }
- return false;
- }
- case(osgGA::GUIEventAdapter::DRAG)://拖动
- if (m_bLeftButtonDown)
- {
- m_vRotation._v[2]-=osg::DegreesToRadians((m_fAngle/1000)*(mouseX-m_fpushX));
- //m_vRotation._v[0]+=osg::DegreesToRadians(1.1*(mouseY-mouseX));
- /*if (m_vRotation._v[0]>=3.14)
- {
- m_vRotation._v[0]=3.14;
- }
- if (m_vRotation._v[0]<=0)
- {
- m_vRotation._v[0]=0;
- }*/
- if (mouseY-m_fpushY<0)
- {
- ChangePosition(osg::Vec3f (0.0,10.0,0.0));
- }
- if (mouseY-m_fpushY>0)
- {
- ChangePosition(osg::Vec3f (0.0,-10.0,0.0));
- }
- }
- return false;
- case(osgGA::GUIEventAdapter::RELEASE)://鼠标释放
- {
- if (ea.getButton()==1)
- {
- m_bLeftButtonDown=false;
- }
- return false;
- }
- default:
- return false;
- }
- }
- float m_fAngle;
- void ChangePosition(osg::Vec3&delta)
- {
- if (m_bPeng)
- {
- osg::Vec3 newPos1=m_vPosition+delta;
- osgUtil::IntersectVisitor ivXY;
- osg::ref_ptr<osg::LineSegment>lineXY=new osg::LineSegment(newPos1,m_vPosition);
- osg::ref_ptr<osg::LineSegment>lineZ=new osg::LineSegment(newPos1+osg::Vec3(0.0f,0.0f,m_fMoveSpeed),newPos1-osg::Vec3f(0.0,0.0,m_fMoveSpeed));
- ivXY.addLineSegment(lineZ.get());
- ivXY.addLineSegment(lineXY.get());
- //m_pHostViewer->getSceneData()->accept(ivXY);
- m_node->accept(ivXY);
- if (!ivXY.hits())
- {
- m_vPosition+=delta;
- }
- }
- else
- {
- m_vPosition+=delta;
- }
- //m_vPosition+=delta;
- }
- bool m_bPeng;
- float getSpeed()
- {
- return m_fMoveSpeed;
- }
- void setSpeed(float&sp)
- {
- m_fMoveSpeed=sp;
- }
- //void setDistance( double distance )
- //{
- // m_vdistance = distance;
- //}
- ///** Get the distance of the camera to the center. */
- //double getDistance() const
- //{
- // return m_vdistance;
- //}
- void SetPosition(osg::Vec3&position)
- {
- m_vPosition=position;
- }
- osg::Vec3 GetPosition()
- {
- return m_vPosition;
- }
- void setNode(osg::Node* node)
- {
- m_node=node;
- }
- void computeHomePosition()
- {
- if (m_node.get())
- {
- const osg::BoundingSphere&boundingSpere=m_node->getBound();
- osg::Vec3 bp=boundingSpere._center;
- m_vRotation=osg::Vec3(osg::PI_2,0.0f,0.0f);
- SetPosition(bp);
- }
- }
- };
- int main()
- {
- osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer();
- //TravelManipulator::travelToScence(viewer.get());
- osg::ref_ptr<osg::Group>root=new osg::Group();
- osg::ref_ptr<osg::Node>node=osgDB::readNodeFile("lz.osg");
- osg::ref_ptr<osgGA::AnimationPathManipulator> apm = new osgGA::AnimationPathManipulator("../data/animation.path");
- //viewer->setCameraManipulator(apm.get());
- osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
- // keyswitchManipulator->setNode(node.get());
- //keyswitchManipulator->addMatrixManipulator( '4', "TrackballTravel", new osgGA::TrackballManipulator());
- keyswitchManipulator->addMatrixManipulator( '1', "TerrianTravelTravel", new osgGA::TerrainManipulator());
- keyswitchManipulator->addMatrixManipulator( '2', "ControlTravel", new TravelManipulator());
- keyswitchManipulator->addMatrixManipulator( '3', "path",apm );
-
- //m_node=node.get();
- root->addChild(node.get());
- /*osgUtil::Optimizer optimizer;
- optimizer.optimize(root.get());*/
- viewer->setSceneData(root.get());
- viewer->setCameraManipulator( keyswitchManipulator.get() );
- //viewer->setCameraManipulator(new TravelManipulator() );
- viewer->realize();
- viewer->run();
- //while(!viewer->done())
- //{
- //
- // viewer->frame();
- // // 让其它程也占用CPU,放出MS
- //}
-
- return 0;
- }
复制代码 可是osgGA::TerrainManipulator()或是自定义的漫游器,他们之间切换是可以的,贴上代码,望指点~~~ |
|