如何在 QML 场景上绘制 3D 线条

How to draw 3D lines onto a QML scene?

本文关键字:绘制 3D 线条 QML      更新时间:2023-10-16

我尝试将Bullet Physics的调试绘图接口集成到QML中,因此我必须实现一个drawLine()方法。

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

尝试的是,我从QQuickItem3D和btIDebugDraw继承了一个在场景中使用的项目。在drawLine()中,我将线添加到成员向量中。在Qt的drawItem()中,我迭代这些行并使用OpenGL调用来渲染它们。但是,它们不会出现在屏幕上。

如何在 3D 空间和正确的摄像机视图中绘制线条?

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();
}

我最终使用了QtQuick的line类,并使用Bullet的flushLines()方法中的setVertices()设置了它的顶点。