Qt3D SceneLoader 实体未呈现

Posted

技术标签:

【中文标题】Qt3D SceneLoader 实体未呈现【英文标题】:Qt3D SceneLoader entity is not rendered 【发布时间】:2018-11-14 10:17:33 【问题描述】:

我正在尝试创建一个能够呈现包含透明对象的 obj 的 Qt3D 原型。因此,我需要以某种方式将QSortPolicy 与自定义框架图合并。我整理了一些在互联网上找到的示例(不幸的是,没有很多 Qt3D C++ 示例)。原型的来源如下所示。问题是我将QTorusMeshQSceneLoader 的内容添加到根实体,但是在渲染时只会显示圆环,obj(在本例中为猴子)不会渲染,我不会知道为什么,也不知道如何调试它。我倾倒了所有的树(场景图和框架图)并且找不到任何不一致之处。只有我能想到加载的对象以某种方式需要不同的渲染器(QGeometryRenderer),需要将其添加到框架图中?有人知道我在做什么错吗?

示例:Custom frame graph、Scene walker

#include <QGuiApplication>

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>

#include <Qt3DInput/QInputAspect>

#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QTorusMesh>

#include <Qt3DRender/QCamera>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DRender/QSceneLoader>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QLayerFilter>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QLayer>
#include <Qt3DRender/QRenderSettings>

class SceneWalker : public QObject

public:
    SceneWalker(Qt3DRender::QSceneLoader* loader):
        m_loader(loader)
    

    void onStatusChanged();

private:
    void walkEntity(Qt3DCore::QEntity* e, int depth = 0);

    Qt3DRender::QSceneLoader* m_loader;
;

void SceneWalker::onStatusChanged()

    qDebug() << "Status changed:" << m_loader->status();
    if (m_loader->status() != Qt3DRender::QSceneLoader::Ready)
        return;

    // The QSceneLoader instance is a component of an entity. The loaded scene
    // tree is added under this entity.
    QVector<Qt3DCore::QEntity*> entities = m_loader->entities();

    // Technically there could be multiple entities referencing the scene loader
    // but sharing is discouraged, and in our case there will be one anyhow.
    if (entities.isEmpty())
        return;

    Qt3DCore::QEntity* root = entities[0];
    // Print the tree.
    walkEntity(root);

    // To access a given node (like a named mesh in the scene), use QObject::findChild().
    // The scene structure and names always depend on the asset.
    Qt3DCore::QEntity* e = root->findChild<Qt3DCore::QEntity*>(QStringLiteral("PlanePropeller_mesh")); // toyplane.obj
    if (e)
        qDebug() << "Found propeller node" << e << "with components" << e->components();


void SceneWalker::walkEntity(Qt3DCore::QEntity* e, int depth)

    Qt3DCore::QNodeVector nodes = e->childNodes();
    for (int i = 0; i < nodes.count(); ++i)
    
        Qt3DCore::QNode* node = nodes[i];
        Qt3DCore::QEntity* entity = qobject_cast<Qt3DCore::QEntity*>(node);
        if (entity)
        
            QString indent;
            indent.fill(' ', depth * 2);
            qDebug().noquote() << indent << "Entity:" << entity << "Components:" << entity->components();
            walkEntity(entity, depth + 1);
        
    


