正文
我之前在这篇博文《OSG嵌入QT的简明总结》中论述了OSG在QT中显示的可视化问题。其中提到官方提供的osgQt项目(地址:https://github.com/openscenegraph/osgQt )很久前已经更新了。但是我一直没有时间同步更新,最近重新尝试了一下,还是有一些问题。
原先的osgQt版本是兼容Qt4的QGLWidget,这个类Qt官方准备废弃了,现在使用的OpenGL支持组件是QOpenGLWidget,新的osgQt项目就是基于这个类来进行扩展的。在项目中提供了一个例子osgviewerQt,我稍微试用了一下,将其修改成自己的代码时发现了问题,就是渲染的场景宽高比不正确,尤其是将窗体设置成很长或者很窄的时候。我还特意在这个项目中提交了issue:I tried the demo in the project, and the correct aspect ratio cannot be displayed。
后续也有人回答了这个问题,一个解决方案就是需要在初始化响应函数中设置相机投影矩阵的宽高比。我改写的例子如下:
#include <QApplication>
#include <QSurfaceFormat>
#include <iostream>
#include <osgDB/ReadFile>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>int main(int argc, char* argv[]) {QSurfaceFormat format = QSurfaceFormat::defaultFormat();#ifdef OSG_GL3_AVAILABLEformat.setVersion(3, 2);format.setProfile(QSurfaceFormat::CoreProfile);format.setRenderableType(QSurfaceFormat::OpenGL);format.setOption(QSurfaceFormat::DebugContext);
#elseformat.setVersion(2, 0);format.setProfile(QSurfaceFormat::CompatibilityProfile);format.setRenderableType(QSurfaceFormat::OpenGL);format.setOption(QSurfaceFormat::DebugContext);
#endifformat.setDepthBufferSize(24);// format.setAlphaBufferSize(8);format.setSamples(8);format.setStencilBufferSize(8);format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);QSurfaceFormat::setDefaultFormat(format);QApplication app(argc, argv);osgQOpenGLWidget widget;QObject::connect(&widget, &osgQOpenGLWidget::initialized, [&widget] {// set up the camera manipulators.widget.getOsgViewer()->setCameraManipulator(new osgGA::TrackballManipulator());// add the state manipulatorwidget.getOsgViewer()->addEventHandler(new osgGA::StateSetManipulator(widget.getOsgViewer()->getCamera()->getOrCreateStateSet()));// add the thread model handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::ThreadingHandler);// add the window size toggle handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::WindowSizeHandler);// add the stats handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::StatsHandler);// add the record camera path handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::RecordCameraPathHandler);// add the LOD Scale handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::LODScaleHandler);// add the screen capture handlerwidget.getOsgViewer()->addEventHandler(new osgViewer::ScreenCaptureHandler);// load the datastd::string filename = "C:/Data/001/010137001.obj";osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(filename);// optimize the scene graph, remove redundant nodes and state etc.osgUtil::Optimizer optimizer;optimizer.optimize(loadedModel);widget.getOsgViewer()->setSceneData(loadedModel);//增加宽高比设置QSize size = widget.size(); float aspectRatio =static_cast<float>(size.width()) / static_cast<float>(size.height());widget.getOsgViewer()->getCamera()->setProjectionMatrixAsPerspective(60.f, aspectRatio, 1.f, 1000.f);return 0;});widget.resize(200, 600);widget.show();return app.exec();
}
上述例子确实可以让场景显示正常,即使窗体宽设置为200,高设置为600。不过我发现了另外一个问题,按S显示帧数的时候帧数比之前的解决方案低很多。之前的解决方案帧数可以达到200帧,但是这个解决方案帧数大概在90帧左右。
具体看了一下其封装的osgQOpenGLWidget的实现,我觉得可能有两个原因,第一个是渲染的帧函数中有同步锁,不知道会不会有所影响。第二个是这个解决方案获取的帧数好像是自己计算的,与OSG内部计算的帧数不同似乎也正常。不过我这里是不太敢用这个解决方案了,目前还是使用之前的解决方案,以后有机会还是自己研究一下其中的实现。
参考
- OpenSceneGraph + QOpenGLWidget - minimal example
- OSG 使用Qt的QOpenGLWidget