Osg

您所在的位置:网站首页 compass会员 Osg

Osg

2023-12-19 14:25| 来源: 网络整理| 查看: 265

Osg-OsgEarth地图界面显示指北针(指南针)(Qt5.14.2+osgEarht3.6.5+win10)

 

 

相关资料:

https://blog.csdn.net/zbf00138/article/details/52288598

 

实例:

Compass.h

1 #ifndef COMPASS_H 2 #define COMPASS_H 3 4 #include 5 6 #include 7 #include 8 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 #include 20 #include 21 22 #include 23 #include 24 25 #include 26 #include 27 #include 28 29 class Compass : public osg::Camera 30 { 31 public: 32 Compass(); 33 Compass( const Compass& copy, osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY ); 34 META_Node( osg, Compass ); 35 36 37 void setPlate( osg::MatrixTransform* plate ) { _plateTransform = plate; } 38 osg::MatrixTransform* getPlate() { return _plateTransform.get(); } 39 const osg::MatrixTransform* getPlate() const { return _plateTransform.get(); } 40 41 42 void setNeedle( osg::MatrixTransform* needle ) { _needleTransform = needle; } 43 osg::MatrixTransform* getNeedle() { return _needleTransform.get(); } 44 const osg::MatrixTransform* getNeedle() const { return _needleTransform.get(); } 45 46 47 void setMainCamera( osg::Camera* camera ) { _mainCamera = camera; } 48 osg::Camera* getMainCamera() { return _mainCamera.get(); } 49 const osg::Camera* getMainCamera() const { return _mainCamera.get(); } 50 void setWidthHeight(int x, int y, int width, int height){ m_xx = x; m_yy = y; m_width = width; m_height = height; }; 51 virtual void traverse( osg::NodeVisitor& nv ); 52 53 protected: 54 virtual ~Compass(); 55 int m_width, m_height; 56 int m_x, m_y, m_xx, m_yy; 57 osg::ref_ptr _plateTransform; 58 osg::ref_ptr _needleTransform; 59 osg::observer_ptr _mainCamera; 60 }; 61 62 #endif // COMPASS_H View Code

Compass.cpp

1 #include "Compass.h" 2 3 4 Compass::Compass() 5 { 6 } 7 8 Compass::Compass( const Compass& copy, osg::CopyOp copyop ) 9 : osg::Camera(copy, copyop), 10 _plateTransform(copy._plateTransform), 11 _needleTransform(copy._needleTransform), 12 _mainCamera(copy._mainCamera) 13 { 14 } 15 16 Compass::~Compass() 17 { 18 } 19 20 void Compass::traverse( osg::NodeVisitor& nv ) 21 { 22 if ( _mainCamera.valid() && nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR ) 23 { 24 osg::Matrix matrix = _mainCamera->getViewMatrix(); 25 matrix.setTrans( osg::Vec3() ); 26 27 osg::Vec3 northVec = osg::Z_AXIS * matrix; 28 northVec.z() = 0.0f; 29 northVec.normalize(); 30 31 osg::Vec3 axis = osg::Y_AXIS ^ northVec; 32 float angle = atan2(axis.length(), osg::Y_AXIS*northVec); 33 axis.normalize(); 34 35 if ( _plateTransform.valid() ) 36 _plateTransform->setMatrix( osg::Matrix::rotate(angle, axis) ); 37 38 if (m_x != _mainCamera->getViewport()->width() || _mainCamera->getViewport()->height() != m_y) 39 { 40 m_x = _mainCamera->getViewport()->width(); 41 m_y = _mainCamera->getViewport()->height(); 42 this->setViewport(_mainCamera->getViewport()->width()-m_width-m_xx, _mainCamera->getViewport()->height()-m_height-m_yy, m_width, m_height); 43 } 44 } 45 46 47 _plateTransform->accept( nv ); 48 _needleTransform->accept( nv ); 49 osg::Camera::traverse( nv ); 50 } View Code

调用

1 osg::ref_ptr compass = new Compass; 2 compass->setProjectionMatrix(osg::Matrixd::ortho(-1.5, 1.5, -1.5, 1.5, -10.0, 10.0)); 3 compass->setPlate(createCompassPart("d:\\a.png", 1.5f, -1.0f)); //圆盘图片 4 compass->setNeedle(createCompassPart("d:\\b.png", 1.5f, 0.0f));//指针图片 5 compass->setWidthHeight(100,100,100,100); //起始点、宽高 6 compass->setMainCamera(m_pViewer->getCamera()); 7 8 compass->setRenderOrder(osg::Camera::POST_RENDER); 9 compass->setClearMask(GL_DEPTH_BUFFER_BIT); 10 compass->setAllowEventFocus(false); 11 compass->setReferenceFrame(osg::Transform::ABSOLUTE_RF); 12 compass->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); 13 compass->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); 14 15 m_pRoot->addChild(compass); //加入跟节点 View Code createCompassPart函数 1 osg::MatrixTransform *Widget::createCompassPart(const std::string &image, float radius, float height) 2 { 3 osg::Vec3 center(-radius, -radius, height); 4 osg::ref_ptr geode = new osg::Geode; 5 geode->addDrawable( 6 createTexturedQuadGeometry(center, osg::Vec3(radius*2.0f, 0.0f, 0.0f), osg::Vec3(0.0f, radius*2.0f, 0.0f))); 7 8 osg::ref_ptr texture = new osg::Texture2D; 9 texture->setImage(osgDB::readImageFile(image)); 10 11 osg::ref_ptr part = new osg::MatrixTransform; 12 part->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture.get()); 13 part->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 14 part->addChild(geode.get()); 15 return part.release(); 16 } View Code

 

作者:疯狂Delphi 出处:https://www.cnblogs.com/FKdelphi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

欢迎关注我,一起进步!扫描下方二维码即可加我

posted on 2021-10-14 14:38  疯狂delphi  阅读(742)  评论(0)  编辑  收藏  举报



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3