QML/Qt3D GeometryRenderer 使用基本示例 [英] QML/Qt3D Basic example of GeometryRenderer use

查看:45
本文介绍了QML/Qt3D GeometryRenderer 使用基本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a basic example of how to set up an Entity with a GeometryRenderer in QML.

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

The code I'm using right now is shown below. If I replace geometryRenderer by a CuboidMesh then a blank cube is shown. The goal is to display a simple triangle. Is my GeometryRenderer declaration correct ?

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

推荐答案

问题如下:Buffer 对象的 data 属性实际上需要一个 QByteArray.但是,QByteArray 不是 QML 类型,仅将顶点写入列表是行不通的.

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.

解决方案是编写一个 C++ 包装器,然后 提供到 QML.这个包装器应该至少为 QBuffer 或直接为 QByteArray 提供可读属性.

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.

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

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

以下代码是一个基本示例,我改编自 this answer 其中它在功能方面更加完善.

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

其中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; }

这篇关于QML/Qt3D GeometryRenderer 使用基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