问题
I'm looking for a basic example of how to set up an Entity with a GeometryRenderer in QML.
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]
}
回答1:
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; }
来源:https://stackoverflow.com/questions/47296979/qml-qt3d-basic-example-of-geometryrenderer-use