c++ - 删除框架图时 Qt3D 崩溃

标签 c++ qt qt3d

我正在尝试使用 Qt3D 创建一个应用程序,我可以在其中在同一场景上创建多个 View 窗口。我从 Qt3DWindow 和 Simple C++ Example 的代码开始并开始移动东西。我想到的是每个 View 窗口都会定义自己的帧图(现在只使用一个简单的 QForwardRenderer)和相机,然后我会将每个窗口的帧图添加到场景中的主帧图中。

当我创建多个窗口时,一切似乎都运行良好,但当我关闭窗口并开始删除框架图时,应用程序崩溃了。它在 Qt3DCore 或 Qt3DRender 模块某处的后台线程上崩溃,我无法获取源代码。据我了解,我应该能够在运行时动态修改框架图,但这不是线程安全的吗?您是否希望将一个框架图批量替换为另一个框架图,而不是像我正在做的那样修改事件框架图?

--- 编辑 ---

我做了更多的测试,如果我在从父框架图中删除其框架图后稍微延迟销毁 QWindow(即它试图渲染到的表面),我就不会崩溃。我确实但是在控制台上收到一些警告:

Qt3D.Renderer.Backend: bool __cdecl Qt3DRender::Render::GraphicsContext::makeCurrent(class QSurface *) makeCurrent failed

我猜这是一个线程问题,后端在主线程上销毁后仍在尝试使用 QSurface 进行渲染。我不太喜欢我的解决方案(我只是使用了一个单发计时器来延迟 1 秒销毁窗口),但这总比崩溃好。

RenderWindow.h

#ifndef RENDERWINDOW_H
#define RENDERWINDOW_H
#include <QWindow>
#include <Qt3DCore>
#include <Qt3DRender>
#include <Qt3DInput>
#include <Qt3DExtras/QForwardRenderer>

class RenderWindow : public QWindow
{
public:
  RenderWindow(QScreen* screen = nullptr);
  ~RenderWindow();

  Qt3DRender::QCamera* camera() const;
  Qt3DRender::QFrameGraphNode* frameGraph() const;

protected:
  void resizeEvent(QResizeEvent *) override;

private:
  // Rendering
  Qt3DRender::QFrameGraphNode* mpFrameGraph;
  Qt3DRender::QCamera* mpCamera;

  static bool msFormatDefined;
};

#endif // RENDERWINDOW_H

RenderWindow.cpp

#include "renderwindow.h"
#include <QDebug>

bool RenderWindow::msFormatDefined = false;

namespace
{
    // Different clear colors so that it's obvious each window is using a
    // different camera and frame graph.
    static QColor sClearColors[] = {
        Qt::darkBlue,
        Qt::blue,
        Qt::darkCyan,
        Qt::cyan
    };
    static int sViewCount = 0;
}

RenderWindow::RenderWindow(QScreen* screen)
    : QWindow(screen)
    , mpFrameGraph(nullptr)
    , mpCamera(new Qt3DRender::QCamera)
{
    setSurfaceType(QWindow::OpenGLSurface);

    // Set the default surface format once
    if (!msFormatDefined)
    {
        QSurfaceFormat format;
        format.setVersion(4, 3);
        format.setProfile(QSurfaceFormat::CoreProfile);
        format.setDepthBufferSize(24);
        format.setSamples(4);
        format.setStencilBufferSize(8);
        setFormat(format);

        QSurfaceFormat::setDefaultFormat(format);
        msFormatDefined = true;
    }

    // Camera
    mpCamera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    mpCamera->setPosition(QVector3D(0, 0, 40.0f));
    mpCamera->setViewCenter(QVector3D(0, 0, 0));

    // Frame Graph (using forward renderer for now)
    Qt3DExtras::QForwardRenderer* renderer = new Qt3DExtras::QForwardRenderer;
    renderer->setCamera(mpCamera);
    renderer->setSurface(this);
    renderer->setClearColor(sClearColors[sViewCount++ % 4]);
    mpFrameGraph = renderer;
}

RenderWindow::~RenderWindow()
{
    qDebug() << "start ~RenderWindow";

    // Unparent objects.  Probably not necessary but it makes me feel
    // good inside.
    mpFrameGraph->setParent(static_cast<Qt3DCore::QNode*>(nullptr));
    mpCamera->setParent(static_cast<Qt3DCore::QNode*>(nullptr));

    delete mpFrameGraph;
    delete mpCamera;

    qDebug() << "end ~RenderWindow";
}

Qt3DRender::QCamera* RenderWindow::camera() const
{
    return mpCamera;
}

Qt3DRender::QFrameGraphNode* RenderWindow::frameGraph() const
{
    return mpFrameGraph;
}

void RenderWindow::resizeEvent(QResizeEvent *)
{
    mpCamera->setAspectRatio((float)width()/(float)height());
}

场景.h

#ifndef SCENE_H
#define SCENE_H
#include <Qt3DCore/QEntity>

#include <Qt3DInput/QInputAspect>

#include <Qt3DRender/QFrameGraphNode>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DRender/QRenderSettings>

class RenderWindow;

class Scene
{
public:
    Scene();
    ~Scene();

    Qt3DCore::QEntityPtr rootNode() const;

    void addView(RenderWindow* window);

private:
    void setupScene();

private:
    Qt3DCore::QEntityPtr mpRoot;

    // Frame Graph
    Qt3DRender::QFrameGraphNode* mpFrameGraph;
    Qt3DRender::QRenderSettings* mpRenderSettings;

    // Aspects
    Qt3DCore::QAspectEngine* mpEngine;
    Qt3DRender::QRenderAspect* mpRenderAspect;
    Qt3DInput::QInputAspect* mpInputAspect;
};

