|
楼主 |
发表于 2011-9-2 13:39:16
|
显示全部楼层
- osg::Matrixd CSouth::getMatrix(void) const
- {
- osg::Matrixd mat;
- mat.makeRotate(m_vRotation._v[0], osg::Vec3(1.0f, 0.0f, 0.0f),
- m_vRotation._v[1], osg::Vec3(0.0f, 1.0f, 0.0f),
- m_vRotation._v[2], osg::Vec3(0.0f, 0.0f, 1.0f));
- return mat*osg::Matrixd::translate(m_vPosition);
- }
- osg::Matrixd CSouth::getInverseMatrix(void) const
- {
- osg::Matrixd mat;
- mat.makeRotate(m_vRotation._v[0], osg::Vec3(1.0f, 0.0f, 0.0f),
- m_vRotation._v[1], osg::Vec3(0.0f, 1.0f, 0.0f),
- m_vRotation._v[2], osg::Vec3(0.0f, 0.0f, 1.0f));
- return osg::Matrixd::inverse(mat*osg::Matrixd::translate(m_vPosition));
- }
- bool CSouth::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);
- return true;
- }
- if (ea.getKey()==0xFF50)
- {
- ChangePosition(osg::Vec3(0,0,m_fMoveSpeed));
- return true;
- }
- if (ea.getKey()==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)
- {
- 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)
- {
- ChangePosition(osg::Vec3(0,m_fMoveSpeed*cosf(osg::PI_2+m_vRotation._v[2]),0));
- ChangePosition(osg::Vec3(-m_fMoveSpeed*sinf(osg::PI_2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()==0x44||ea.getKey()==0x64)
- {
- ChangePosition(osg::Vec3(0,-m_fMoveSpeed*cosf(osg::PI_2+m_vRotation._v[2]),0));
- ChangePosition(osg::Vec3(m_fMoveSpeed*sinf(osg::PI_2+m_vRotation._v[2]),0,0));
- return true;
- }
- if (ea.getKey()==0xFF53)
- {
- m_vRotation._v[2]-=osg::DegreesToRadians(m_fAngle);
- }
- if (ea.getKey()==0xFF51)
- {
- m_vRotation._v[2]+=osg::DegreesToRadians(m_fAngle);
- }
- if (ea.getKey()==0x46||ea.getKey()==0x66)
- {
- computeHomePosition();
- m_fAngle-=0.2;
- return true;
- }
- if (ea.getKey()==0x47||ea.getKey()==0x67)
- {
- 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*(mouseX-m_fpushX));
- m_vRotation._v[0] += osg::DegreesToRadians(1.1*(mouseY-m_fpushY));
- if (m_vRotation._v [0] >= 3.14) m_vRotation._v [0] = 3.14;
- if (m_vRotation._v [0] <= 0) m_vRotation._v [0] = 0;
- }
- return false;
- case (osgGA::GUIEventAdapter::RELEASE):
- if (ea.getButton()==1)
- {
- m_bLeftButtonDown = false;
- }
- return false ;
- default:
- return false;
- }
- }
- void CSouth::ChangePosition(osg::Vec3&delta)
- {
- if (m_bPeng)
- {
- osg::Vec3 newPos = m_vPosition + delta;
- osgUtil::IntersectVisitor iv;
- osg::ref_ptr<osg::LineSegment> line = new osg::LineSegment(newPos,m_vPosition);
- osg::ref_ptr<osg::LineSegment> lineZ = new osg::LineSegment(newPos+osg::Vec3(0.0f,0.0f,m_fMoveSpeed),
- newPos-osg::Vec3(0.0f, 0.0f,m_fMoveSpeed));
- iv.addLineSegment(lineZ.get());
- iv.addLineSegment (line.get());
- m_node ->accept(iv);
- if (!iv.hits())
- {
- m_vPosition += delta;
- }
- }
- else
- m_vPosition += delta;
- }
- float CSouth::getSpeed()
- {
- return m_fMoveSpeed;
- }
- void CSouth::setSpeed(float sp)
- {
- m_fMoveSpeed = sp;
- }
- void CSouth::SetPosition(osg::Vec3 &position)
- {
- m_vPosition = position ;
- }
- void CSouth::SetPosition(double* position)
- {
- m_vPosition._v[0] = position[0];
- m_vPosition._v[1] = position[1];
- m_vPosition._v[2] = position[2];
- }
- osg::Vec3 CSouth::GetPosition()
- {
- return m_vPosition;
- }
- void CSouth::setNode(osg::Node*node)
- {
- m_node = node;
- }
- void CSouth::computeHomePosition()
- {
- if(m_node.get())
- {
- const osg::BoundingSphere& boundingSphere=m_node->getBound();
- osg::Vec3 bp = boundingSphere._center; SetPosition(bp);
- }
- }
- void CSouth::setPeng(bool peng)
- {
- m_bPeng = peng;
- }
- bool CSouth::getPeng()
- {
- return m_bPeng;
- }
- void CSouth::setFpeng()
- {
- m_bPeng = !m_bPeng;
- }
- void main()
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
- viewer->setSceneData(osgDB::readNodeFile("ceep.ive"));
- viewer->setCameraManipulator(new CSouth());
- viewer->realize();
- viewer->run();
- }
复制代码 |
|