OSG系列文章目录
前言
我在windows10下使用visual studio 2022加入osg3.6.5,、osgearth3.2编程,配置了osg和osgearth 好了头文件、lib、bin目录,现在的需求是在地球上添加图标和文字。网上有很多代码也能实现,但是作者往往用的osg和osgEarth版本都很老了,特别是osgEarth还在用2点几的版本,所有基本都是osgEarth::Symbology 和 osgEarth::Annotation 以及它们的成员。在 OSGEarth 3.2 中,Annotation 和 Symbology 模块确实已经被移除或重构,不再以之前的方式提供。这意味着直接包含 <osgEarth/Annotation> 和 <osgEarth/Symbology> 头文件的方法不再适用。
一、osg3.6.5,osgearth3.2版本下的代码
1.头文件
#pragma once
#include <osg/Group>
#include <osgviewer/Viewer>
#include <osgDB/ReadFile>
#include <osgviewer/api/Win32/GraphicsWindowWin32>
#include <osgGA/TrackballManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/Utils>
#include <osgEarth/EarthManipulator>
#include <osgEarth/SkyView>
#include <osgEarth/Units>
//#include <osgEarth/Util/SkyNode> // 确保包含 SkyNode 的定义
#include <osgEarth/Sky>
#include <osgEarth/Ephemeris>
#include <osgEarth/ImageLayer>
#include <osgEarth/AnnotationUtils>
#include <osgText/Text>
#include <osgEarth/GeoTransform>
#include <osgEarth/AnnotationNode>
#include <osgEarth/MapInfo>
#include <osgEarth/Symbol>
#include <osgEarth/GeoMath>
#include <osgEarth/SpatialReference>
#include <osgEarth/TextSymbol>
#include <osgEarth/Feature>
#include <osgEarth/PlaceNode>
using namespace osgEarth;
using namespace osgEarth::Util;
class COSGObject
{
public:
COSGObject(HWND hWnd);
~COSGObject();
void InitOSG();
void InitSceneGraph();
void InitCameraConfig();
void PreFrameUpdate();
void PostFrameUpdate();
static void Render(void* ptr);
void InitOsgEarth();
void setChinaBoundariesOpacity(double opt);
double getChinaBoundariesOpacity();
void rmvChinaBoundaryes();
void addChinaBoundaryes();
//新增地标
void addLabel();
osgViewer::Viewer* GetViewer();
private:
HWND m_hWnd;
osg::ref_ptr<osgViewer::Viewer> m_viewer;
osg::ref_ptr<osg::Group> m_root;
osg::ref_ptr<osgEarth::MapNode> m_mapNode;
osg::ref_ptr<osgEarth::EarthManipulator> m_em;
osg::ref_ptr<osgEarth::SkyNode> m_skyNode;
time_t m_nowTime;
tm* m_tm;
//国界线图层
//osg::ref_ptr<osgEarth::Layer> m_chinaBoundaries;
osg::ref_ptr<osgEarth::ImageLayer> m_chinaBoundaries;
//地标
osg::ref_ptr<osg::Group> m_earthLabel;
};
2.实现文件
#include "pch.h"
#include "COSGObject.h"
#include "DigitalEarth.h"
COSGObject::COSGObject(HWND hWnd)
{
m_hWnd = hWnd;
m_viewer = NULL;
m_root = NULL;
}
COSGObject::~COSGObject()
{
}
void COSGObject::InitOSG()
{
InitSceneGraph();
InitCameraConfig();
InitOsgEarth();
}
void COSGObject::InitSceneGraph()
{
//osg::ref_ptr<osg::Group> rr = new osg::Group;
try {
osgEarth::initialize();
m_root = new osg::Group;
//m_root->addChild(osgDB::readNodeFile("../../vs2022_64bit_3rdParty_osg365_oe32/runtime/test/earthFile/china-simple.earth"));
//osg::ref_ptr<osg::Node> mp = osgDB::readNodeFile("../../vs2022_64bit_3rdParty_osg365_oe32/runtime/test/earth/TestCommon10/output.ive");
osg::ref_ptr<osg::Node> mp = osgDB::readNodeFile("../../vs2022_64bit_3rdParty_osg365_oe32/runtime/test/earthFile/china-simple.earth");
m_root->addChild(mp);
//注意:osgEarth::MapNode只能加载earth文件,加载会ive失败
m_mapNode = dynamic_cast<osgEarth::MapNode*>(mp.get());
m_earthLabel = new osg::Group;
m_root->addChild(m_earthLabel);
}
catch(const std::exception& e) {
AfxMessageBox(CString("Failed to create osg::Group in constructor: ") + CString(e.what()));
throw std::runtime_error(std::string("Failed to create osg::Group in constructor: ") + e.what());
}
}
void