2024-12-26 00:28:25 +00:00
|
|
|
#include "scene/OEScene.h"
|
|
|
|
|
|
|
|
#include <osg/Shape>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/Program>
|
|
|
|
#include <osg/LightSource>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgEarth/GLUtils>
|
|
|
|
#include <osgEarth/EarthManipulator>
|
|
|
|
|
|
|
|
#include <osgShadow/ShadowedScene>
|
|
|
|
#include <osgShadow/ViewDependentShadowMap>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
|
2024-12-29 12:26:57 +00:00
|
|
|
#include "config.h"
|
2024-12-26 00:28:25 +00:00
|
|
|
#include "common/SpdLogger.h"
|
|
|
|
#include "common/RecourceHelper.h"
|
|
|
|
#include "scene/ScopedTimer.h"
|
|
|
|
#include "viewer/OsgView.h"
|
|
|
|
|
2024-12-29 12:26:57 +00:00
|
|
|
const osgEarth::SpatialReference* g_srs_{ nullptr };
|
|
|
|
|
2024-12-26 00:28:25 +00:00
|
|
|
|
|
|
|
OEScene::OEScene() {
|
|
|
|
osgDB::FilePathList& pathList = osgDB::Registry::instance()->getDataFilePathList();
|
|
|
|
const std::string& basePath = RecourceHelper::Get().GetBasePath().toStdString();
|
|
|
|
pathList.push_back(basePath + "/resources/earth/");
|
2024-12-27 16:51:30 +00:00
|
|
|
pathList.push_back(basePath + "/resources/textures/");
|
2024-12-26 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OEScene::InitEventHandle(class OsgView* view) {
|
|
|
|
if (nullptr == view) {
|
|
|
|
LOG_WARN("view is nullptr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-29 02:23:08 +00:00
|
|
|
//view->GetView()->addEventHandler(new osgEarth::Util::EarthManipulator());
|
2024-12-26 00:28:25 +00:00
|
|
|
|
|
|
|
view->GetView()->addEventHandler(new osgViewer::HelpHandler);
|
2025-01-02 00:14:56 +00:00
|
|
|
view->GetView()->addEventHandler(new osgViewer::StatsHandler);
|
2024-12-26 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OEScene::AttachView(OsgView* view) {
|
|
|
|
if (nullptr == view) {
|
|
|
|
LOG_WARN("view is nullptr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-27 16:51:30 +00:00
|
|
|
earthRootNode_ = osgDB::readNodeFile("triton.earth");
|
|
|
|
if (!earthRootNode_) {
|
|
|
|
LOG_ERROR("read earth node(triton.earth) failed");
|
|
|
|
return;
|
|
|
|
}
|
2024-12-29 02:23:08 +00:00
|
|
|
logarithmicDepthBuffer_ = std::make_unique<osgEarth::Util::LogarithmicDepthBuffer>();
|
2024-12-27 16:51:30 +00:00
|
|
|
|
|
|
|
earthMapNode_ = osgEarth::MapNode::get(earthRootNode_);
|
|
|
|
LOG_INFO("earth map node get success: {}", earthMapNode_.valid());
|
|
|
|
|
|
|
|
entityRoot_ = new osg::Group;
|
|
|
|
if (earthMapNode_) {
|
|
|
|
earthMapNode_->addChild(entityRoot_);
|
2024-12-29 12:26:57 +00:00
|
|
|
g_srs_ = earthMapNode_->getMapSRS();
|
|
|
|
dyt_check(nullptr != g_srs_);
|
2024-12-27 16:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
skyDome_ = osgEarth::SkyNode::create();
|
|
|
|
if (!earthMapNode_) {
|
|
|
|
LOG_WARN("eart map node is nullptr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
skyDome_->addChild(earthMapNode_);
|
|
|
|
|
|
|
|
osg::Node* node = view->GetView()->getSceneData();
|
|
|
|
if (nullptr == node) {
|
|
|
|
LOG_INFO("view scene data is nullptr, root valid:{}", skyDome_.valid());
|
2024-12-29 02:23:08 +00:00
|
|
|
view->GetView()->setSceneData(skyDome_);
|
2024-12-27 16:51:30 +00:00
|
|
|
} else {
|
|
|
|
osg::Group* group = node->asGroup();
|
|
|
|
if (nullptr != group) {
|
|
|
|
LOG_INFO("node is group");
|
|
|
|
group->addChild(skyDome_);
|
|
|
|
} else {
|
|
|
|
LOG_INFO("node is not group");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-29 02:23:08 +00:00
|
|
|
skyDome_->attach(view->GetView());
|
2024-12-27 16:51:30 +00:00
|
|
|
|
|
|
|
skyDome_->setAtmosphereVisible(true);
|
|
|
|
skyDome_->setSunVisible(true);
|
|
|
|
skyDome_->setMoonVisible(true);
|
|
|
|
skyDome_->setStarsVisible(true);
|
|
|
|
|
2024-12-29 02:23:08 +00:00
|
|
|
skyDome_->setDateTime(osgEarth::DateTime(2024, 12, 24, 3));
|
|
|
|
skyDome_->setSimulationTimeTracksDateTime(true);
|
2024-12-27 16:51:30 +00:00
|
|
|
|
|
|
|
logarithmicDepthBuffer_->install(view->GetView()->getCamera());
|
|
|
|
|
2024-12-26 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OEScene::DetachView(OsgView* view) {
|
|
|
|
if (nullptr != earthRootNode_) {
|
|
|
|
std::vector<osg::Group*> parents = earthRootNode_->getParents();
|
|
|
|
for (const auto& parent : parents) {
|
|
|
|
parent->removeChild(earthRootNode_);
|
|
|
|
}
|
|
|
|
}
|
2024-12-27 16:51:30 +00:00
|
|
|
logarithmicDepthBuffer_->uninstall(view->GetView()->getCamera());
|
2024-12-26 00:28:25 +00:00
|
|
|
view->GetView()->setSceneData(nullptr);
|
2024-12-27 16:51:30 +00:00
|
|
|
|
2024-12-26 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define USE_CUSTOM_SHADER
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::TextureCubeMap> OEScene::LoadCubeMapTextures(const std::string& dir) {
|
|
|
|
enum {
|
|
|
|
POS_X, NEG_X, POS_Y, NEG_Y, POS_Z, NEG_Z
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string filenames[6];
|
|
|
|
|
|
|
|
std::string basePath = RecourceHelper::Get().GetBasePath().toStdString();
|
|
|
|
|
|
|
|
filenames[POS_X] = basePath + "/resources/textures/" + dir + "/east.png";
|
|
|
|
filenames[NEG_X] = basePath + "/resources/textures/" + dir + "/west.png";
|
|
|
|
filenames[POS_Z] = basePath + "/resources/textures/" + dir + "/north.png";
|
|
|
|
filenames[NEG_Z] = basePath + "/resources/textures/" + dir + "/south.png";
|
|
|
|
filenames[POS_Y] = basePath + "/resources/textures/" + dir + "/down.png";
|
|
|
|
filenames[NEG_Y] = basePath + "/resources/textures/" + dir + "/up.png";
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::TextureCubeMap> cubeMap = new osg::TextureCubeMap;
|
|
|
|
cubeMap->setInternalFormat(GL_RGBA);
|
|
|
|
|
|
|
|
cubeMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
|
|
|
|
cubeMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
|
|
|
|
cubeMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
cubeMap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X, osgDB::readImageFile(filenames[NEG_X]));
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_X, osgDB::readImageFile(filenames[POS_X]));
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Y, osgDB::readImageFile(filenames[NEG_Y]));
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Y, osgDB::readImageFile(filenames[POS_Y]));
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z, osgDB::readImageFile(filenames[NEG_Z]));
|
|
|
|
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z, osgDB::readImageFile(filenames[POS_Z]));
|
|
|
|
|
|
|
|
return cubeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Group* OEScene::GetData() {
|
|
|
|
return earthMapNode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OEScene::AddToScene(osg::Node* node) {
|
|
|
|
assert(nullptr != entityRoot_);
|
|
|
|
entityRoot_->addChild(node);
|
|
|
|
/* osgOcean::OceanScene* oceanScene = GetOceanScene();
|
|
|
|
if (nullptr == oceanScene) {
|
|
|
|
LOG_WARN("oceanScene is nullptr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setNodeMask(oceanScene->getNormalSceneMask() |
|
|
|
|
oceanScene->getReflectedSceneMask() |
|
|
|
|
oceanScene->getRefractedSceneMask() |
|
|
|
|
CAST_SHADOW | RECEIVE_SHADOW);
|
|
|
|
oceanScene->addChild(node);*/
|
|
|
|
/* osg::Group* root = GetScene();
|
|
|
|
root->addChild(node);*/
|
|
|
|
}
|