指北针设置

来源:互联网 发布:mac ssh客户端工具 编辑:程序博客网 时间:2024/05/19 17:10
头文件:compass.h
class Compass : public osg::Camera{public:    Compass();    Compass( const Compass& copy, osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY );    META_Node( osg, Compass );           void setPlate( osg::MatrixTransform* plate ) { _plateTransform = plate; }    osg::MatrixTransform* getPlate() { return _plateTransform.get(); }    const osg::MatrixTransform* getPlate() const { return _plateTransform.get(); }           void setNeedle( osg::MatrixTransform* needle ) { _needleTransform = needle; }    osg::MatrixTransform* getNeedle() { return _needleTransform.get(); }    const osg::MatrixTransform* getNeedle() const { return _needleTransform.get(); }            void setMainCamera( osg::Camera* camera ) { _mainCamera = camera; }    osg::Camera* getMainCamera() { return _mainCamera.get(); }    const osg::Camera* getMainCamera() const { return _mainCamera.get(); }void setWidthHeight(int x, int y, int width, int height){ m_xx = x; m_yy = y; m_width = width; m_height = height; };    virtual void traverse( osg::NodeVisitor& nv );    protected:    virtual ~Compass();int m_width, m_height;int m_x, m_y, m_xx, m_yy;    osg::ref_ptr<osg::MatrixTransform> _plateTransform;    osg::ref_ptr<osg::MatrixTransform> _needleTransform;    osg::observer_ptr<osg::Camera> _mainCamera;};


源文件:compass.cpp

Compass::Compass(){}Compass::Compass( const Compass& copy, osg::CopyOp copyop ):   osg::Camera(copy, copyop),    _plateTransform(copy._plateTransform),    _needleTransform(copy._needleTransform),    _mainCamera(copy._mainCamera){}Compass::~Compass(){}void Compass::traverse( osg::NodeVisitor& nv ){    if ( _mainCamera.valid() && nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR )    {        osg::Matrix matrix = _mainCamera->getViewMatrix();        matrix.setTrans( osg::Vec3() );                osg::Vec3 northVec = osg::Z_AXIS * matrix;        northVec.z() = 0.0f;        northVec.normalize();                osg::Vec3 axis = osg::Y_AXIS ^ northVec;        float angle = atan2(axis.length(), osg::Y_AXIS*northVec);        axis.normalize();                if ( _plateTransform.valid() )            _plateTransform->setMatrix( osg::Matrix::rotate(angle, axis) );
<span style="white-space:pre"></span>//自动根据窗口大小设置位置,默认右上角if (m_x != _mainCamera->getViewport()->width() || _mainCamera->getViewport()->height() != m_y){m_x = _mainCamera->getViewport()->width();m_y = _mainCamera->getViewport()->height();this->setViewport(_mainCamera->getViewport()->width()-m_width-m_xx, _mainCamera->getViewport()->height()-m_height-m_yy, m_width, m_height);}}        _plateTransform->accept( nv );    _needleTransform->accept( nv );    osg::Camera::traverse( nv );}

调用方法:

osg::ref_ptr<Compass> compass = new Compass;compass->setProjectionMatrix(osg::Matrixd::ortho(-1.5, 1.5, -1.5, 1.5, -10.0, 10.0));compass->setPlate(SELibrary::getInstance()->createCompassPart(m_strRootPath + "\\images\\compass_plate.png", 1.5f, -1.0f)); //圆盘图片compass->setNeedle(SELibrary::getInstance()->createCompassPart(m_strRootPath + "\\images\\compass_needle.png", 1.5f, 0.0f));//指针图片compass->setWidthHeight(x,y,width,height); //起始点、宽高compass->setMainCamera(m_pEarthViewer->getCamera());compass->setRenderOrder(osg::Camera::POST_RENDER);compass->setClearMask(GL_DEPTH_BUFFER_BIT);compass->setAllowEventFocus(false);compass->setReferenceFrame(osg::Transform::ABSOLUTE_RF);compass->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);compass->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);root->addChild(compass); //加入跟节点



0 0
原创粉丝点击