osgGA::KeySwitchMatrixManipulator 跟随
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osg/Group> #include <osg/Camera> #include <osgDB/ReadFile> #include <osg/Node> #include <osg/Geometry> #include <osg/Image> #include <osg/ShapeDrawable> #include <osg/Texture2D> #include <osg/MatrixTransform> #include <osg/AnimationPath> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgGA/DriveManipulator> #include <osgGA/GUIEventHandler> #include <osgGA/GUIEventAdapter> #include <osgGA/GUIActionAdapter> #include <osgGA/AnimationPathManipulator> #include <osgGA/KeySwitchMatrixManipulator> #include <osgUtil/LineSegmentIntersector> #include <iostream> using namespace std; class Follow:public osgGA::KeySwitchMatrixManipulator { public: Follow(osgViewer::Viewer *viewerParam) { m_vPosition = osg::Vec3(0.0, -200.0, -70.0); m_vRotation = osg::Vec3(osg::PI_2, 0.0f, 0.0f); m_fMoveSpeed = 2.0; m_fAnglg = 2.5; viewer1 = viewerParam; } virtual void setByMatrix() { } virtual void setByInverseMatrix() { } virtual osg::Matrixd getMatrix() const { osg::Matrixd mat1; mat1.makeRotate(m_vRotation.x(), osg::Vec3(1.0, 0.0, 0.0), m_vRotation.y(), osg::Vec3(0.0, 1.0, 0.0), m_vRotation.z(), osg::Vec3(0.0, 0.0, 1.0)); return mat1*osg::Matrixd::translate(m_vPosition); } virtual osg::Matrixd getInverseMatrix() const { osg::Matrixd mat1; mat1.makeRotate(m_vRotation.x(), osg::Vec3(1.0, 0.0, 0.0), m_vRotation.y(), osg::Vec3(0.0, 1.0, 0.0), m_vRotation.z(), osg::Vec3(0.0, 0.0, 1.0)); return osg::Matrixd::inverse(mat1*osg::Matrixd::translate(m_vPosition)); } virtual float getFusionDistanceValue() const { //return _current->getFusionDistanceValue(); return viewer1->getFusionDistanceValue(); } virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return viewer1->getFusionDistanceMode(); } void changePosition(osg::Vec3 delta) { m_vPosition += delta; } bool handle(const osgGA::GUIEventAdapter& gea,osgGA::GUIActionAdapter& gaa) { // switch (gea.getEventType()) { case osgGA::GUIEventAdapter::DOUBLECLICK: std::cout << "-double click--" << std::endl; break; case osgGA::GUIEventAdapter::KEYDOWN: //std::cout << "-ket down--" << std::endl; if (gea.getKey()==87) { changePosition(osg::Vec3(m_fMoveSpeed*cosf(osg::PI_2 + m_vRotation[2]), m_fMoveSpeed*sinf(osg::PI_2 + m_vRotation[2]), 0)); } else if (gea.getKey()==83) { changePosition(osg::Vec3(-m_fMoveSpeed*cosf(osg::PI_2 + m_vRotation[2]), -m_fMoveSpeed*sinf(osg::PI_2 + m_vRotation[2]), 0)); } else if (gea.getKey()== 0xFF53) { m_vRotation._v[2] -= osg::DegreesToRadians(m_fAnglg); } else if (gea.getKey()==0xFF51) { m_vRotation._v[2] += osg::DegreesToRadians(m_fAnglg); } else { } break; default: break; } return false; } private: osg::Vec3 m_vPosition; osg::Vec3 m_vRotation; float m_fMoveSpeed; float m_fAnglg; osgUtil::SceneView::FusionDistanceMode model1; osgViewer::Viewer *viewer1; }; int main() { osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer; osg::ref_ptr<osg::Group> group1 = new osg::Group; osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\四合院2019.osgb"); group1->addChild(node1.get()); viewer1->setSceneData(group1); viewer1->setUpViewInWindow(200, 200, 800, 600, 0); viewer1->setCameraManipulator(new Follow(viewer1)); //viewer1->addEventHandler(new osgViewer::WindowSizeHandler); viewer1->realize(); return viewer1->run(); }
QQ 3087438119