QScreenRayCaster找不到实体.我有什么错吗? [英] QScreenRayCaster not finding entity. What am I doiong wrong?
问题描述
在Qt3D中对新QScreenRayCaster的描述似乎正是我要使用的,但是我无法让它对我有用.我猜想我需要在初始化中做一些事情,但是我找不到在线示例来指出正确的方向.我编写了一个非常简单的程序来测试该功能.它在窗口的中心绘制一个单位球体,然后在中心点触发QScreenRayCaster,据我所见,应该然后返回命中列表,只有在我的程序中才返回.任何帮助或见解表示赞赏.
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;
};
推荐答案
您必须将ray caster作为组件添加到根实体.您还应该将插槽连接到射线投射器的hitsChanged()
功能并从此处显示命中.这样,您可以确定施法者已经计算出了匹配数.
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.
顺便说一句:如果Qt3D有问题,请检出其github存储库并查找
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.
这篇关于QScreenRayCaster找不到实体.我有什么错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!