gpt4 book ai didi

qt3d - QScreenRayCaster 未找到实体。我做错了什么?

转载 作者:行者123 更新时间:2023-11-30 23:57:10 26 4
gpt4 key购买 nike

Qt3D 中新 QScreenRayCaster 的描述看起来正是我想要使用的,但我无法让它为我工作。我想在初始化过程中我需要做一些事情,但我在网上找不到任何示例来为我指明正确的方向。我写了一个非常简单的程序来测试这个功能。它在窗口的中心绘制一个单位球体,然后在中心点触发一个 QScreenRayCaster,据我所知,它应该返回一个命中列表,只有在我的程序中它没有。任何帮助或见解表示赞赏。

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;
};

最佳答案

您必须将光线转换器作为组件添加到根实体。您还应该将插槽连接到光线转换器的 hitsChanged() 函数并从那里显示命中。这样你就可以确定施法者已经计算了命中。

顺便说一句:如果您对 Qt3D 有疑问,请查看他们的 github 存储库并查找 test folder .里面有一个包含手动测试的文件夹,您可以在其中看到 Qt3D 的大部分功能。

关于qt3d - QScreenRayCaster 未找到实体。我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52666741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com