1

I have a simple Qt3D code to test API.

#include <QGuiApplication>

#include <Qt3DCore/QEntity>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DLogic/QFrameAction>

#include "qt3dwindow.h"
Qt3DCore::QEntity *createScene()
{
    // Root entity
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;

    // Sphere
    Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
    sphereMesh->setRadius(3);
    sphereMesh->setGenerateTangents(true);

    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
    sphereTransform->setTranslation(QVector3D(0,0,0));

    Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);

    sphereEntity->addComponent(sphereMesh);
    sphereEntity->addComponent(sphereTransform);
    sphereEntity->addComponent(material);

    return rootEntity;
}

int main(int argc, char* argv[])
{
    QGuiApplication app(argc, argv);
    Qt3DExtras::Qt3DWindow view;

    Qt3DCore::QEntity *scene = createScene();

    // Camera
    Qt3DRender::QCamera *camera = view.camera();
    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 0));
    camera->setViewCenter(QVector3D(0, 0, 0));
    // Loop
    Qt3DLogic::QFrameAction* loop = new Qt3DLogic::QFrameAction();
    QObject::connect(loop, &Qt3DLogic::QFrameAction::triggered, [camera](float dt){
        camera->setPosition(camera->position() + QVector3D(0,0.1,0));
    });
    scene->addComponent(loop);

    view.setRootEntity(scene);
    view.show();

    return app.exec();
}

And this image represents the scene from the sample.

Scene

  • At first, both camera and sphere are placed in Vector(0,0,0)
  • The camera goes in +Y direction per frame.
  • Camera will always face the sphere and the sphere will always place in the middle of the screen. I assumed that setViewCenter(position_of_sphere) function would achieve this spec.

I have 2 questions.

  1. Does Qt3D uses left-hand coordinate system? Is my image right?
  2. When I run the code above, I got this animation as result.

Result of the sample code

I am not sure why the sphere goes up-right direction and does not place in the middle of the screen.


Update

I tested the sample code with Windows 10 environment and got the exact result as I expected. Thank you for the comment, Florian! It seems that this issue only causes in MacOSX (10.15 in exact).

0 Answers0