指北针设置

 头文件 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) );		

		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); //加入跟节点

 

osg::MatrixTransform* SELibrary::createCompassPart(const std::string& image, float radius, float height)
{
    osg::Vec3 center(-radius, -radius, height);
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(
        createTexturedQuadGeometry(center, osg::Vec3(radius*2.0f, 0.0f, 0.0f), osg::Vec3(0.0f, radius*2.0f, 0.0f)));

    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    texture->setImage(osgDB::readImageFile(image));

    osg::ref_ptr<osg::MatrixTransform> part = new osg::MatrixTransform;
    part->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture.get());
    part->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
    part->addChild(geode.get());
    return part.release();
}

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值