DYTSrouce/src/viewer/OsgView.cpp

91 lines
2.4 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "viewer/OsgView.h"
#include <osgGA/TrackballManipulator>
#include <osg/PositionAttitudeTransform>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osg/ShapeDrawable>
#include <osg/Group>
#include <osgManipulator/Dragger>
#include <osgManipulator/TranslateAxisDragger>
#include <osgViewer/GraphicsWindow>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgDB/ReadFile>
#include <osg/LightModel>
2025-01-12 17:35:51 +00:00
#include "config.h"
2025-01-04 04:12:51 +00:00
#include "common/SpdLogger.h"
#include "viewer/KeyMapQtOsg.h"
#include "viewer/OsgCameraManipulator.h"
2025-01-12 17:35:51 +00:00
#include "viewer/OsgViewUI.h"
2025-01-04 04:12:51 +00:00
OsgView::OsgView(QObject* parent) noexcept
: QObject(parent) {
LOG_INFO("actor, self={}", fmt::ptr(this));
}
OsgView::~OsgView() {
LOG_INFO("dctor, self={}", fmt::ptr(this));
}
void OsgView::InitView(osgViewer::View* pView) {
2025-03-13 00:42:41 +00:00
if (nullptr == pView) {
2025-01-04 04:12:51 +00:00
return;
}
2025-03-13 00:42:41 +00:00
2025-01-04 04:12:51 +00:00
view_ = pView;
}
osgViewer::View* OsgView::GetView() const {
return view_.get();
}
void OsgView::Initialize(OsgCameraManipulator* cameraManipulator) {
if (initialized_) {
return;
}
if (nullptr != cameraManipulator) {
cameraManipulator->Initialize();
SetCameraManipulator(cameraManipulator);
}
2025-01-12 17:35:51 +00:00
if (!viewUI_.valid()) {
const osg::Viewport* viewport = view_->getCamera()->getViewport();
osg::Viewport::value_type width = viewport->width();
osg::Viewport::value_type height = viewport->height();
viewUI_ = new OsgViewUI(view_, width, height);
osg::Group* root = view_->getSceneData()->asGroup();
dyt_check(nullptr != root);
root->addChild(viewUI_.get());
}
view_->addEventHandler(new osgViewer::StatsHandler);
view_->addEventHandler(new osgGA::StateSetManipulator(view_->getCamera()->getOrCreateStateSet()));
2025-01-04 04:12:51 +00:00
initialized_ = true;
}
void OsgView::Uninitialize(void) {
if (!initialized_) {
return;
}
initialized_ = false;
}
bool OsgView::SetCameraManipulator(OsgCameraManipulator* cameraManipulator) {
assert(view_.valid());
if (cameraManipulator_ == cameraManipulator) {
return false;
}
osgGA::CameraManipulator* cameraMainp = cameraManipulator->GetManipulator();
if (nullptr == cameraMainp) {
return false;
}
cameraManipulator_ = cameraManipulator;
view_->setCameraManipulator(cameraMainp);
return true;
}