Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Qt3D point and line picking?
QtWS25 Last Chance

Qt3D point and line picking?

Scheduled Pinned Locked Moved Solved Game Development
6 Posts 4 Posters 2.3k 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.
  • U Offline
    U Offline
    unclejimbo 0
    wrote on last edited by unclejimbo 0
    #1

    Hi there, I've been trying to do point and line picking on a mesh in Qt3D. Here's a working version of triangle picking,

    auto renderSettings = new Qt3DRender::QRenderSettings(root);
    renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
    
    auto entity = new Qt3DCore::QEntity(root);
    auto picker = new Qt3DRender::QObjectPicker;
    auto sphere = new Qt3DExtras::QSphereGeometry;
    entity->addComponent(picker);
    entity->addComponent(sphere);
    
    connect(picker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* e) {
        auto p = dynamic_cast<Qt3DRender::QPickTriangleEvent*>(e);
        auto idx = p->triangleIndex();
    });
    

    However, when I switched to point picking or line picking, the clicked event is never triggered. Any idea how to make this work? Thanks in advance.

    L 1 Reply Last reply
    0
    • P Offline
      P Offline
      poma0001
      Banned
      wrote on last edited by poma0001
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • U unclejimbo 0

        Hi there, I've been trying to do point and line picking on a mesh in Qt3D. Here's a working version of triangle picking,

        auto renderSettings = new Qt3DRender::QRenderSettings(root);
        renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
        
        auto entity = new Qt3DCore::QEntity(root);
        auto picker = new Qt3DRender::QObjectPicker;
        auto sphere = new Qt3DExtras::QSphereGeometry;
        entity->addComponent(picker);
        entity->addComponent(sphere);
        
        connect(picker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* e) {
            auto p = dynamic_cast<Qt3DRender::QPickTriangleEvent*>(e);
            auto idx = p->triangleIndex();
        });
        

        However, when I switched to point picking or line picking, the clicked event is never triggered. Any idea how to make this work? Thanks in advance.

        L Offline
        L Offline
        lcadet
        wrote on last edited by
        #3

        Hi @unclejimbo-0 ,

        in my experience, Line and Point Picking doesn't works on Qt5.9, but tested and functionnal on Qt5.12.

        The main reason why you can't pick lines or points is that yout entity is NOT a Line or a Point. Actually, your entity represent a Sphere which is composed of triangles only. Thus, no line or point picking available.

        if you want to perform Line Picking, I recommend you to start from the custom-mesh example :

        https://github.com/qt/qt3d/tree/5.12/tests/manual/custom-mesh-cpp

        and adapt it draw lines. Once vertices and indices modified, simply change the MeshRenderer primitive types to Line

        // From
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
        // To
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
        

        Then, in the render settings set

        renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::LinePicking);
        

        and add your QObjectPicker component to your custom Mesh.

        It should be working. If not working properly, try to increase the picking tolerance

        renderSettings->pickingSettings()->setWorldSpaceTolerance(10.0f); // default is 0.1f
        

        This last parameter is tricky as long as it performs tolerance checking in the world space rather than Screen space. Thus, if the camera is really close to the line, it will be easy to intersect. However, as long as you get farther, it gets difficult to pick lines due to pixel to ray resolution.

        Hope it helps.

        U 1 Reply Last reply
        0
        • L lcadet

          Hi @unclejimbo-0 ,

          in my experience, Line and Point Picking doesn't works on Qt5.9, but tested and functionnal on Qt5.12.

          The main reason why you can't pick lines or points is that yout entity is NOT a Line or a Point. Actually, your entity represent a Sphere which is composed of triangles only. Thus, no line or point picking available.

          if you want to perform Line Picking, I recommend you to start from the custom-mesh example :

          https://github.com/qt/qt3d/tree/5.12/tests/manual/custom-mesh-cpp

          and adapt it draw lines. Once vertices and indices modified, simply change the MeshRenderer primitive types to Line

          // From
          customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
          // To
          customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
          

          Then, in the render settings set

          renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::LinePicking);
          

          and add your QObjectPicker component to your custom Mesh.

          It should be working. If not working properly, try to increase the picking tolerance

          renderSettings->pickingSettings()->setWorldSpaceTolerance(10.0f); // default is 0.1f
          

          This last parameter is tricky as long as it performs tolerance checking in the world space rather than Screen space. Thus, if the camera is really close to the line, it will be easy to intersect. However, as long as you get farther, it gets difficult to pick lines due to pixel to ray resolution.

          Hope it helps.

          U Offline
          U Offline
          unclejimbo 0
          wrote on last edited by
          #4

          @lcadet Hi, thank you for your reply.

          I believe you are right. Although I ended up not doing lines or points picking, but I think your explanation is the right way to go.

          One more thing I think might be important for picking is that you need to set the vertex count properly. It's quite confusing that Qt3D can render the geometry correctly even without calling QAttribute::setCount, as long as you call QGeometryRenderer::setVertexCount. I've dug the qt3d source code a bit and I think they have a way of guessing the vertex count upon rendering, based on the counts you've provided. However, the picking process seems to merely counting on the count set in QAttribute. And if you forget to set it, the picking will fail even for triangles.

          So, that's basically my experience so far. I hope qt3d could provide better documentation and more intuitive APIs.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lcadet
            wrote on last edited by
            #5

            Hope that it could help someone.
            Did you try this strategy ? If so and if it solved your problem, could you set the topic as solved ?
            I think it could help other people who is looking for it :-)
            Regards.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jHappy
              wrote on last edited by
              #6

              I checked on Qt5.12 and 5.13.
              renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::PointPicking);
              renderSettings->pickingSettings()->setWorldSpaceTolerance(10.0f);

              But I can't pick the primitive point.

              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