Qt3D删除帧图时崩溃

时间:2018-03-14 20:42:36

标签: c++ qt qt3d

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

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

---编辑---

我做了一些测试,如果我从父框架图中删除其框架图后延迟销毁QWindow(即它试图渲染的表面),我就不会这样做。得到崩溃。我然而在控制台上发出一些警告:

  

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

我的猜测是它是一个线程问题,后端仍在尝试使用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());
}

Scene.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

Scene.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);
}

MainWindow.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

MainWindow.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();
    });
}

2 个答案:

答案 0 :(得分:2)

我已经彻底测试了你的例子并得出了相同的结论。当您破坏窗口太快时,应用程序崩溃,可能是因为Qt3D仍然尝试向底层QSurface发出一些OpenGL命令。我认为这是一个应该报告的错误。

A&#39;清洁工&#39;解决此问题的方法可能是跟踪主窗口中生成的三维窗口。您可以维护一个指向生成所有窗口的指针列表(并且可能在某个时刻被用户关闭)。窗口最终在主窗口的析构函数中被销毁。

答案 1 :(得分:0)

我有完全一样的问题。我正在对话框中创建一个从Qt3DWindow派生的类,以便用户可以预览所做选择的效果,并且退出对话框时程序崩溃。实际上,在Windows上,此崩溃会导致调试器和Qt Creator也崩溃!

我尝试了各种方法来解决这个问题,并且有所帮助,因为事实证明这是10月23日修复的线程问题:

https://github.com/qt/qt3d/commit/3314694004b825263c9b9f2d69bd85da806ccbbc

现在的解决方法是应用补丁程序,然后重新编译Qt。我预计5.11.3(或可能是5.12)很快就会发布,但是如果您在对话框中使用Qt3D,则此bug是致命的。