Qt3D picking triangles
-
I've been trying to get triangle data for a good while now using Qt3D in C++ with no success. I have been able to get the PickEvent to work and it's data is correct but I don't know how to get the PickTriangleEvent. I've search all over the place but was only able to find one example on getting the triangle data and it was in QML - when I tried the same thing in C++ it didn't work.
Currently, this is how I'm getting the PickEvent data.
I add the QObjectPicker component to the mesh object.
connect the signalconnect(m_pObjectPicker,SIGNAL(clicked(Qt3DRender::QPickEvent*)),this,SLOT(clicked(Qt3DRender::QPickEvent*)));The clicked slot looks like this:
void Mesh::clicked(Qt3DRender::QPickEvent* event){ ... }My pick settings are already set to triangles:
m_pFrameGraph->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking); m_pFrameGraph->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);And this all works fine except when trying to get the triangle data.
-
I've been trying to get triangle data for a good while now using Qt3D in C++ with no success. I have been able to get the PickEvent to work and it's data is correct but I don't know how to get the PickTriangleEvent. I've search all over the place but was only able to find one example on getting the triangle data and it was in QML - when I tried the same thing in C++ it didn't work.
Currently, this is how I'm getting the PickEvent data.
I add the QObjectPicker component to the mesh object.
connect the signalconnect(m_pObjectPicker,SIGNAL(clicked(Qt3DRender::QPickEvent*)),this,SLOT(clicked(Qt3DRender::QPickEvent*)));The clicked slot looks like this:
void Mesh::clicked(Qt3DRender::QPickEvent* event){ ... }My pick settings are already set to triangles:
m_pFrameGraph->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking); m_pFrameGraph->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);And this all works fine except when trying to get the triangle data.
@RLayman I had the same problem, I had this method to add
Qt3DRender::QObjectPickerto my entities:Qt3DRender::QObjectPicker *EditorScene::createObjectPickerForEntity(Qt3DCore::QEntity *entity) { Qt3DRender::QObjectPicker *picker = new Qt3DRender::QObjectPicker(entity); entity->addComponent(picker); connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &EditorScene::handlePickerPress); return picker; }My rendering settings
Qt3DRender::QRenderSettingsare added to entities like this, note that thePickMethodmust beTrianglePicking:m_renderSettings = new Qt3DRender::QRenderSettings(); m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking); m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::NearestPick); entity->addComponent(m_renderSettings);And I had this code to handle click press events I was able to print click position/location by
event->worldIntersection()but i didn't know how to get the index of clicked triangle. Finally, I managed to print the index of clicked triangle by castingQt3DRender::QPickEvent *asQt3DRender::QPickTriangleEvent *:void EditorScene::handlePickerPress(Qt3DRender::QPickEvent *event) { // // "event" will give me clicked coordinates like this: qDebug() << __func__ << ": global Coord: " << event->worldIntersection(); // // Also I can get picked/clicked triangle index and its vertices by casting event pointer type: Qt3DRender::QPickTriangleEvent *eventTri = static_cast<Qt3DRender::QPickTriangleEvent *>(event); qDebug() << __func__ << "Pick Triangle Index: " << eventTri->triangleIndex(); qDebug() << __func__ << "Pick Triangle Vertex 1: " << eventTri->vertex1Index(); qDebug() << __func__ << "Pick Triangle Vertex 2: " << eventTri->vertex2Index(); qDebug() << __func__ << "Pick Triangle Vertex 3: " << eventTri->vertex3Index(); } -
@RLayman I had the same problem, I had this method to add
Qt3DRender::QObjectPickerto my entities:Qt3DRender::QObjectPicker *EditorScene::createObjectPickerForEntity(Qt3DCore::QEntity *entity) { Qt3DRender::QObjectPicker *picker = new Qt3DRender::QObjectPicker(entity); entity->addComponent(picker); connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &EditorScene::handlePickerPress); return picker; }My rendering settings
Qt3DRender::QRenderSettingsare added to entities like this, note that thePickMethodmust beTrianglePicking:m_renderSettings = new Qt3DRender::QRenderSettings(); m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking); m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::NearestPick); entity->addComponent(m_renderSettings);And I had this code to handle click press events I was able to print click position/location by
event->worldIntersection()but i didn't know how to get the index of clicked triangle. Finally, I managed to print the index of clicked triangle by castingQt3DRender::QPickEvent *asQt3DRender::QPickTriangleEvent *:void EditorScene::handlePickerPress(Qt3DRender::QPickEvent *event) { // // "event" will give me clicked coordinates like this: qDebug() << __func__ << ": global Coord: " << event->worldIntersection(); // // Also I can get picked/clicked triangle index and its vertices by casting event pointer type: Qt3DRender::QPickTriangleEvent *eventTri = static_cast<Qt3DRender::QPickTriangleEvent *>(event); qDebug() << __func__ << "Pick Triangle Index: " << eventTri->triangleIndex(); qDebug() << __func__ << "Pick Triangle Vertex 1: " << eventTri->vertex1Index(); qDebug() << __func__ << "Pick Triangle Vertex 2: " << eventTri->vertex2Index(); qDebug() << __func__ << "Pick Triangle Vertex 3: " << eventTri->vertex3Index(); }@m3g1dd Hi, is your QGeometryRenderer component a built-in mesh in Qt3SDExtras or is it a custom mesh?
I've got some troubles picking triangles for a custom mesh built from QGeometry, QAttribute and QBuffer. The clicked event never trigers. But when I use a built-in mesh like QSphereMesh it works fine. Any idea how to do triangle picking for custom meshes?
-
@m3g1dd Hi, is your QGeometryRenderer component a built-in mesh in Qt3SDExtras or is it a custom mesh?
I've got some troubles picking triangles for a custom mesh built from QGeometry, QAttribute and QBuffer. The clicked event never trigers. But when I use a built-in mesh like QSphereMesh it works fine. Any idea how to do triangle picking for custom meshes?
@unclejimbo-0 Hello. My Entity is imported with
Qt3DRender::QSceneLoaderlike this:m_sceneLoaderEntity = new Qt3DCore::QEntity(); Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(m_sceneLoaderEntity); // EditorSceneItemModel::handleImportEntityLoaderStatusChanged is a slot developed to react to a signal when import operation is finished QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, this, &EditorSceneItemModel::handleImportEntityLoaderStatusChanged); sceneLoader->setSource(fileUrl); m_sceneLoaderEntity->addComponent(sceneLoader);So, it might be considered as custom mesh! I think picking triangles should work fine with custom meshes. By the way, I'm working with Qt 5.11.2