QML/Qt3D GeometryRenderer 使用的基本示例
QML/Qt3D Basic example of GeometryRenderer use
我正在寻找有关如何在 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]
}
问题如下:Buffer 对象的 data
属性 实际上需要 QByteArray。然而,QByteArray 不是 QML 类型,简单地在列表中写入顶点是行不通的。
解决方案是编写一个 C++ 包装器,然后 made available to QML。这个包装器应该至少为 QBuffer 或直接为 QByteArray 提供可读的 属性。
然后可以使用 C++ 或 QML 设置包装器的数据(提供一些写入功能,see here)。
下面的代码是一个基本的例子,我从那里改编了它,它在功能方面更完整。
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 中使用 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]
}
问题如下:Buffer 对象的 data
属性 实际上需要 QByteArray。然而,QByteArray 不是 QML 类型,简单地在列表中写入顶点是行不通的。
解决方案是编写一个 C++ 包装器,然后 made available to QML。这个包装器应该至少为 QBuffer 或直接为 QByteArray 提供可读的 属性。
然后可以使用 C++ 或 QML 设置包装器的数据(提供一些写入功能,see here)。
下面的代码是一个基本的例子,我从
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; }