DYTSrouce/src/scene/OEScene.cpp
2025-03-13 08:42:41 +08:00

131 lines
3.7 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"
#include "effects/ConeWave.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_);
view->setCameraManipulator( new osgEarth::Util::EarthManipulator() );
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::InitEventHandle(osgViewer::View* view) {
// if (nullptr == view) {
// LOG_WARN("view is nullptr");
// return;
// }
//
// //view->GetView()->addEventHandler(new osgEarth::Util::EarthManipulator());
//
// view->addEventHandler(new osgViewer::HelpHandler);
// view->addEventHandler(new osgViewer::StatsHandler);
// }
// void OEScene::DetachView(osgViewer::View* view) {
// if (nullptr != earthRootNode_) {
// std::vector<osg::Group*> parents = earthRootNode_->getParents();
// for (const auto& parent : parents) {
// parent->removeChild(earthRootNode_);
// }
// }
// logarithmicDepthBuffer_->uninstall(view->getCamera());
// view->setSceneData(nullptr);
//
// }
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();
entityRoot_ = new osg::Group;
addChild(entityRoot_);
}