Qt3D: using Qt3DRender::QLayerFilter
Asked Answered
A

1

3

I am trying to use layer filtering as shown in this answer. For this I wrote a simple test (see below). if I transfer code from a My3DWindow class to a main.cpp - this code is worked. Where is the error in My3DWindow class? The full test project can be viewed here.

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication application(argc, argv);
    auto window = new My3DWindow;

    auto sphere1 = new Qt3DCore::QEntity(window->Scene());
    auto sphere2 = new Qt3DCore::QEntity(window->Scene());

    auto transform1 = new Qt3DCore::QTransform;
    transform1->setTranslation(QVector3D(0.0f, 0.0f, -10.0f));

    auto transform2 = new Qt3DCore::QTransform;
    transform2->setTranslation(QVector3D(-10.0f, 0.0f, 0.0f));

    auto material1 = new Qt3DExtras::QPhongMaterial;
    material1->setAmbient(Qt::blue);

    auto material2 = new Qt3DExtras::QPhongAlphaMaterial;
    material2->setAmbient(Qt::red);

    auto spheremesh = new Qt3DExtras::QSphereMesh;
    spheremesh->setRadius(15.0);
    spheremesh->setSlices(32);
    spheremesh->setRings(32);

    sphere1->addComponent(transform1);
    sphere1->addComponent(material1);
    sphere1->addComponent(spheremesh);
    sphere1->addComponent(window->OpaqueLayer());

    sphere2->addComponent(transform2);
    sphere2->addComponent(material2);
    sphere2->addComponent(spheremesh);
    sphere2->addComponent(window->TransparentLayer());

    window->show();
    return application.exec();
}

and My3DWindow class:

My3DWindow::My3DWindow(QScreen *screen):
    Qt3DExtras::Qt3DWindow(screen)
{
    m_Scene = new Qt3DCore::QEntity;
    setRootEntity(m_Scene);

    auto renderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector(m_Scene);
    renderSurfaceSelector->setSurface(this);
    auto clearBuffers = new Qt3DRender::QClearBuffers(renderSurfaceSelector);
    clearBuffers->setBuffers(Qt3DRender::QClearBuffers::AllBuffers);
    clearBuffers->setClearColor(Qt::gray);

    auto viewport = new Qt3DRender::QViewport(renderSurfaceSelector);
    auto cameraSelector = new Qt3DRender::QCameraSelector(viewport);
    m_Camera = new Qt3DRender::QCamera(cameraSelector);
    m_Camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    m_Camera->setPosition(QVector3D(0.0f, 0.0f, 100.0f));
    m_Camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));

    auto cameraController = new Qt3DExtras::QFirstPersonCameraController(m_Scene);
    cameraController->setCamera(m_Camera);

    m_TransparentLayer = new Qt3DRender::QLayer;
    auto transparentFilter = new Qt3DRender::QLayerFilter(m_Camera);
    transparentFilter->addLayer(m_TransparentLayer);

    m_OpaqueLayer = new Qt3DRender::QLayer;
    auto opaqueFilter = new Qt3DRender::QLayerFilter(m_Camera);
    opaqueFilter->addLayer(m_OpaqueLayer);

    setActiveFrameGraph(renderSurfaceSelector);
}
Admixture answered 21/12, 2019 at 14:48 Comment(3)
You move the code from My3DWindow.cpp to main.cpp and then what you could have done differently is "parent" dependencies like renderSurfaceSelector->setSurface(this). We cannot see the working case with main.cpp yet to discover what is really different yet.Penmanship
@AlexanderV My mistake. 1) To code to need in the morning and not at night. 2) I missed an important line after initializing the camera (before declaring "cameraController"): cameraSelector->setCamera(m_Camera); The correct code completely I'll post here But a small question still remained. Sometimes a test application crashes when closed. What could be the reason?Admixture
@AlexanderV Appeared also a problem with artifacts of displaying at some superposition of spheres. Sounds nice. But this is probably a separate issue. Just try to press WSAD buttons in a full test code.Admixture
A
3

The correct code on the main issue is given below. The remaining problem is described here.

My3DWindow class:

My3DWindow::My3DWindow(QScreen *screen):
    Qt3DExtras::Qt3DWindow(screen)
{
    m_Scene = new Qt3DCore::QEntity;
    setRootEntity(m_Scene);

    auto renderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector(m_Scene);
    renderSurfaceSelector->setSurface(this);
    auto clearBuffers = new Qt3DRender::QClearBuffers(renderSurfaceSelector);
    clearBuffers->setBuffers(Qt3DRender::QClearBuffers::AllBuffers);
    clearBuffers->setClearColor(Qt::gray);

    auto viewport = new Qt3DRender::QViewport(renderSurfaceSelector);
    auto cameraSelector = new Qt3DRender::QCameraSelector(viewport);

    m_Camera = new Qt3DRender::QCamera(cameraSelector);
    m_Camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    m_Camera->setPosition(QVector3D(0.0f, 0.0f, 100.0f));
    m_Camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));

    cameraSelector->setCamera(m_Camera);

    auto cameraController = new Qt3DExtras::QFirstPersonCameraController(m_Scene);
    cameraController->setCamera(m_Camera);

    // именно в таком порядке: m_OpaqueLayer, m_TransparentLayer
    m_OpaqueLayer = new Qt3DRender::QLayer;
    auto opaqueFilter = new Qt3DRender::QLayerFilter(m_Camera);
    opaqueFilter->addLayer(m_OpaqueLayer);

    m_TransparentLayer = new Qt3DRender::QLayer;
    auto transparentFilter = new Qt3DRender::QLayerFilter(m_Camera);
    transparentFilter->addLayer(m_TransparentLayer);

    setActiveFrameGraph(renderSurfaceSelector);
}

main.cpp:

My3DWindow window;

instead

auto window = new My3DWindow;
Admixture answered 22/12, 2019 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.