How do I draw a simple line in Qt3D?
Asked Answered
G

3

9

I feel like this should be pretty straightforward, but for my life I can't figure out how to draw a basic line using Qt 3D. The only guidance I've been able to find on the subject is this obscure video, in which there's an off-putting amount of raw byte buffer and memory manipulation going on via scarcely documented classes.

Is there a better way to do this using the shiny new API that I'm missing?

Godevil answered 28/6, 2016 at 5:38 Comment(0)
C
18

From the video you linked, I came up with the code below (also posted in Qt forums: https://forum.qt.io/topic/66808/qt3d-draw-grid-axis-lines/3).

First, you need to create your QGeometry. As it is a simple line, it is only composed of 2 vertices (start point, end point), and 2 indices (that links the vertices). To do so, you need to create 2 QByteArray and store them into a QBuffer. In the first one, you store the 2 vertices (x, y, and z coordinates for each). In the second, you just say that you want to link your first vertex to the second. As we use Qt3DRender::QGeometryRenderer::Lines on the renderer, only 2 indices are required.

Once it is done, you just have to put your QGeometry in a QGeometryRenderer to have a mesh, and put the mesh in a QEntity so it appears in the tree and it is rendered.

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>

void drawLine(const QVector3D& start, const QVector3D& end, const QColor& color, Qt3DCore::QEntity *_rootEntity)
{
    auto *geometry = new Qt3DRender::QGeometry(_rootEntity);

    // position vertices (start and end)
    QByteArray bufferBytes;
    bufferBytes.resize(3 * 2 * sizeof(float)); // start.x, start.y, start.end + end.x, end.y, end.z
    float *positions = reinterpret_cast<float*>(bufferBytes.data());
    *positions++ = start.x();
    *positions++ = start.y();
    *positions++ = start.z();
    *positions++ = end.x();
    *positions++ = end.y();
    *positions++ = end.z();

    auto *buf = new Qt3DRender::QBuffer(geometry);
    buf->setData(bufferBytes);

    auto *positionAttribute = new Qt3DRender::QAttribute(geometry);
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
    positionAttribute->setVertexSize(3);
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(buf);
    positionAttribute->setByteStride(3 * sizeof(float));
    positionAttribute->setCount(2);
    geometry->addAttribute(positionAttribute); // We add the vertices in the geometry

    // connectivity between vertices
    QByteArray indexBytes;
    indexBytes.resize(2 * sizeof(unsigned int)); // start to end
    unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
    *indices++ = 0;
    *indices++ = 1;

    auto *indexBuffer = new Qt3DRender::QBuffer(geometry);
    indexBuffer->setData(indexBytes);

    auto *indexAttribute = new Qt3DRender::QAttribute(geometry);
    indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
    indexAttribute->setBuffer(indexBuffer);
    indexAttribute->setCount(2);
    geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry

    // mesh
    auto *line = new Qt3DRender::QGeometryRenderer(_rootEntity);
    line->setGeometry(geometry);
    line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
    auto *material = new Qt3DExtras::QPhongMaterial(_rootEntity);
    material->setAmbient(color);

    // entity
    auto *lineEntity = new Qt3DCore::QEntity(_rootEntity);
    lineEntity->addComponent(line);
    lineEntity->addComponent(material);
}
Columbite answered 24/7, 2018 at 10:21 Comment(5)
Tried your code an it works, well done. Just a small typo: In the function signature you wrote Q3DCore::QEntity *_rootEntity but the t is missing in Qt3D ;)Onomatology
Q3DCore::QEntity should be Qt3DCore::QEntityLucianaluciano
I tried this code to draw a million lines and it crashes. Is there something I should change ?Lillie
This code no longer renders lines using Qt6 on MacOS. (Qt5 is okay). Does anyone know if metal no longer supports rasterised lines? Since I know the graphics backend changed from Qt5 to Qt6Businessman
I want to add some changes that occur if you use QT6.2. You need to change the following namespaces: Qt3DRender/QAttribute -> Qt3DCore/QAttribute; Qt3DRender/QBuffer -> Qt3DCore/QBuffer; Qt3DRender/QGeometry -> Qt3DCore/QGeometry; Also you need to change the namespaces in the code below to the proper namespace. And you need to include Qt3DRender.Palatal
D
1

Here's the correction I had to do on buffer type (I assume its deprecated now), respectively:

auto *buf = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, geometry);
auto *indexBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, geometry);

Mine is Qt 5.9 on Win 10.

Dyarchy answered 21/2, 2019 at 11:56 Comment(0)
E
-4

I'd suggest looking at https://doc-snapshots.qt.io/qt5-5.9/qt3d-basicshapes-cpp-example.html , as a while ago I asked myself a similar question, namely, how to draw a circle. Well, a circle in 3D is a torus with a special proportion of its radii:

// thin Torus = Circle in 3D
Qt3DCore::QEntity *torusEntity0 = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QTorusMesh *torusMesh0 = new Qt3DExtras::QTorusMesh;
torusMesh0->setRadius(15);
torusMesh0->setMinorRadius(0.01f);
torusMesh0->setRings(100);
torusMesh0->setSlices(20);
torusEntity0->addComponent(torusMesh0);
torusEntity0->addComponent(material);

So what would be a line in 3D? It would be a cylinder with a very small outer radius.

Erect answered 1/5, 2017 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.