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

Why are Qt3D Camera settings different in C++ and QML ?



  • Hi.
    I'm trying to learn, with some difficulty, Qt3D.

    I have a simple 3D text example written in both C++ and QML, with, what I believe exactly the same parameters.

    However, I found out that in QML, the image would be upside down + I had to set the z position of the camera to a much bigger value than in C++.

    To correct the orientation, I need to explicitly set the upVector property of the camera to Qt.vector3d( 0.0, -1.0, 0.0 ) in QML.

    Why these differences between C++ and QML?

    C++ code:

    #include <QCoreApplication>
    #include <Qt3DCore/Qt3DCore>
    #include <Qt3DExtras/Qt3DExtras>
    #include <Qt3DExtras/QExtrudedTextMesh>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication a(argc, argv);
        Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
    
        view->setTitle(QStringLiteral("3D Text CPP"));
        view->setHeight(480);
        view->setWidth(800);
        view->defaultFrameGraph()->setClearColor(QColor("white"));
    
        auto *root = new Qt3DCore::QEntity();
    
        Qt3DRender::QCamera *camera = view->camera();
        camera->lens()->setPerspectiveProjection(65.f, 800.0f/480.f, 0.1f, 100.f);
        camera->setPosition(QVector3D(0.f, 1.f, 3.f));
        camera->setViewCenter(QVector3D(0.f, 1.f, 0.f));
    
        auto *cameraController = new Qt3DExtras::QOrbitCameraController(root);
        cameraController->setCamera(camera);
    
        QFont font("Mukta Mahee ExtraBold", 32, -1, false);
        auto *textMesh = new Qt3DExtras::QExtrudedTextMesh();
        textMesh->setDepth(.25f);
        textMesh->setFont(font);
        textMesh->setText("TEST");
    
        auto *textMaterial = new Qt3DExtras::QMetalRoughMaterial(root);
        textMaterial->setBaseColor(QColor("white"));
        textMaterial->setMetalness(1.0);
        textMaterial->setRoughness(0.0);
    
        auto *textTransform = new Qt3DCore::QTransform();
        textTransform->setTranslation(QVector3D(-2.5f, 1.0f, -0.5f));
    
        auto *text = new Qt3DCore::QEntity(root);
        text->addComponent(textMaterial);
        text->addComponent(textMesh);
        text->addComponent(textTransform);
    
        view->setRootEntity(root);
        view->show();
    
        return a.exec();
    }
    

    QML:

    import Qt3D.Extras 2.12
    import Qt3D.Core 2.12
    import Qt3D.Render 2.12
    import Qt3D.Input 2.12
    
    Entity {
        id : root
    
        components: [
               RenderSettings {
                   activeFrameGraph: ForwardRenderer {
                       clearColor: "white"
                       camera: camera
                   }
               },
    
                InputSettings { }
        ]
    
        Camera {
            id: camera
    
            projectionType: CameraLens.PerspectiveProjection
            fieldOfView: 65
            aspectRatio: 800/480
            nearPlane : 0.1
            farPlane : 100.0
    
            upVector: Qt.vector3d( 0.0, -1.0, 0.0 ) //<-- y must be -1  to be up?
            position: Qt.vector3d( 0.0, 1.0, 60.0 ) //<--  z was only 3.0 in C++ to get it in the view
    
            viewCenter: Qt.vector3d( 0.0, 1.0, 0.0 )
        }
    
        OrbitCameraController {
            camera: camera
        }
    
        ExtrudedTextMesh {
            id: textMesh
            font.family: "Mukta Mahee ExtraBold"
            font.pixelSize: 32
            font.bold: true
            text: "TEST"
            depth: 0.25
        }
    
        MetalRoughMaterial {
            id: material
            baseColor: "white"
            metalness: 1.0
            roughness: 0.0
        }
    
        Transform {
            id: textTransform
            translation: Qt.vector3d(-2.5, 1.0, -0.5)
            scale: 0.8
        }
    
        Entity {
            id: text3d
            components: [ textMesh, material, textTransform ]
        }
    }
    

Log in to reply