如何在标准GUI应用程序中使用Qt3D进行渲染?

时间:2014-04-22 22:00:36

标签: c++ qt opengl qt3d

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

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

我已经查看了Qtgl小部件,但我真的想使用Qt3D的场景管理功能。

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

这可能吗?

更新

所以我把它添加到我的MainWindow.cpp它基于这个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开头是这样的:

#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);

    }

}

它崩溃了: 此应用程序已请求运行时以不寻常的方式终止它。

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

编辑添加了有问题的其他课程

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

LoadModelView view;

然而,说

LoadModelView *view = new LoadModelView(this)

崩溃

1 个答案:

答案 0 :(得分:7)

你可以继承QGLView类,它扩展了QGLWidget并支持3D查看:

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-Layouts中。这样你  可以将您的QGLView嵌入到小部件中。