gpt4 book ai didi

qt - QML/Qt3D GeometryRenderer 使用的基本示例

转载 作者:行者123 更新时间:2023-12-02 11:23:22 36 4
gpt4 key购买 nike

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

我现在使用的代码如下所示。如果我用 CuboidMesh 替换 geometryRenderer ,则会显示一个空白立方体。目标是显示一个简单的三角形。我的 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]
}

最佳答案

问题如下: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; }

关于qt - QML/Qt3D GeometryRenderer 使用的基本示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47296979/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com