3

The description of the new QScreenRayCaster in Qt3D looks like its exactly what I want to use, but I cannot get it to work for me. I guess that there is something I need to do in the initialization but I cannot find any examples online to point me in the right direction. I've written a very simple program to test the function. It draws a unit sphere in the center of a window and then triggers a QScreenRayCaster at the center point which as far as I can see should then return a list of hits, only in my program it doesn't. Any help or insight appreciated.

MyQt3DWindow:: MyQt3DWindow(QScreen *screen)
    : Qt3DExtras::Qt3DWindow(screen)
{
    // scene
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
    Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
    Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
    sphereEntity->addComponent(sphereMesh);
    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
    sphereEntity->addComponent(sphereTransform);
    sphereEntity->addComponent(material);

    // Camera
    Qt3DRender::QCamera *camera = this->camera();
    camera->lens()->setOrthographicProjection(-2, 2, -2, 2, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 40.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));

    // picker
    m_screenRayCaster = new Qt3DRender::QScreenRayCaster(rootEntity);
    m_screenRayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
    Qt3DRender::QLayer *pickableLayer = new Qt3DRender::QLayer(rootEntity);
    m_screenRayCaster->addLayer(pickableLayer);
    sphereEntity->addComponent(pickableLayer);

    this->setRootEntity(rootEntity);
    this->setGeometry(100, 100, 400, 400);

    // and call the caster after a short delay
    QTimer::singleShot(1000, this, SLOT(testRayCaster()));
}

void MyQt3DWindow::testRayCaster()
{
    m_screenRayCaster->trigger(QPoint(200, 200));
    Qt3DRender::QAbstractRayCaster::Hits hits = m_screenRayCaster->hits();
    qDebug() << "Hits found = " << hits.size() << "\n";
}

class MyQt3DWindow: public Qt3DExtras::Qt3DWindow
{
Q_OBJECT
public:
    MyQt3DWindow(QScreen *screen = nullptr);

public slots:
    void testRayCaster();

private:
    Qt3DRender::QScreenRayCaster *m_screenRayCaster;
};
Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48
Bill Sellers
  • 436
  • 3
  • 9

2 Answers2

9

You have to add the ray caster to the root entity as a component. You should also connect a slot to the ray caster's hitsChanged() function and display the hits from there. This way you can be sure that the caster computed the hits already.

By the way: If you have issues with Qt3D, checkout their github repository and look for the test folder. There is a folder inside with manual tests where you can see most of the functioning of Qt3D.

Florian Blume
  • 3,237
  • 17
  • 37
  • 2
    Just tried the suggestions and they work. It's a bit odd that Qt3DRender::QScreenRayCaster(rootEntity) does not automatically add the ray caster. Adding rootEntity->addComponent(m_screenRayCaster) makes it work. The hitsChanged suggestion is necessary too because the code only works some of the time as is. There is obviously some delayed calculation going on. – Bill Sellers Oct 05 '18 at 16:47
  • 1
    The delay is inherent in Qt's rendering process. The aspects are threaded which means that certain states are not always up to date. Another example is `QRenderCapture`, where you have to request a capture. If there wasn't any threading going on, the image could be returned immediately. This way you have to wait for it. – Florian Blume Oct 05 '18 at 18:52
  • The system of adding components is what drove me insane, as well. It's quite counter intuitive, to say the least... – Florian Blume Oct 05 '18 at 18:53
  • I think I'm getting the hang of the delay but it would be handy if there were a call to bring everything up to date. It's a bit inflexible to have to use a signal. – Bill Sellers Oct 09 '18 at 12:00
0

For people who stumble across this question when trying to get the screen caster to work, in addition to the accepted answer:

If you would like to the intersection to be carried out for all entities, you need to set the QLayer as a component of the root, and then set it as recursive. (Instead of setting it to sub entities individually, like sphereEntity in the question).

_rayCaster = new QScreenRayCaster(_rootEntity);
_rayCaster->setRunMode(QAbstractRayCaster::SingleShot);

auto pickableLayer = new QLayer(_rootEntity);
pickableLayer->setRecursive(true);
_rootEntity->addComponent(pickableLayer);

_rayCaster->addLayer(pickableLayer);
_rootEntity->addComponent(_rayCaster);
connect(_rayCaster, &QScreenRayCaster::hitsChanged, this, &SceneWidget::onRayCastResult);
RandomName
  • 163
  • 1
  • 10