QML / Qt3D GeometryRenderer使用的基本示例

时间:2017-11-14 23:27:05

标签: qt opengl qml qt3d

我正在寻找如何在QML中使用GeometryRenderer设置实体的基本示例。

我现在使用的代码如下所示。如果我将geometryRenderer替换为CuboidMesh,则会显示空白多维数据集。目标是显示一个简单的三角形。我的GeometryRenderer声明是否正确?

Entity{

    Material{
      id: simpleMaterial 
      effect: SimpleEffect{}
    }

    GeometryRenderer{
      id: geometryRenderer

    instanceCount: 1
    primitiveType: GeometryRenderer.Triangles
    geometry: Geometry{

            Attribute{
                attributeType: Attribute.VertexAttribute
                vertexBaseType: Attribute.Float
                vertexSize: 3
                byteOffset: 0
                byteStride: 3 * 4
                count: 3

                buffer : Buffer{
                    id: vertexBuffer
                    type: Buffer.VertexBuffer
                    data: [ 0.0, 0.0, 0.0,
                            0.0, 10.0, 0.0,
                            10.0, 10.0, 0.0]
                }
            }
        }
    }

components: [simpleMaterial, geometryRenderer]
}

1 个答案:

答案 0 :(得分:4)

问题如下:Buffer对象的data属性实际上需要QByteArray。但是,QByteArray不是QML类型,只需将顶点写入列表就不会有诀窍。

解决方案是编写一个C ++包装器,然后是made available to QML。此包装器应至少为QBuffer或QByteArray提供可读属性。

然后可以使用C ++或QML设置包装器的数据(提供一些写入功能,see here)。

以下代码是一个基本示例,我从this answer对其进行了调整,在功能方面它更完整。

Entity{

    property DrawData drawData

    Material{
        id: material
        ...
    }

    GeometryRenderer{
        id: geometryRenderer

        instanceCount: drawData.count
        primitiveType: GeometryRenderer.TriangleStrip

        geometry: Geometry{

            Attribute{
                name: "vertexPosition" // Name of attribute in the shader
                attributeType: Attribute.VertexAttribute
                vertexBaseType: Attribute.Float
                vertexSize: 3
                byteOffset: 0 // See OpenGL doc for details about these properties
                byteStride: 3 * 4
                count: drawData.count
                buffer : drawData.buffer // The actual QBuffer which holds the vertex data
            }
        }
    }

    components: [material, geometryRenderer]
}

DrawData的定义如下:

/* 
 * This struct is not strictly necessary, it is just convenient in case more
 * vertex attributes are needed. If so, it is necessary to change the ByteStride 
 * in the geometry attribute. 
 */ 
struct DrawVBOData {
QVector3D position;
};

class DrawData : public Qt3DCore::QNode {
    Q_OBJECT
    // Make properties available in QML
    Q_PROPERTY(Qt3DRender::QBuffer *buffer READ buffer CONSTANT)
    Q_PROPERTY(int count READ count NOTIFY countChanged)

public:
    explicit DrawData (Qt3DCore::QNode *parent = 0);
    Qt3DRender::QBuffer *buffer();
    void setData(const QVector<QVector3D> &positions);
    int count() const;

signals:
    void countChanged(int count);

public slots:

private:
    QScopedPointer<Qt3DRender::QBuffer> m_buffer;
    int m_count = 0;
};

最后,实施:

DrawData::DrawData(Qt3DCore::QNode *parent)
    : Qt3DCore::QNode(parent), m_buffer(new QBuffer(QBuffer::VertexBuffer, this)){}

Qt3DRender::QBuffer *DrawData::buffer() { return m_buffer.data();}

void DrawData::setData(const QVector<QVector3D> &positions) {
    QByteArray ba;
    ba.resize(positions.size() * sizeof(DrawVBOData));
    DrawVBOData *vboData = reinterpret_cast<DrawVBOData *>(ba.data());
    for (int i = 0; i < positions.size(); i++) {
        DrawVBOData &vbo = vboData[i];
        vbo.position = positions[i];
    }
    m_buffer->setData(ba);
    m_count = positions.count();
    emit countChanged(m_count);
}

int DrawData::count() const { return m_count; }