Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use Qt3D rendertree and QtSceneLoader without geting object siluet in view?



  • Using a split RenderTree for transparentLayer and opaqueLayer. When using the SceenLoader to load object into the seen it seams that there are some graphics draw so that a black siluete of the object apare infrot of the sceen. Is this a bug or is there another way to use the sceen loader?

    Transparent.png

    In the picture the siluete of the robot is shown from the robot loaded infront of the transparent cone.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        createSceen();
    
        setWindowTitle("Load scene transparent");
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::createSceen()
    {
        // Create a view and attach to widget
        m_view = new Qt3DExtras::Qt3DWindow();
        QWidget *viewContainer = QWidget::createWindowContainer(m_view);
        setCentralWidget(viewContainer);
    
        // The root entity for the new sceen
        m_rootEntity = new Qt3DCore::QEntity();
    
        // Create a splitt rendertree
        m_transparentLayer = new Qt3DRender::QLayer;
        m_opaqueLayer = new Qt3DRender::QLayer;
    
    
        Qt3DRender::QRenderSurfaceSelector *renderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector();
        renderSurfaceSelector->setSurface(m_view);
        Qt3DRender::QClearBuffers *clearBuffers = new Qt3DRender::QClearBuffers(renderSurfaceSelector);
        clearBuffers->setBuffers(Qt3DRender::QClearBuffers::AllBuffers);
        clearBuffers->setClearColor(Qt::white);
    
        Qt3DRender::QViewport *viewport = new Qt3DRender::QViewport(renderSurfaceSelector);
        Qt3DRender::QCameraSelector *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(-20.0f, 20.0f, 0.0f));
        m_camera->setUpVector(QVector3D(0,1,0));
        m_camera->setViewCenter(QVector3D(0, 0, 0));
    
        cameraSelector->setCamera(m_camera);
    
    
        // not entirely sure why transparent filter has to go first
        // I would have expected the reversed order of the filters but this works...
    
        Qt3DRender::QLayerFilter *transparentFilter = new Qt3DRender::QLayerFilter(m_camera);
        transparentFilter->addLayer(m_transparentLayer);
    
        Qt3DRender::QLayerFilter *opaqueFilter = new Qt3DRender::QLayerFilter(m_camera);
        opaqueFilter->addLayer(m_opaqueLayer);
    
        m_view->setActiveFrameGraph(renderSurfaceSelector);
    
        m_opaqueLayer->setRecursive(true);
        m_transparentLayer->setRecursive(true);
    
        m_view->setRootEntity(m_rootEntity);
    
        Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(m_rootEntity);
        camController->setLinearSpeed( 50.0f );
        camController->setLookSpeed( 180.0f );
        camController->setCamera(m_camera);
    
        // Create a few basic geometries that works...
    
        // Create a Cone Shape
        Qt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh();
        cone->setTopRadius(0.5);
        cone->setBottomRadius(1);
        cone->setLength(3);
        cone->setRings(50);
        cone->setSlices(20);
    
        // ConeMesh Transform
        Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform();
        coneTransform->setScale(1.5f);
        coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
        coneTransform->setTranslation(QVector3D(0.0f, 4.0f, -1.5));
    
        // Cone material
        Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial();
        coneMaterial->setDiffuse(QColor(QRgb(0x928327)));
    
        // Cone
        Qt3DCore::QEntity * coneEntity = new Qt3DCore::QEntity(m_rootEntity);
        coneEntity->addComponent(cone);
        coneEntity->addComponent(coneMaterial);
        coneEntity->addComponent(coneTransform);
        coneEntity->addComponent(m_opaqueLayer);
    
    
        // Create a Cylinder shape
        Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();
        cylinder->setRadius(1);
        cylinder->setLength(3);
        cylinder->setRings(100);
        cylinder->setSlices(20);
    
        // CylinderMesh Transform
        Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
        cylinderTransform->setScale(1.5f);
        cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
        cylinderTransform->setTranslation(QVector3D(-5.0f, 4.0f, -1.5));
    
        // Cylinder material
        Qt3DExtras::QPhongAlphaMaterial *cylinderMaterial = new Qt3DExtras::QPhongAlphaMaterial();
        cylinderMaterial->setDiffuse(QColor(QRgb(0x928327)));
        cylinderMaterial->setAlpha(0.5f);
    
        // Cylinder
        Qt3DCore::QEntity * cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);
        cylinderEntity->addComponent(cylinder);
        cylinderEntity->addComponent(cylinderMaterial);
        cylinderEntity->addComponent(cylinderTransform);
        cylinderEntity->addComponent(m_transparentLayer);
    
        // Now create a object using the scene loader...
        // NOT WORKING? This cause an black area to be displayed infront of the sceen :(
        QString fileName = QStandardPaths::locate(QStandardPaths::HomeLocation,"object-1-obj/object-1.obj");
        Qt3DCore::QEntity * objectEntity = loadFromFile(QUrl::fromLocalFile(fileName));
        objectEntity->addComponent(m_transparentLayer);
    }
    
    Qt3DCore::QEntity *MainWindow::loadFromFile(const QUrl & url)
    {
        qDebug() << "Load from file: " << url;
    
        Qt3DCore::QEntity *sceneLoaderEntity = new Qt3DCore::QEntity(m_rootEntity);
        Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(sceneLoaderEntity);
    
        sceneLoaderEntity->addComponent(sceneLoader);
        sceneLoader->setSource(url);
    
        return sceneLoaderEntity;
    }
    
    


  • Created a bug report for this problem https://bugreports.qt.io/browse/QTBUG-81087.


Log in to reply