Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt3D picking triangles
Qt 6.11 is out! See what's new in the release blog

Qt3D picking triangles

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RLayman
    wrote on last edited by
    #1

    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 signal

    connect(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.

    M 1 Reply Last reply
    0
    • R RLayman

      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 signal

      connect(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.

      M Offline
      M Offline
      m3g1dd
      wrote on last edited by m3g1dd
      #2

      @RLayman I had the same problem, I had this method to add Qt3DRender::QObjectPicker to 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::QRenderSettings are added to entities like this, note that the PickMethod must be TrianglePicking:

      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 casting Qt3DRender::QPickEvent * as Qt3DRender::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();
      }
      
      U 1 Reply Last reply
      0
      • M m3g1dd

        @RLayman I had the same problem, I had this method to add Qt3DRender::QObjectPicker to 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::QRenderSettings are added to entities like this, note that the PickMethod must be TrianglePicking:

        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 casting Qt3DRender::QPickEvent * as Qt3DRender::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();
        }
        
        U Offline
        U Offline
        unclejimbo 0
        wrote on last edited by
        #3

        @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?

        M 1 Reply Last reply
        0
        • U unclejimbo 0

          @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?

          M Offline
          M Offline
          m3g1dd
          wrote on last edited by m3g1dd
          #4

          @unclejimbo-0 Hello. My Entity is imported with Qt3DRender::QSceneLoader like 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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved