osgEarth指北针(使用原生界面canvas实现,更轻量级)

一、前言

之前版本使用相机后期处理实现,参见相机后期处理实现指北针,当开启对数深度缓存后,指北针就显示不正常了,本人感觉这种实现有点太繁琐、太耗性能了,后续维护起来也不方便,故采用osg原生图形界面的方式实现。

二、效果

以下图片可自行换成自己的

三、实现

VDCompassUseCanvas.h

cpp 复制代码
/***********************************************************************
 * 功能:指北针(使用界面canvas实现)
 * 作者:xxx
 * 历史:2026年2月12日09:55:35新建。
 ***********************************************************************/
#pragma once
#include <osgEarth/Controls>
#include <osgViewer/Viewer>

class VDCompassUseCanvas :public osgEarth::Util::Controls::ControlCanvas
{
public:
	VDCompassUseCanvas(osgViewer::Viewer *view);
	~VDCompassUseCanvas();

	void SetHeading(const double &heading);

private:

	osg::ref_ptr<osgEarth::Util::Controls::ImageControl> m_pImageControl = nullptr;
};

VDCompassUseCanvas.cpp

cpp 复制代码
#include "VDCompassUseCanvas.h"
#include <osgDB/ReadFile>
#include <QCoreApplication>
VDCompassUseCanvas::VDCompassUseCanvas(osgViewer::Viewer *view)
{
	if (!view) {
		OE_FATAL << "create compass error,viewer is invalid." << std::endl;
	}
	else {
		getOrCreate(view);
		//绘制背景
		osg::ref_ptr<osg::Image> imageBG = osgDB::readImageFile((QCoreApplication::applicationDirPath() + QString("\\data\\image\\compass-b4.png")).toStdString());
		if (imageBG.valid())
		{
			osg::ref_ptr<osgEarth::Util::Controls::ImageControl> imageControl = new osgEarth::Util::Controls::ImageControl(imageBG.get());
			imageControl->setHorizAlign(osgEarth::Util::Controls::Control::ALIGN_RIGHT);
			imageControl->setFixSizeForRotation(true);
			imageControl->setSize(100, 100);
			addControl(imageControl.get());
		}
		//绘制指针
		osg::ref_ptr<osg::Image> image = osgDB::readImageFile((QCoreApplication::applicationDirPath() + QString("\\data\\image\\compass-p2.png")).toStdString());
		if (image.valid())
		{
			m_pImageControl = new osgEarth::Util::Controls::ImageControl(image.get());
			m_pImageControl->setHorizAlign(osgEarth::Util::Controls::Control::ALIGN_RIGHT);
			m_pImageControl->setFixSizeForRotation(true);
			m_pImageControl->setSize(100, 100);
			addControl(m_pImageControl.get());
		}
	}
}

VDCompassUseCanvas::~VDCompassUseCanvas()
{
	OE_NOTICE << "~VDCompassUseCanvas" << std::endl;
}

void VDCompassUseCanvas::SetHeading(const double &heading)
{
	if (m_pImageControl.valid())
		m_pImageControl->setRotation(osgEarth::Angular((heading), osgEarth::Units::DEGREES));
}

原创不易,记得点赞加关注哦,我会持续分享实用的功能(:-

相关推荐
一梦、んんん3 天前
Osgearth后期处理+多窗口显示
osgearth·fbo
妙为2 个月前
osgEarth中文显示乱码
中文乱码·osgearth·osg
WALL-EC6 个月前
Qt工具栏中图标槽函数没有响应的问题分析
开发语言·qt·osgearth
幽迷狂6 个月前
AFSIM入门教程03.03:更新所有依赖库版本
c++·qt·仿真·osgearth·osg·军事·afsim
星火撩猿10 个月前
如何配置osg编译使支持png图标加载显示
osgearth·osg·地图图标添加
slntJy10 个月前
ubantu&windows搭建gis开发环境(qt+osg+osgearth+osgqt)
gis·win11·osgearth·ubantu·osg·osgqt
读万卷书不如行万里路呀1 年前
实时飞行粒子尾迹(十二)
osgearth·osg·实时飞行粒子尾迹
杨石兴2 年前
lesson01-在osgEarth上实时的贴地显示无人机或卫星传回的图片
osgearth·osg
aspiretop2 年前
ubuntu上使用osg3.2+osgearth2.9
ubuntu·osgearth