Qt3D Filtering Entities

177 views Asked by At

I am trying to display two entites into two different viewport. Looks like QFilter should help, but I don't know how.

Here is what I did:

Root entites creation:

    auto rootNode        = new Qt3DCore::QEntity;
    auto rootScene       = new Qt3DCore::QEntity(rootNode);
    auto renderSettings  = new Qt3DRender::QRenderSettings(rootNode);
    auto surfaceSelector = new Qt3DRender::QRenderSurfaceSelector(renderSettings);

    renderSettings->setActiveFrameGraph(surfaceSelector);
    rootNode->addComponent(renderSettings);

Then Create viewports:

    // Main viewport
    auto mainViewport = new Qt3DRender::QViewport(surfaceSelector);
    mainViewport->setNormalizedRect(QRectF(0, 0, 1, 1));

    // clear buffer + NoDrawBuffer
    auto clearBuffer = new Qt3DRender::QClearBuffers(mainViewport);
    clearBuffer->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);
    clearBuffer->setClearColor(QColor(QRgb(0xa7a8db)));
    new Qt3DRender::QNoDraw(clearBuffer);

    // Left viewport
    auto leftViewport = new Qt3DRender::QViewport(mainViewport);
    leftViewport->setNormalizedRect(QRectF(0, 0, 0.5, 1));
    auto leftCamera = new Qt3DRender::QCamera(leftViewport);
    leftCamera->setProjectionType(Qt3DRender::QCameraLens::OrthographicProjection);
    leftCamera->setPosition(QVector3D(0, 0, 20.0f));
    leftCamera->setViewCenter(QVector3D(0, 0, 0));
    auto leftCameraSelector = new Qt3DRender::QCameraSelector(leftViewport);
    leftCameraSelector->setCamera(leftCamera);
    auto leftCameraController = new Qt3DExtras::QOrbitCameraController(leftViewport);
    leftCameraController->setCamera(leftCamera);

    // Right viewport
    auto rightViewport = new Qt3DRender::QViewport(mainViewport);
    rightViewport->setNormalizedRect(QRectF(0.5, 0, 0.5, 1));
    auto rightCamera = new Qt3DRender::QCamera(rightViewport);
    rightCamera->setProjectionType(Qt3DRender::QCameraLens::OrthographicProjection);
    rightCamera->setPosition(QVector3D(0, 0, 20.0f));
    rightCamera->setViewCenter(QVector3D(0, 0, 0));
    auto rightCameraSelector = new Qt3DRender::QCameraSelector(rightViewport);
    rightCameraSelector->setCamera(rightCamera);
    auto rightCameraController = new Qt3DExtras::QOrbitCameraController(rightViewport);
    rightCameraController->setCamera(rightCamera);

Next I create some object to be displayed on separated viewports.

    // Sphere object
    auto sent  = new Qt3DCore::QEntity(rootScene);
    auto smesh = new Qt3DExtras::QSphereMesh(sent);
    smesh->setRadius(10.0);
    sent->addComponent(smesh);
    sent->addComponent(new Qt3DExtras::QGoochMaterial);

    // Cone object
    auto cent  = new Qt3DCore::QEntity(rootScene);
    auto cmesh = new Qt3DExtras::QConeMesh(cent);
    cmesh->setTopRadius(1.0);
    cmesh->setBottomRadius(12.0);
    cmesh->setLength(15);
    cent->addComponent(cmesh);
    cent->addComponent(new Qt3DExtras::QMetalRoughMaterial);

Then Layer to split :

    // Show sphere on the left
    auto sphereLayer = new Qt3DRender::QLayer(sent);
    sphereLayer->setRecursive(true);
    sent->addComponent(sphereLayer);
    auto leftLayerFilter = new Qt3DRender::QLayerFilter(leftViewport);
    leftLayerFilter->addLayer(sphereLayer);

    // Show cone on the right
    auto coneLayer = new Qt3DRender::QLayer(cent);
    coneLayer->setRecursive(true);
    cent->addComponent(coneLayer);
    auto rightLayerFilter = new Qt3DRender::QLayerFilter(rightViewport);
    rightLayerFilter->addLayer(coneLayer);

Final configurations:

    // Picker to select right viewport
    auto picker = new Qt3DRender::QObjectPicker(rootNode);
    QObject::connect(picker, &Qt3DRender::QObjectPicker::pressed, [&](Qt3DRender::QPickEvent* pick) {
        if (pick->viewport() == leftViewport)
        {
            leftCameraController->setEnabled(true);
            rightCameraController->setEnabled(false);
        }
        else if (pick->viewport() == rightViewport)
        {
            leftCameraController->setEnabled(false);
            rightCameraController->setEnabled(true);
        }
    });
    rootNode->addComponent(picker);

    // Display
    Qt3DExtras::Qt3DWindow w3d;
    w3d.setRootEntity(rootNode);
    w3d.setActiveFrameGraph(surfaceSelector);
    w3d.show();

    leftCamera->viewAll();
    rightCamera->viewAll();

Result is that both objects are display on both viewports. What did I miss ?

0

There are 0 answers