正文

我之前在这篇博文《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_AVAILABLE
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#else
format.setVersion(2, 0);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#endif
format.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 manipulator
widget.getOsgViewer()->addEventHandler(new osgGA::StateSetManipulator(
widget.getOsgViewer()->getCamera()->getOrCreateStateSet())); // add the thread model handler
widget.getOsgViewer()->addEventHandler(new osgViewer::ThreadingHandler); // add the window size toggle handler
widget.getOsgViewer()->addEventHandler(new osgViewer::WindowSizeHandler); // add the stats handler
widget.getOsgViewer()->addEventHandler(new osgViewer::StatsHandler); // add the record camera path handler
widget.getOsgViewer()->addEventHandler(
new osgViewer::RecordCameraPathHandler); // add the LOD Scale handler
widget.getOsgViewer()->addEventHandler(new osgViewer::LODScaleHandler); // add the screen capture handler
widget.getOsgViewer()->addEventHandler(new osgViewer::ScreenCaptureHandler); // load the data
std::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内部计算的帧数不同似乎也正常。不过我这里是不太敢用这个解决方案了,目前还是使用之前的解决方案,以后有机会还是自己研究一下其中的实现。

参考

  1. OpenSceneGraph + QOpenGLWidget - minimal example
  2. OSG 使用Qt的QOpenGLWidget

OSG嵌入QT的简明总结2的更多相关文章

  1. OSG嵌入QT的简明总结

    目录 1.解决方案 2.存在问题 1) 警告提示 2) 多线程问题 3) 其他 1.解决方案 不得不说关于OSG的资料实在太零散了,搜索了很多关于OSG在QT下的解决方案,都是各有各的说法,有的说的不 ...

  2. OSG嵌入QT(QT界面使用Qt Designer编辑)

    本文主要内容:使用Qt Designer编辑好QT界面后,将OSG中的ViewerWidget嵌入到QT的Widget中. 在VS中嵌入QT工具,建立QT GUIApplication后,打开自动生成 ...

  3. [原][osg][QT]osg与QT界面结合的简单例子二

    //main.cpp #include "VREObliqueEditorQTWindow.h" #include <QtWidgets/QApplication> # ...

  4. 基于osgQt将OSG嵌入到Qt窗口中(有错误)

    1, 编译OSG 由于重装了win10的系统,Qt也安装了最新版5.13,把之前OSG重新编译了一遍,过程与之前的一模一样. Windows7 + OSG3.6 + VS2017 + Qt5.11 2 ...

  5. OpenGL学习笔记5——嵌入Qt框架

    学习OpenGL也有段时间了,前几篇将GL最基本的画图过程解析了一下,后面进阶的就随项目需要再学.因为之前一直是用glut这个实用工具包来开发很方便,但是会附带一个控制台的窗口,实在觉得有些low,因 ...

  6. [转][osg][QT]osg与QT界面结合的简单例子

    //QT += core gui opengl //LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt ...

  7. 2018.8.23几日重新编译OSG+OE+Qt遇到的问题

    Qt安装多个版本的时候,注意屏蔽掉不使用的Qt,例如OE中的CMakeLists.txt中的# FIND_PACKAGE(Qt4) 使用以前编译好的libcurl.dll现在出现"无法定位序 ...

  8. 将Unreal4打包后的工程嵌入到Qt或者桌面中

    #include "widget.h" #include "ui_widget.h" #include "windows.h" #inclu ...

  9. 对《将Unreal4打包后的工程嵌入到Qt或者桌面中》一文的补充

    在上一文中本人尝试将Ue4嵌入到Qt中,但依然有一些问题没有去尝试解决.今天因为帮助知乎专栏作者@大钊的关系,顺便进行补完. 2018.7.18更新: 正好在参加杭州UnrealCircle的时候见到 ...

  10. 单独编译osgQt模块 Qt moc

    从alphapixel网站下载了OSG3.0.1VS2010x64版本的库,但是里面不包括osgQt模块,于是得自己编译 *************osgQtx64.zip工程文件可以去本博客园的“文 ...

随机推荐

  1. SharedPreferences-PreferenceUtils

    SharedPreferences   easy use import android.content.Context; import android.content.SharedPreference ...

  2. 从内核世界透视 mmap 内存映射的本质(源码实现篇)

    本文基于内核 5.4 版本源码讨论 通过上篇文章 <从内核世界透视 mmap 内存映射的本质(原理篇)>的介绍,我们现在已经非常清楚了 mmap 背后的映射原理以及它的使用方法,其核心就是 ...

  3. 【matplotlib 实战】--南丁格尔玫瑰图

    南丁格尔玫瑰图是一种用极坐标下的柱状图或堆叠柱状图来展示数据的图表. 虽然南丁格尔玫瑰图外观类似饼图,但是表示数据的方式不同,它是以半径来表示数值的,而饼图是以扇形的弧度来表达数据的. 所以,南丁格尔 ...

  4. 入手react的 第一坑

    npm verb cli /usr/local/bin/node /usr/local/bin/npm npm info using npm@9.8.1 npm info using node@v18 ...

  5. Nacos源码阅读心得

    Nacos注册中心(1.4.1)源码解读心得 一丶Nacos介绍 Nacos是阿里巴巴推出的一款新开源项目,是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.它致力于帮助您发现.配置和 ...

  6. zabbix监控Tomcat/JVM 实例性能

    1.背景 zabbix-4.0 环境已部署好 JDK .Tomcat环境已部署好 2.配置Tomcat JMX 编辑catalina.sh加入以下配置 # vim /usr/local/tomcat/ ...

  7. ELK-WEB中文版化-redis高性能加速

    1.ELK-WEB中文汉化版支持:(kibana所在机器执行)Kibana WEB平台所有的字段均显示为英文,目前5.x版本默认没有中文汉化版插件或者汉化包(7.x版本支持汉化默认有汉化),感谢Git ...

  8. CF B. Gardener and the Array

    B. Gardener and the Array 思路:只要找到一个c他的每一位均在除了它的集合中出现过即可 这题T了2发,用来multiset,注意multiset大的时间复杂度是O(K + lo ...

  9. C# ConfigMan.cs

    public static class ConfigMan { public static string ReadKey(string key) { return ConfigurationManag ...

  10. 面试题:MySQL事务的ACID如何实现?

    大家好,我是[码老思],事务是一个数据库绕不开的话题,今天和大家一起聊聊. 事务是什么? 事务(Transaction)是并发控制的基本单位.所谓的事务呢,它是一个操作序列,这些操作要么都执行,要么都 ...