如何在 qt3d 中将 rendersetting 组件添加到我的根实体?

How to add rendersetting component to my root entity in qt3d?

我正在研究一些关于 qt3d 的代码。我创建了一个 Qt3DWindow(命名视图)。

尝试在其中添加一个根实体(名为rootEntity)。

像这样放置一个立方体实体:

m_cubeEntity = new Qt3DCore::QEntity;
...
Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;
Qt3DRender::QMaterial *cubeMaterial = new Qt3DRender::QMaterial;
Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform;
Qt3DRender::QObjectPicker *objectPicker = new Qt3DRender::QObjectPicker;
...
m_cubeEntity.addComponent(cubeMesh);
m_cubeEntity.addComponent(cubeMaterial);
m_cubeEntity.addComponent(cubeTransform);
m_cubeEntity.adComponent(objectPicker);

m_cubeEntity.setParent(m_rootEntity);

一切正常。

然后我发现使用 PickingSettings.BoundingVolumePicking 来选择我的立方体实体是不准确的。我想要 PickingSettings.TrianglePicking。 ================================================ ==================/

然后我这样做:

    m_renderSettings = new Qt3DRender::QRenderSettings();
    m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
    m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
    m_renderer = new Qt3DExtras::QForwardRenderer();
    m_renderer->setClearColor(Qt::lightGray);
    m_renderSettings->setActiveFrameGraph(m_renderer);
    m_rootEntity->addComponent(m_renderSettings);

但是现在没有渲染任何东西。如果我从 rootEntity 中删除 "m_renderSettings",一切 returns 都是正确的。

如何为根实体正确设置渲染设置?

很可能你只是设置了一个错误的框架图。您可以使用dumpObjectTree()轻松显示标准框架图,这在调试Qt3D框架图时往往是一个非常有用的功能。

对于您的简单用例,只需使用 Qt3DWindowactiveFrameGraph() 中已经包含的 renderSettings 就足够了。

只需尝试以下简单的应用程序,其中包含您想要的 QObjectPicker

#include <QApplication>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QPickEvent>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QFrameGraphNode>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QRenderSettings>
#include <QDebug>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    auto view = new Qt3DExtras::Qt3DWindow();

    auto rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);

    // Shows your framegraph! Simple forward renderer! 
    view->activeFrameGraph()->dumpObjectTree(); 
    auto rendersettings=view->renderSettings();
    rendersettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
    rendersettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);

    auto cameraEntity = view->camera();
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
    cameraEntity->setPosition(QVector3D(0, 2, 0));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));

    auto cubeEntity = new Qt3DCore::QEntity(rootEntity);
    auto cubeMesh = new Qt3DExtras::QCuboidMesh;
    cubeMesh->setXExtent(1.);
    cubeMesh->setYExtent(1.);
    auto cubeMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
    auto objectPicker = new Qt3DRender::QObjectPicker;

    QObject::connect(objectPicker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* pick) {
        qDebug() << pick;
    });
    cubeEntity->addComponent(cubeMesh);
    cubeEntity->addComponent(cubeMaterial);
    cubeEntity->addComponent(objectPicker);

    view->show();
    return a.exec();
}