Search code examples
c++qtopenglqmlqtquick3d

How to draw 3D lines onto a QML scene?


I try to integrate Bullet Physics' debug drawing interface into QML, so I have to implement a drawLine() method.

void drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color);

What I tried is that I inherited an item, that is used in the scene, from both QQuickItem3D and btIDebugDraw. In drawLine(), I add the lines to a member vector. In Qt's drawItem(), I iterate over the lines and use OpenGL calls to render them. However, they do not appear on screen.

How can I draw the lines in 3D space and from the correct camera view?

void DebugDrawer::drawItem(QGLPainter *painter)
{
    if (lines_.size() < 1)
        return;

    // Draw current lines
    painter->modelViewMatrix().push();
    glBegin(GL_LINES);
    for (auto &line : lines_) {
        glColor3f(line.color.getX(), line.color.getY(), line.color.getZ());
        glVertex3f(line.from.getX(), line.from.getY(), line.from.getZ());
        glVertex3f(line.to.getX(), line.to.getY(), line.to.getZ());
    }
    glEnd();
    painter->modelViewMatrix().pop();

    // Reset buffer
    lines_.clear();
}

Solution

  • I ended up using QtQuick's line class and set its vertices using setVertices() in Bullet's flushLines() method.