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 points and lines
QtWS25 Last Chance

Qt3d points and lines

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt3ddraw lines
18 Posts 9 Posters 11.0k 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.
  • G guy incognito

    So I figuered out how to draw simple lines with the help of

    Qt3DRender::QGeometryRenderer
    Qt3DRender::QGeometry
    Qt3DRender::QAttribute

    and Vertex/Index buffer. Now I got some flat lines. I would like to give them some more "volume" to make them look like tubes.
    Anyone got an approach?

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #3

    Well, tubes are meshes, aren't they, so perhaps check out the QMesh class or the QCylinderMesh.

    Read and abide by the Qt Code of Conduct

    G 1 Reply Last reply
    1
    • kshegunovK kshegunov

      Well, tubes are meshes, aren't they, so perhaps check out the QMesh class or the QCylinderMesh.

      G Offline
      G Offline
      guy incognito
      wrote on last edited by
      #4

      @kshegunov

      I don't quite get how to make a cylinder mesh behave like a line or make a line look like a cylinder.

      kshegunovK 1 Reply Last reply
      0
      • G guy incognito

        @kshegunov

        I don't quite get how to make a cylinder mesh behave like a line or make a line look like a cylinder.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #5

        I don't understand the question. A line is a mathematical abstraction, it has no volume and no surface area (just like a point). If one needs to draw a line one uses a realistic object that represents the line - e.g. a rectangle in 2D, or a cuboid or cylinder in 3D. Also I don't understand how does a line is supposed to behave ...?

        Read and abide by the Qt Code of Conduct

        G 1 Reply Last reply
        0
        • G guy incognito

          So I figuered out how to draw simple lines with the help of

          Qt3DRender::QGeometryRenderer
          Qt3DRender::QGeometry
          Qt3DRender::QAttribute

          and Vertex/Index buffer. Now I got some flat lines. I would like to give them some more "volume" to make them look like tubes.
          Anyone got an approach?

          C Offline
          C Offline
          chris2401
          wrote on last edited by
          #6

          @guy-incognito could you share the code for drawing lines ? thanks!

          1 Reply Last reply
          0
          • kshegunovK kshegunov

            I don't understand the question. A line is a mathematical abstraction, it has no volume and no surface area (just like a point). If one needs to draw a line one uses a realistic object that represents the line - e.g. a rectangle in 2D, or a cuboid or cylinder in 3D. Also I don't understand how does a line is supposed to behave ...?

            G Offline
            G Offline
            guy incognito
            wrote on last edited by
            #7

            @kshegunov

            Let' s say i want to draw a triangle. I define the vertices p1 (0,0,0), p2 (2,0,0), and p3(1,2,0) and draw the lines between them. That's what i currently do. Now I want to draw a triangle with tubes instead of lines, so that it sort of looks like the triangle music instrument. I think thats called tubing and extrusion in OpenGL.

            It was my question if there is a convenient way in qt3d to do this. Maybe my lines could be replaced with cylinders, so the bottom surface starts at (0,0,0), the top surface ends in (2,0,0) etc. (thats what i meant with "behave like a line").

            kshegunovK 1 Reply Last reply
            0
            • G guy incognito

              @kshegunov

              Let' s say i want to draw a triangle. I define the vertices p1 (0,0,0), p2 (2,0,0), and p3(1,2,0) and draw the lines between them. That's what i currently do. Now I want to draw a triangle with tubes instead of lines, so that it sort of looks like the triangle music instrument. I think thats called tubing and extrusion in OpenGL.

              It was my question if there is a convenient way in qt3d to do this. Maybe my lines could be replaced with cylinders, so the bottom surface starts at (0,0,0), the top surface ends in (2,0,0) etc. (thats what i meant with "behave like a line").

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #8

              Look at this thread's example code. It should be enough to get you started.

              Read and abide by the Qt Code of Conduct

              G 1 Reply Last reply
              1
              • kshegunovK kshegunov

                Look at this thread's example code. It should be enough to get you started.

                G Offline
                G Offline
                guy incognito
                wrote on last edited by guy incognito
                #9

                @kshegunov

                Thanks, the x,y and z axis look exactly like what I want. The only problem is, that QTransform for the QCylincerMesh is pretty unconvenient (as far as I see).

                @chris2401

                Take a look at this code. I used it to get an idea how to create custom meshes. You can adapt it to make it draw lines instead of TetraHedrons pretty easily.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Steve Brunton
                  wrote on last edited by
                  #10

                  This has been my PITA also. I have just found an example in qt5.6 that was dropped in all further releases but it provides a working set of code that creates a custom entity with your own set of vectors / normals / colors. If you look at this in code.qt.io

                  http://code.qt.io/cgit/qt/qt3d.git/tree/examples/qt3d/custom-mesh-cpp?h=5.6

                  I wish Qt (KDAB is actually doing the Qt3D stuff) would spend a bit of time and put together some documented examples at the level of what their qGraphicsItem module was. Or at a minimum put some details to the qt function documentation. It is very frustrating to try and figure things out when there is no documentation or good functional examples on the objects we are supposed to use.

                  M 1 Reply Last reply
                  1
                  • S Steve Brunton

                    This has been my PITA also. I have just found an example in qt5.6 that was dropped in all further releases but it provides a working set of code that creates a custom entity with your own set of vectors / normals / colors. If you look at this in code.qt.io

                    http://code.qt.io/cgit/qt/qt3d.git/tree/examples/qt3d/custom-mesh-cpp?h=5.6

                    I wish Qt (KDAB is actually doing the Qt3D stuff) would spend a bit of time and put together some documented examples at the level of what their qGraphicsItem module was. Or at a minimum put some details to the qt function documentation. It is very frustrating to try and figure things out when there is no documentation or good functional examples on the objects we are supposed to use.

                    M Offline
                    M Offline
                    MatPaul
                    wrote on last edited by
                    #11

                    @Steve-Brunton
                    I am trying to draw a line in qml. I used the custom_mesh_qml for checking how it works in qt 5.9. but I encountered an error in the main.cpp file in the line 'engine.aspectEngine()->setData(data);' like setData not found. Can you help me on this. Thank you.

                    jsulmJ 1 Reply Last reply
                    0
                    • M MatPaul

                      @Steve-Brunton
                      I am trying to draw a line in qml. I used the custom_mesh_qml for checking how it works in qt 5.9. but I encountered an error in the main.cpp file in the line 'engine.aspectEngine()->setData(data);' like setData not found. Can you help me on this. Thank you.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @MatPaul Why don't you post the actual error message?
                      I guess the compiler tells you that there is no such method: setData.
                      I guess aspectEngine() returns https://doc.qt.io/qt-5/qt3dcore-quick-qqmlaspectengine.html and QQmlAspectEngine does not have setData() method.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @MatPaul Why don't you post the actual error message?
                        I guess the compiler tells you that there is no such method: setData.
                        I guess aspectEngine() returns https://doc.qt.io/qt-5/qt3dcore-quick-qqmlaspectengine.html and QQmlAspectEngine does not have setData() method.

                        M Offline
                        M Offline
                        MatPaul
                        wrote on last edited by
                        #13

                        @jsulm
                        I had used the code from this link
                        http://code.qt.io/cgit/qt/qt3d.git/tree/examples/qt3d/custom-mesh-qml?h=5.6

                        And run that on qt5.9
                        I think the setData() functionality is removed in 5.9. is there a new function which does the similar behaviour

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          AlanWasHere
                          wrote on last edited by
                          #14

                          Try this piece of code to draw lines using pipes. It assumes that your camera up vector is the Y axis:
                          double w, h, d, l, tx, ty, tz, lxz, anglex, angley;
                          w = to.GetX() - from.GetX();
                          h = to.GetY() - from.GetY();
                          d = to.GetZ() - from.GetZ();
                          l = sqrt(ww + hh + dd);
                          lxz = sqrt(w
                          w + d*d);
                          tx = from.GetX() + w/2;
                          ty = from.GetY() + h/2;
                          tz = from.GetZ() + d/2;
                          anglex = acos(h/l)*180/3.14159265;
                          angley = acos(d/lxz)180/3.14159265;
                          QVector3D QVx(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0));
                          QVector3D QVy(static_cast<float>(0), static_cast<float>(1
                          ((w<0)?-1:1)), static_cast<float>(0));

                          // Cylinder shape data
                          Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();
                          cylinder->setRadius(.005f);
                          
                          cylinder->setLength(static_cast<float>(l));
                          cylinder->setRings(100);
                          cylinder->setSlices(20);
                          
                          // CylinderMesh Transform
                          Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
                          cylinderTransform->setScale(1.0f);
                          cylinderTransform->setRotation(Qt3DCore::QTransform::fromAxesAndAngles(QVx,static_cast<float>(anglex), QVy, static_cast<float>(angley)));
                          cylinderTransform->setTranslation(QVector3D(static_cast<float>(tx), static_cast<float>(ty), static_cast<float>(tz)));
                          
                          Qt3DExtras::QDiffuseSpecularMaterial *cylinderMaterial = new Qt3DExtras::QDiffuseSpecularMaterial();
                          cylinderMaterial->setDiffuse(QColor(QRgb(0xffffff)));
                          
                          // Cylinder
                          m_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);
                          m_cylinderEntity->addComponent(cylinder);
                          m_cylinderEntity->addComponent(cylinderMaterial);
                          m_cylinderEntity->addComponent(cylinderTransform);
                          

                          }

                          halimaH 1 Reply Last reply
                          0
                          • A AlanWasHere

                            Try this piece of code to draw lines using pipes. It assumes that your camera up vector is the Y axis:
                            double w, h, d, l, tx, ty, tz, lxz, anglex, angley;
                            w = to.GetX() - from.GetX();
                            h = to.GetY() - from.GetY();
                            d = to.GetZ() - from.GetZ();
                            l = sqrt(ww + hh + dd);
                            lxz = sqrt(w
                            w + d*d);
                            tx = from.GetX() + w/2;
                            ty = from.GetY() + h/2;
                            tz = from.GetZ() + d/2;
                            anglex = acos(h/l)*180/3.14159265;
                            angley = acos(d/lxz)180/3.14159265;
                            QVector3D QVx(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0));
                            QVector3D QVy(static_cast<float>(0), static_cast<float>(1
                            ((w<0)?-1:1)), static_cast<float>(0));

                            // Cylinder shape data
                            Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();
                            cylinder->setRadius(.005f);
                            
                            cylinder->setLength(static_cast<float>(l));
                            cylinder->setRings(100);
                            cylinder->setSlices(20);
                            
                            // CylinderMesh Transform
                            Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
                            cylinderTransform->setScale(1.0f);
                            cylinderTransform->setRotation(Qt3DCore::QTransform::fromAxesAndAngles(QVx,static_cast<float>(anglex), QVy, static_cast<float>(angley)));
                            cylinderTransform->setTranslation(QVector3D(static_cast<float>(tx), static_cast<float>(ty), static_cast<float>(tz)));
                            
                            Qt3DExtras::QDiffuseSpecularMaterial *cylinderMaterial = new Qt3DExtras::QDiffuseSpecularMaterial();
                            cylinderMaterial->setDiffuse(QColor(QRgb(0xffffff)));
                            
                            // Cylinder
                            m_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);
                            m_cylinderEntity->addComponent(cylinder);
                            m_cylinderEntity->addComponent(cylinderMaterial);
                            m_cylinderEntity->addComponent(cylinderTransform);
                            

                            }

                            halimaH Offline
                            halimaH Offline
                            halima
                            wrote on last edited by
                            #15

                            @AlanWasHere
                            Hello,
                            what do you mean by : static_cast<float>(1((w<0)?-1:1)) please?

                            jsulmJ 1 Reply Last reply
                            0
                            • halimaH halima

                              @AlanWasHere
                              Hello,
                              what do you mean by : static_cast<float>(1((w<0)?-1:1)) please?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @halima What exactly are you asking?
                              static_cast casts from one type to another:

                              static_cast<T>(W) - this casts from W to T
                              

                              So, in this case it casts from int to float.
                              Or do you want to know what

                              (w<0)?-1:1)
                              

                              means?
                              If so then see https://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              halimaH 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @halima What exactly are you asking?
                                static_cast casts from one type to another:

                                static_cast<T>(W) - this casts from W to T
                                

                                So, in this case it casts from int to float.
                                Or do you want to know what

                                (w<0)?-1:1)
                                

                                means?
                                If so then see https://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm

                                halimaH Offline
                                halimaH Offline
                                halima
                                wrote on last edited by
                                #17

                                @jsulm ok I got my answer, Thanks :D

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  Alexandr Sudnik
                                  wrote on last edited by
                                  #18

                                  Hi! I wrote resolution like @AlanWasHere, but a little clearer

                                  void addLine(QEntity *parentEntity, const QVector3D &srcPos, const QVector3D &targPos)
                                  {
                                      auto edgeEntity = new QEntity{parentEntity};
                                  
                                      auto cylinder = new QCylinderMesh{edgeEntity};
                                  
                                      auto len = srcPos.distanceToPoint(targPos);
                                      cylinder->setLength(len);
                                      cylinder->setRadius(0.1f);
                                  
                                      auto transPoint = targPos - srcPos;
                                      auto xAngle = atan(sqrt(pow(transPoint.z(), 2) + pow(transPoint.x(), 2)) / transPoint.y()) / M_PI * 180;
                                      auto yAngle = (transPoint.x() == 0 && transPoint.z() == 0) ? 0 : atan(transPoint.x() / transPoint.z()) / M_PI * 180;
                                  
                                      auto transform = new Qt3DCore::QTransform{edgeEntity};
                                      transform->setRotationX(xAngle);
                                      transform->setRotationY(yAngle);
                                      transform->setTranslation((srcPos + targPos) / 2);
                                  
                                      auto material = new QPhongMaterial{edgeEntity};
                                      material->setDiffuse("#ffff00");
                                  
                                      edgeEntity->addComponent(cylinder);
                                      edgeEntity->addComponent(transform);
                                      edgeEntity->addComponent(material);
                                  }
                                  
                                  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