Qt3D SceneLoader实体未呈现

时间:2018-11-14 10:17:33

标签: c++ qt3d qt5.11

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

示例:Custom frame graphScene 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 个答案:

答案 0 :(得分:2)

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

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

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

    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)。