QML/Qt3D Basic example of GeometryRenderer use

人盡茶涼 提交于 2019-12-05 09:23:21

The problem was the following: the data property of the Buffer object actually expects a QByteArray. However, QByteArray is not a QML type and simply writing the vertices in a list won't do the trick.

The solution is to write a C++ wrapper which is then made available to QML. This wrapper should offer at least a readable property to a QBuffer or directly a QByteArray.

It is then possible to set the wrapper's data using either C++ or QML (provided some write function, see here).

The following code is a basic example, I adapted it from this answer where it is more complete in terms of functionality.

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]
}

Where DrawData is defined as follows:

/* 
 * 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;
};

And finally, the implementation:

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; }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!