如何在标准GUI应用程序中的Qt3D中渲染? [英] How to render in Qt3D in standard GUI application?

查看:301
本文介绍了如何在标准GUI应用程序中的Qt3D中渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用Qt3D,但是我看到的所有示例都是全窗口应用程序.从示例中我无法理解的是如何将qt3d渲染窗口添加到常规的qt gui应用程序中.

I enjoy using Qt3D, but all of the examples I see for it are full window applications. What I can't understand from the examples is how to add a qt3d rendering window to a regular qt gui application.

基本上我想要的是Qt5 Gui应用程序的一个渲染小部件.

Basically what I want is a little rendering widget for my Qt5 Gui application.

我已经研究了Qtgl小部件,但我确实想使用Qt3D的场景管理功能.

I've looked into Qtgl widget, but I really want to use the scene management abilities of Qt3D.

如何在qt Gui窗口内部渲染为子窗口?

How can I render as a sub window inside of a qt Gui window?

这可能吗?

更新

因此我将其添加到MainWindow.cpp中.它大致基于此 http://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/

So I added this to my MainWindow.cpp It is loosely based off of this http://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/

LoadModelView *view = new LoadModelView(); //Crashes on this. Will not compile with
                                           // LoadModelView(this) 

    QWidget *container = QWidget::createWindowContainer(view);
    container->setFocusPolicy(Qt::TabFocus);

    ui->gridLayout->addWidget(container);

这似乎是正确的.

我的load_model.cpp像这样开始:

my load_model.cpp begins like this:

#include "qglmaterialcollection.h"
#include "qglmaterial.h"
#include "qglscenenode.h"
#include "qgllightmodel.h"
#include "qglabstractscene.h"
#include <QtGui/qmatrix4x4.h>

#include <QPropertyAnimation>
#include <QtCore/qmath.h>

#define DEGREE_TO_RAD (3.1415926/180.0)

LoadModelView::LoadModelView(QWindow *parent)
    : QGLView(parent)
    , m_pSTLScene(0)

{
    loadModels();

    camera()->setCenter(QVector3D(0, 0, 0));
    camera()->setEye(QVector3D(0, 4, 10));
}
LoadModelView::~LoadModelView()
{

    delete m_pSTLScene;
}

void LoadModelView::paintGL(QGLPainter *painter)
{
    QMatrix4x4 stlWorld;
    stlWorld.setToIdentity();
    stlWorld.scale(0.1);
    stlWorld.translate(QVector3D(2.0,0.0,0.0));

    painter->setStandardEffect(QGL::LitMaterial);
    painter->setFaceColor(QGL::AllFaces,QColor(170,202,0));

    painter->modelViewMatrix() = camera()->modelViewMatrix() * stlWorld;

    m_pSTLScene->mainNode()->draw(painter);
}

void FixNodesRecursive(int matIndex, QGLSceneNode* pNode)
{
    if (pNode) {
        pNode->setMaterialIndex(matIndex);
       // pNode->setEffect(QGL::FlatReplaceTexture2D);
        foreach (QGLSceneNode* pCh, pNode->children()) {
            FixNodesRecursive(matIndex, pCh);
        }
    }
}

void LoadModelView::loadModels()
{
    {
        m_pSTLScene = QGLAbstractScene::loadScene(QLatin1String(":/models/Sheep.stl"), QString(),"CorrectNormals CorrectAcute");
        Q_ASSERT(m_pSTLScene!=0);
        QGLMaterial *mat = new QGLMaterial;
        mat->setAmbientColor(QColor(170,202,0));
        mat->setDiffuseColor(QColor(170,202,0));
        mat->setShininess(128);

        QGLSceneNode* pSTLSceneRoot = m_pSTLScene->mainNode();
        int matIndex = pSTLSceneRoot->palette()->addMaterial(mat);
        pSTLSceneRoot->setMaterialIndex(matIndex);
        pSTLSceneRoot->setEffect(QGL::FlatReplaceTexture2D);
        FixNodesRecursive(matIndex,pSTLSceneRoot);

    }

}

它崩溃与: 该应用程序已请求运行时以一种异常方式终止它.

It crashes with: This application has requested the runtime to terminate it in an unusual way.

并在qt应用程序输出中: 无效的参数传递给C运行时函数.

and in the qt application output: Invalid parameter passed to C runtime function.

编辑添加了相关课程的其余部分

我注意到在示例中,我正在调整 http://doc.qt.digia.com/qt-quick3d-snapshot/qt3d-penguin-main-cpp.html 窗口的初始化过程是:

I notice that in the example I am adapting http://doc.qt.digia.com/qt-quick3d-snapshot/qt3d-penguin-main-cpp.html the window is initialized by saying:

LoadModelView view;

但是,说

LoadModelView *view = new LoadModelView(this)

崩溃

推荐答案

您可以将QGLView类作为子类,该类扩展了QGLWidget并支持3D查看:

You can subclass QGLView class which extends QGLWidget with support for 3D viewing:

class GLView : public QGLView
{
    Q_OBJECT

public:
    GLView(QWidget *parent = 0);
    ~GLView();

protected:
    void initializeGL(QGLPainter *painter);
    void paintGL(QGLPainter *painter);

private:
    QGLAbstractScene *m_scene;
    QGLSceneNode *m_rootNode;
};

GLView::GLView(QWidget *parent)
    : QGLView(parent)
    , m_scene(0)
    , m_rootNode(0)
{
    // Viewing Volume
    camera()->setFieldOfView(25);
    camera()->setNearPlane(1);
    camera()->setFarPlane(1000);

    // Position of the camera
    camera()->setEye(QVector3D(0, 3, 4));

    // Direction that the camera is pointing
    camera()->setCenter(QVector3D(0, 3, 0));
}

GLView::~GLView()
{
    delete m_scene;
}

void GLView::initializeGL(QGLPainter *painter)
{
    // Background color
    painter->setClearColor(QColor(70, 70, 70));

    // Load the 3d model from the file
    m_scene = QGLAbstractScene::loadScene("models/model1/simplemodel.obj");

    m_rootNode = m_scene->mainNode();
}

void GLView::paintGL(QGLPainter *painter)
{
    m_rootNode->draw(painter);
}

Qt 5.1引入了功能QWidget :: createWindowContainer().一个为现有QWindow创建QWidget包装器的函数,使它可以驻留在基于QWidget的应用程序中.您可以使用QWidget :: createWindowContainer在QWidget中创建一个QWindow.这允许将QWindow子类放置在Widget布局中.这样你 可以将您的QGLView嵌入到小部件中.

Qt 5.1 introduces the function QWidget::createWindowContainer(). A function that creates a QWidget wrapper for an existing QWindow, allowing it to live inside a QWidget-based application. You can use QWidget::createWindowContainer which creates a QWindow in a QWidget. This allows placing QWindow-subclasses in Widget-Layouts. This way you can embed your QGLView inside a widget.

这篇关于如何在标准GUI应用程序中的Qt3D中渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