#endif // SCENE_H

场景.cpp

#include "scene.h"

#include <QDebug>
#include <QPropertyAnimation>

#include <Qt3DCore/QTransform>

#include <Qt3DRender/QClearBuffers>

#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QTorusMesh>

#include "orbittransformcontroller.h"
#include "RenderWindow.h"

Scene::Scene()
    : mpRoot(nullptr)
    , mpFrameGraph(new Qt3DRender::QFrameGraphNode)
    , mpRenderSettings(new Qt3DRender::QRenderSettings)
    , mpEngine(new Qt3DCore::QAspectEngine)
    , mpRenderAspect(new Qt3DRender::QRenderAspect)
    , mpInputAspect(new Qt3DInput::QInputAspect)
{
    mpEngine->registerAspect(mpRenderAspect);

    mpRenderSettings->setActiveFrameGraph(mpFrameGraph);

    setupScene();

    mpRoot->addComponent(mpRenderSettings);
    mpEngine->setRootEntity(mpRoot);
}

Scene::~Scene()
{
    qDebug() << "start ~Scene";

    mpEngine->setRootEntity(Qt3DCore::QEntityPtr());
    mpRoot.clear();

    delete mpEngine;
    // mpRenderSettings and mpFrameGraph are children of the
    // root node and are automatically destroyed when it is.

    qDebug() << "end ~Scene";
}

Qt3DCore::QEntityPtr Scene::rootNode() const
{
    return mpRoot;
}

void Scene::addView(RenderWindow* window)
{
    // Add the window's frame graph to the main frame graph
    if (window->frameGraph())
    {
        window->frameGraph()->setParent(mpFrameGraph);
    }
}

void Scene::setupScene()
{
    mpRoot.reset(new Qt3DCore::QEntity);

    Qt3DCore::QEntity* entity = new Qt3DCore::QEntity;
    entity->setParent(mpRoot.data());

    // Create the material
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(entity);
    material->setAmbient(Qt::black);
    material->setDiffuse(QColor(196, 196, 32));
    material->setSpecular(Qt::white);

    // Torrus
    Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(entity);
    Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;
    torusMesh->setRadius(5);
    torusMesh->setMinorRadius(1);
    torusMesh->setRings(100);
    torusMesh->setSlices(20);

    Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform;
    torusTransform->setScale3D(QVector3D(1.5, 1, 0.5));
    torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), -45.0f));

    torusEntity->addComponent(torusMesh);
    torusEntity->addComponent(torusTransform);
    torusEntity->addComponent(material);

    // Sphere
    Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(entity);
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
    sphereMesh->setRadius(3);

    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
    /*OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
    controller->setTarget(sphereTransform);
    controller->setRadius(20.0f);

    QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform);
    sphereRotateTransformAnimation->setTargetObject(controller);
    sphereRotateTransformAnimation->setPropertyName("angle");
    sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0));
    sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360));
    sphereRotateTransformAnimation->setDuration(10000);
    sphereRotateTransformAnimation->setLoopCount(-1);
    sphereRotateTransformAnimation->start();*/

    sphereEntity->addComponent(sphereMesh);
    sphereEntity->addComponent(sphereTransform);
    sphereEntity->addComponent(material);
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "scene.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void createWindow();

private:
    Ui::MainWindow *ui;
    Scene* scene;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include <QDebug>
#include "ui_mainwindow.h"
#include "renderwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene(new Scene())
{
    ui->setupUi(this);
    connect(ui->createButton, &QPushButton::clicked, this, &MainWindow::createWindow);
}

MainWindow::~MainWindow()
{
    qDebug() << "~MainWindow";

    delete scene;
    delete ui;
}

void MainWindow::createWindow()
{
    RenderWindow* window = new RenderWindow();
    scene->addView(window);
    window->resize(640, 480);
    window->show();

    QVector3D pos[] = {
        QVector3D(0, 0, 40),
        QVector3D(0, 25, -30),
        QVector3D(-20, -20, -20),
        QVector3D(40, 0, 0)
    };
    static int count = 0;
    window->camera()->setPosition(pos[count++%4]);
    window->camera()->setViewCenter(QVector3D(0, 0, 0));

    // Delete the window when it is closed.
    connect(window, &QWindow::visibilityChanged, this, [=](bool on)
    {
        if (!on)
            window->deleteLater();
    });
}

最佳答案

我已经彻底测试了您的示例并得出了相同的结论。当您过快地销毁窗口时,应用程序会崩溃,这可能是因为 Qt3D 仍试图向底层 QSurface 发出一些 OpenGL 命令。我认为这是一个应该报告的错误。

此问题的“更清洁”解决方法可能是在主窗口中跟踪生成的 3d 窗口。您可以维护一个指针列表,指向所有生成的窗口(并且可能在某个时候被用户关闭)。窗口最终在主窗口的析构函数中被销毁。

关于c++ - 删除框架图时 Qt3D 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49287287/

相关文章:

qt - 如何通过自己读取文件来绘制从 qt3d 中的 .obj(wavefront) 文件加载的网格?

c++ - 如何比较精度为 5 位的 double ?

c++ - 忽略调用 openssl EVP_cleanup 会导致严重缺陷或内存泄漏吗?

qt - 组件的 QML 屏幕坐标

qt - Qmake项目依赖(链接库)

qt - Qt3D 是 Qt5 的一部分吗?

c++ - C++ 中的数组初始化(非字符串)

c++ - W2CA 可能会丢失数据吧?

c++ - 如何将 Open GL 驱动程序中的段错误追溯到我的源代码?

Qt3D "qt.glx: qglx_findConfig: Failed to finding matching FBConfig"警告