DYTSrouce/src/viewer/OSGRenderer.cpp
2025-01-16 00:13:05 +08:00

115 lines
3.0 KiB
C++

// Copyright (C) 2017 Mike Krus
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "viewer/OSGRenderer.h"
#include "viewer/OsgOpenGLWindow.h"
#include "viewer/osgQOpenGLWidget.h"
//#include <osgQOpenGL/CullVisitorEx>
//#include <osgQOpenGL/GraphicsWindowEx>
#include <QApplication>
#include <QScreen>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <osgViewer/GraphicsWindow>
#include "config.h"
#include "common/SpdLogger.h"
OSGRenderer::OSGRenderer(QObject* parent)
: QObject(parent), osgViewer::Viewer() {
}
OSGRenderer::~OSGRenderer()
{
}
void OSGRenderer::Init(osg::GraphicsContext* gc) {
LOG_INFO("OSGRenderer::Init {}, gc:{}", isInited_, spdlog::fmt_lib::ptr(gc));
if (!gc) {
return;
}
if (isInited_) {
return;
}
getCamera()->setGraphicsContext(gc);
getCamera()->setViewport(new osg::Viewport(0, 0, gc->getTraits()->width, gc->getTraits()->height));
isInited_ = true;
}
void OSGRenderer::Resize(osg::GraphicsContext* gc, int width, int height) {
LOG_INFO("OSGRenderer::Resize {}, gc:{}", isInited_, spdlog::fmt_lib::ptr(gc));
if (!gc) {
return;
}
if (isInited_) {
return;
}
osgViewer::GraphicsWindow* window = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
dyt_check(nullptr != window);
window->getEventQueue()->windowResize(0, 0, width, height);
window->resized(0, 0, width, height);
}
void OSGRenderer::Render() {
frame();
emit RenderFlush();
}
void OSGRenderer::Destoy() {
done();
}
bool OSGRenderer::event(QEvent* event) {
switch (event->type()) {
case INIT: {
RenderBindGraphicsContextEvent* e = reinterpret_cast<RenderBindGraphicsContextEvent*>(event);
Init(e->gc_.get());
return true;
}
case RENDER: {
Render();
return true;
}
case RESIZE: {
GraphicsWindowResizeEvent* e = reinterpret_cast<GraphicsWindowResizeEvent*>(event);
Resize(e->gc_.get(), e->width_, e->height_);
return true;
}
case DESTROY: {
GraphicsWindowResizeEvent* e = reinterpret_cast<GraphicsWindowResizeEvent*>(event);
Resize(e->gc_.get(), e->width_, e->height_);
return true;
}
}
return QObject::event(event);
}