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 <osgEarth/MapNode> 5 6 #include <osgEarthUtil/EarthManipulator> 7 #include <osgEarthUtil/ExampleResources> 8 9 #include <osgEarthAnnotation/ImageOverlay> 10 #include <osgEarthAnnotation/CircleNode> 11 #include <osgEarthAnnotation/RectangleNode> 12 #include <osgEarthAnnotation/EllipseNode> 13 #include <osgEarthAnnotation/PlaceNode> 14 #include <osgEarthAnnotation/LabelNode> 15 #include <osgEarthAnnotation/LocalGeometryNode> 16 #include <osgEarthAnnotation/FeatureNode> 17 #include <osgEarthAnnotation/ModelNode> 18 19 #include <osgEarthAnnotation/AnnotationEditing> 20 #include <osgEarthAnnotation/ImageOverlayEditor> 21 22 #include <osgEarthSymbology/GeometryFactory> 23 #include <osgEarthUtil/RTTPicker> 24 25 #include <osgViewer/Viewer> 26 #include <osgQOpenGL/osgQOpenGLWidget> 27 #include <osg/DrawPixels> 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<osg::MatrixTransform> _plateTransform; 58 osg::ref_ptr<osg::MatrixTransform> _needleTransform; 59 osg::observer_ptr<osg::Camera> _mainCamera; 60 }; 61 62 #endif // COMPASS_H
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 }
调用
1 osg::ref_ptr<Compass> 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); //加入跟节点
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<osg::Geode> 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<osg::Texture2D> texture = new osg::Texture2D; 9 texture->setImage(osgDB::readImageFile(image)); 10 11 osg::ref_ptr<osg::MatrixTransform> 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 }
请发表评论