DYTSrouce/src/scene/OEScene.cpp
2025-06-22 16:27:44 +08:00

120 lines
3.4 KiB
C++

#include "scene/OEScene.h"
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osg/Program>
#include <osg/LightSource>
#include <osgDB/ReadFile>
#include <osgEarthUtil/EarthManipulator>
#include <osgGA/StateSetManipulator>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ViewDependentShadowMap>
#include <osgViewer/ViewerEventHandlers>
#include "config.h"
#include "common/SpdLogger.h"
#include "common/RecourceHelper.h"
#include "scene/ScopedTimer.h"
#include "viewer/OsgView.h"
#include "viewer/OsgCameraManipulator.h"
#include "scene/ScaleBarHandler.h"
const osgEarth::SpatialReference* g_srs_{ nullptr };
OEScene::OEScene() {
osgDB::FilePathList& pathList = osgDB::Registry::instance()->getDataFilePathList();
const std::string& basePath = RecourceHelper::Get().GetBasePath().toStdString();
pathList.push_back(basePath + "/resources/earth/");
pathList.push_back(basePath + "/resources/textures/");
Init();
}
void OEScene::AttachView(osgViewer::View* view) {
dyt_check(nullptr != earthRootNode_);
skyDome_ = osgEarth::Util::SkyNode::create(earthMapNode_);
if (!earthMapNode_) {
LOG_WARN("eart map node is nullptr");
return;
}
dyt_check(nullptr != earthManipulator_);
view->setCameraManipulator(earthManipulator_);
skyDome_->attach(view);
skyDome_->getSunLight()->setAmbient(osg::Vec4(0.5,0.5,0.5,1.0));
addChild(skyDome_);
skyDome_->setDateTime(osgEarth::DateTime(2024, 12, 24, 3));
view->setSceneData(this);
curentView_ = view;
}
void OEScene::DetachView(osgViewer::View* view) {
view->setSceneData(nullptr);
}
bool OEScene::AddToScene(osg::Node* node) const {
dyt_check(nullptr != entityRoot_);
return entityRoot_->addChild(node);
}
void OEScene::SetHomeViewpoint(const osgEarth::Viewpoint& viewpoint, double duration_s) {
dyt_check(nullptr != earthManipulator_);
earthManipulator_->setViewpoint(viewpoint, duration_s);
//earthManipulator_->setHomeViewpoint(viewpoint, duration_s);
}
bool OEScene::TrackEntity(osg::Node* entity, bool track) const {
dyt_check(nullptr != earthManipulator_);
if (track) {
earthManipulator_->setTetherNode(entity, 5.0);
} else {
earthManipulator_->clearViewpoint();
}
return true;
}
OESceneUI* OEScene::GetOrCreateSceneUI() {
if (sceneUI_) {
LOG_INFO("scene ui is already attached");
return sceneUI_.get();
}
sceneUI_ = new OESceneUI(this);
return sceneUI_.get();
}
osg::Viewport* OEScene::GetViewport() const {
dyt_check(nullptr != curentView_);
return curentView_->getCamera()->getViewport();
}
const osgEarth::SpatialReference* OEScene::GetSrs() {
dyt_check(nullptr != g_srs_);
return g_srs_;
}
void OEScene::Init() {
std::string earthPath = "D:/Project/DYT/Tool/TritonSample/TritonSample/triton.earth";
// earthRootNode_ = osgDB::readNodeFile("triton.earth");
earthRootNode_ = osgDB::readNodeFile(earthPath);
dyt_check(nullptr != earthRootNode_);
addChild(earthRootNode_);
earthMapNode_ = osgEarth::MapNode::get(earthRootNode_);
dyt_check(nullptr != earthMapNode_);
LOG_INFO("earth map node get success: {}", earthMapNode_.valid());
g_srs_ = earthMapNode_->getMapSRS();
earthManipulator_ = new osgEarth::Util::EarthManipulator();
earthManipulator_->getSettings()->setMinMaxPitch(-90.0, -10.0);
entityRoot_ = new osg::Group;
addChild(entityRoot_);
}