int main(int argc, char* argv[])

    QGuiApplication app(argc, argv);

    Qt3DExtras::Qt3DWindow* window = new Qt3DExtras::Qt3DWindow();

    // Root
    Qt3DCore::QEntity* rootEntity = new Qt3DCore::QEntity();
    window->setRootEntity(rootEntity);

    Qt3DRender::QRenderSurfaceSelector *renderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector();
    renderSurfaceSelector->setSurface(window);

    // clearing the buffers
    Qt3DRender::QClearBuffers* clearBuffers = new Qt3DRender::QClearBuffers(renderSurfaceSelector);
    clearBuffers->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);

    // Framegraph for objects
    Qt3DRender::QLayerFilter* objectsLayerFilter = new Qt3DRender::QLayerFilter(renderSurfaceSelector);
    Qt3DRender::QLayer* objectsLayer = new Qt3DRender::QLayer(objectsLayerFilter);
    objectsLayerFilter->addLayer(objectsLayer);
    Qt3DRender::QViewport* viewport = new Qt3DRender::QViewport(objectsLayer);
    Qt3DRender::QCameraSelector* objectsCameraSelector = new Qt3DRender::QCameraSelector(viewport);
    Qt3DRender::QCamera* objectsCamera = new Qt3DRender::QCamera(objectsCameraSelector);
    objectsCamera->lens()->setPerspectiveProjection(45.f, 16.0f/9.0f, 0.01f, 1000.f);
    objectsCamera->setPosition(QVector3D(0, 0, -10));
    objectsCamera->setViewCenter(QVector3D(0, 0, 0));
    objectsCamera->setUpVector(QVector3D(0, 1, 0));
    objectsCameraSelector->setCamera(objectsCamera);

    // Set the new framegraph
    window->setActiveFrameGraph(renderSurfaceSelector);
    window->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::Always);

    // camera controls
    Qt3DExtras::QOrbitCameraController* camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    camController->setLinearSpeed(50.0f);
    camController->setLookSpeed(180.0f);
    camController->setCamera(objectsCamera);

    // Torus
    Qt3DCore::QEntity* torusEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QTorusMesh* torusMesh = new Qt3DExtras::QTorusMesh(torusEntity);
    torusMesh->setSlices(50.0f);
    torusMesh->setRings(50.0f);
    torusMesh->setRadius(2.0f);
    Qt3DExtras::QPhongMaterial* torusMaterial = new Qt3DExtras::QPhongMaterial(torusEntity);
    torusMaterial->setAmbient(Qt::gray);
    Qt3DCore::QTransform* torusTransform = new Qt3DCore::QTransform(torusEntity);
    torusTransform->setTranslation(QVector3D(0.0f, 0.0f, 10.0f));
    torusTransform->setRotationY(50.0f);
    torusTransform->setScale(2.0f);
    torusEntity->addComponent(torusTransform);
    torusEntity->addComponent(torusMesh);
    torusEntity->addComponent(torusMaterial);
    torusEntity->addComponent(objectsLayer);

    // Scene loader
    Qt3DCore::QEntity* sceneLoaderEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QSceneLoader* sceneLoader = new Qt3DRender::QSceneLoader(sceneLoaderEntity);
    sceneLoader->setSource(QUrl::fromLocalFile("monkey.obj"));

    // Transform
    Qt3DCore::QTransform* sceneLoaderTransform = new Qt3DCore::QTransform(sceneLoaderEntity);
    sceneLoaderTransform->setScale(2.0f);
    sceneLoaderTransform->setTranslation(QVector3D(0.0f, 0.0f, 10.0f));

    SceneWalker sceneWalker(sceneLoader);
    QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, &sceneWalker, &SceneWalker::onStatusChanged);

    sceneLoaderEntity->addComponent(sceneLoader);
    sceneLoaderEntity->addComponent(sceneLoaderTransform);
    sceneLoaderEntity->addComponent(objectsLayer);

    window->show();

    return app.exec();

【问题讨论】:

【参考方案1】:

问题是您实际上并没有将猴子添加到对象层。 QSceneLoader 创建自己的场景子树,即使 OBJ 文件包含单个对象。所以你只是将子树的根实体添加到对象层。

SceneWalker 对象遍历QSceneLoader 子树并提供有关其结构的信息。您只需查看控制台输出,找到要渲染的实体的名称,然后将它们附加到所需的层。

这必须使用信号和槽异步完成。在创建 QSceneLoader 对象后立即执行此操作(假设您的网格名为“monkey”):

    QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, &app,
        [sceneLoader, objectsLayer](Qt3DRender::QSceneLoader::Status s) 
            if (s == Qt3DRender::QSceneLoader::Status::Ready)
                sceneLoader->entity("monkey")->addComponent(objectsLayer);
    );

由于您使用的是 OBJ 文件,您甚至可以完全放弃 SceneWalker。 OBJ 文件是基于文本的,因此您只需使用文本编辑器打开它即可找到网格的名称。

如果您成功使用图层过滤器,则无需使用QSortPolicy(查看this answer)。

【讨论】:

以上是关于Qt3D SceneLoader 实体未呈现的主要内容,如果未能解决你的问题,请参考以下文章

Qt3D:根据实体和相机之间的距离缩放实体大小

设置实体旋转的原点(Qt 3D)

如何在页面模块上使用实体?

通过 Qt3D 模块导入 STL 文件:如何配置灯光和材质

如何在 Qt3D 中创建撤消/重做操作? [关闭]

qtcanvas3d和qt3d的区别