osg系列文章目录
前言
我使用的是osg3.6.5,osgearth3.2版本,使用osgearth使渲染地球,运行效果中间被黑色遮挡,不知道具体原因,请看下图
一、代码
#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 的定义
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();
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;
};
#include "pch.h"
#include "COSGObject.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"));
}
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 COSGObject::InitCameraConfig()
{
RECT rect;
m_viewer = new osgViewer::Viewer;
//osg::ref_ptr<osgViewer::View> viewer = new osgViewer::Viewer;
//osg::ref_ptr<osgViewer::Viewer> m_vv = new osgViewer::Viewer;
::GetWindowRect(m_hWnd, &rect);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
osg