指北针设置

头文件: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 _plateTransform;
    osg::ref_ptr _needleTransform;
    osg::observer_ptr _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) );		
		//自动根据窗口大小设置位置,默认右上角
		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 = 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); //加入跟节点



你可能感兴趣的:(osgearth)