QQuick3DGeometry 类
用于定义自定义几何形状的基本类。 更多...
| 头文件 | #include <QQuick3DGeometry> | 
| 由以下类实例化 | 几何 | 
| 继承自 | QQuick3DObject | 
公共函数
| void | addAttribute(QQuick3DGeometry::Attribute::Semantic semantic, int offset, QQuick3DGeometry::Attribute::ComponentType componentType) | 
| void | addAttribute(const QQuick3DGeometry::Attribute &attribute) | 
| void | addSubset(int offset, int count, const QVector3D &boundsMin, const QVector3D &boundsMax, const QString &name = {}) | 
| (since 6.6)void | addTargetAttribute(quint32 targetId, QQuick3DGeometry::Attribute::Semantic semantic, int offset, int stride = 0) | 
| (since 6.6)void | addTargetAttribute(const QQuick3DGeometry::TargetAttribute &attribute) | 
| QQuick3DGeometry::Attribute | attribute(int index) const | 
| int | attributeCount() const | 
| QVector3D | boundsMax() const | 
| QVector3D | boundsMin() const | 
| void | clear() | 
| QByteArray | indexData() const | 
| QQuick3DGeometry::PrimitiveType | primitiveType() const | 
| void | setBounds(const QVector3D &min, const QVector3D &max) | 
| void | setIndexData(const QByteArray &data) | 
| void | setIndexData(int offset, const QByteArray &data) | 
| void | setPrimitiveType(QQuick3DGeometry::PrimitiveType type) | 
| void | setStride(int stride) | 
| (since 6.6)void | setTargetData(const QByteArray &data) | 
| (since 6.6)void | setTargetData(int offset, const QByteArray &data) | 
| void | setVertexData(const QByteArray &data) | 
| void | setVertexData(int offset, const QByteArray &data) | 
| int | stride() const | 
| QVector3D | subsetBoundsMax(int subset) const | 
| QVector3D | subsetBoundsMin(int subset) const | 
| int | subsetCount() const | 
| int | subsetCount(int subset) const | 
| QString | subsetName(int subset) const | 
| int | subsetOffset(int subset) const | 
| (自 6.6)QQuick3DGeometry::TargetAttribute | targetAttribute(int index) const | 
| (自 6.6)int | targetAttributeCount() const | 
| (自 6.6)QByteArray | targetData() const | 
| QByteArray | vertexData() const | 
详细描述
QQuick3DGeometry可用于指定Qt Quick 3D场景中模型的自定义几何形状。
虽然不是强制的,但典型用法是继承这个类。然后通过在类型系统中注册这个子类来将其暴露给QML。然后可以将Model的geometry属性设置为引用已注册类型的实例。
此类的高级结构通常如下
class CustomGeometry : public QQuick3DGeometry { public: CustomGeometry() { rebuildGeometry(); } void setSomething() { // Change relevant internal data. // ... // Then rebuild the vertex and index data and pass it to QQuick3DGeometry. rebuildGeometry(); // Finally, trigger an update. This is relevant in case nothing else // is changing in the scene; this way we make sure a new frame will // be rendered. update(); } private: void rebuildGeometry() { QByteArray vertices; QByteArray indices; ... setPrimitiveType(Lines); setVertexBuffer(vertices); setIndexBuffer(indices); setStride(3 * sizeof(float)); // e.g. when having 3 components per vertex setBounds(...); // minimum and maximum extents, for picking addAttribute(PositionSemantic, 0, F32Type); ... } };
此类可以被注册为QML类型,并与Model一起使用。
在Qt 5中,类型注册是通过qmlRegisterType发生的。
qmlRegisterType<CustomGeometry>("Example", 1, 0, "CustomGeometry");
在Qt 6中,默认方法是通过构建系统的帮助使用自动注册。不再调用qmlRegisterType,现在.pro文件可以包含
CONFIG += qmltypes QML_IMPORT_NAME = Example QML_IMPORT_MAJOR_VERSION = 1
使用CMake,自动注册是默认行为,因此除了基本的QML模块设置外无需特殊设置
qt_add_qml_module(application
    URI Example
    VERSION 1.0
)类实现应添加QML_NAMED_ELEMENT
class CustomGeometry : public QQuick3DGeometry { Q_OBJECT QML_NAMED_ELEMENT(CustomGeometry) ... };
然后QML代码可以使用自定义类型
import Example 1.0
Model {
    id: customModel
    geometry: CustomGeometry {
    }
}至少,自定义几何形状应指定以下内容
- 顶点数据,
- 顶点步长,
- 原语类型,
- 带有PositionSemantic的属性。
这些足以渲染网格。对于索引绘制,还需要指定索引缓冲区数据和带有IndexSemantic的属性。为了支持拾取(输入),类必须使用setBounds()指定边界体积。为了正确的光照,需要一个带有NormalSemantic的属性。当材质使用纹理时,至少需要提供一套UV坐标并描述为TexCoord0Semantic或TexCoord1Semantic属性。一些材质还可能需要切向量和二法向量。
作为具体的、最小的例子,以下类将提供一个三角形的几何形状
class ExampleGeometry : public QQuick3DGeometry { Q_OBJECT QML_NAMED_ELEMENT(ExampleGeometry) public: ExampleGeometry(); private: void updateData(); }; ExampleGeometry::ExampleGeometry() { updateData(); } void ExampleGeometry::updateData() { QByteArray v; v.resize(3 * 3 * sizeof(float)); float *p = reinterpret_cast<float *>(v.data()); // a triangle, front face = counter-clockwise *p++ = -1.0f; *p++ = -1.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = -1.0f; *p++ = 0.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f; setVertexData(v); setStride(3 * sizeof(float)); setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles); addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0, QQuick3DGeometry::Attribute::F32Type); }
依赖于场景中的光照,从Model引用这个几何形状的结果

注意:顶点数据预计将遵循OpenGL约定。这意味着数据必须提供,假设Y轴是指向上方的标准化设备坐标系,顺时针为前面对象的顺时针旋转。
成员函数文档
void QQuick3DGeometry::addAttribute(QQuick3DGeometry::Attribute::Semantic semantic, int offset, QQuick3DGeometry::Attribute::ComponentType componentType)
添加顶点属性描述。每个属性都有一个语义,该语义指定了属性的用途以及它拥有的组件数,一个偏移量,即从顶点开始到属性的位置,以及一个组件类型,该类型指定了属性的数据类型和大小。
语义可以是以下之一
| 常数 | 说明 | 
|---|---|
| PositionSemantic | 属性表示位置。3个组件:x、y和z | 
| NormalSemantic | 该属性是一个法向量。3个分量:x、y和z | 
| TexCoord0Semantic | 该属性是一个纹理坐标。2个分量:u和v | 
| TexCoord1Semantic | 该属性是一个纹理坐标。2个分量:u和v | 
| TangentSemantic | 该属性是一个切向量。3个分量:x、y和z | 
| BinormalSemantic | 该属性是一个双法向量。3个分量:x、y和z | 
| JointSemantic | 该属性是用于皮肤变形的关节索引向量。4个分量:关节索引1-4 | 
| WeightSemantic | 该属性是用于皮肤变形的权重向量。4个分量:关节权重1-4 | 
| ColorSemantic | 该属性是顶点颜色向量。4个分量:r、g、b和a | 
| TargetPositionSemantic | 该属性是第一个变形目标的位置。3个分量:x、y和z | 
| TargetNormalSemantic | 该属性是第一个变形目标的法向量。3个分量:x、y和z | 
| TargetTangentSemantic | 该属性是第一个变形目标的切向量。3个分量:x、y和z | 
| TargetBinormalSemantic | 该属性是第一个变形目标的双法向量。3个分量:x、y和z | 
此外,语义可以是IndexSemantic。在这种情况下,该属性不表示顶点缓冲区中的条目,而是描述索引缓冲区中的索引数据。由于每个顶点只有一个索引,对于索引缓冲区来说,偏移量没有意义,应保持为零。
组件类型可以是以下之一
| 常数 | 说明 | 
|---|---|
| U16Type | 索引组件类型是无符号16位整数。仅支持 IndexSemantic。 | 
| U32Type | 属性(或索引组件)是无符号32位整数。 | 
| I32Type | 属性是32位有符号整数。请注意,旧版本的OpenGL(例如2.1或OpenGL ES 2.0)可能不支持此数据类型。 | 
| F32Type | 属性是单精度浮点数。 | 
注意: 关节索引数据通常是I32Type。为了能够与不支持整型顶点输入属性的API(如OpenGL ES 2.0)兼容,也支持F32Type。
注意: 对于索引数据(IndexSemantic),只有U16Type和U32Type是有意义的且得到支持。
注意: 目标和XXXSemantics将要废弃。可以使用addTargetAttribute用于变形目标。现在这些语义只是为了向后兼容而支持。如果它们与addTargetAttribute和setTargetData混合使用,则结果无法保证。
void QQuick3DGeometry::addAttribute(const QQuick3DGeometry::Attribute &attribute)
这是一个重载函数。
添加顶点属性描述。每个属性都有一个语义,用于指定属性的用途及其分量的数量,一个从顶点开始到属性的偏移量,以及一个componentType指定了属性的数据类型和大小。
void QQuick3DGeometry::addSubset(int offset, int count, const QVector3D &boundsMin, const QVector3D &boundsMax, const QString &name = {})
向几何形状添加新的子集。子集允许使用不同的材质渲染几何形状的各个部分。材质在模型中指定。
如果几何形状有索引缓冲区,则偏移量和计数是指在该子集中原始偏移和索引的计数。如果几何形状仅包含顶点缓冲区,则偏移量是顶点偏移,计数是该子集中的顶点数目。
边界边界最小值和边界最大值应包围该子集,就像几何形状的边界一样。子集还可以有一个名称。
[since 6.6] void QQuick3DGeometry::addTargetAttribute(quint32 targetId, QQuick3DGeometry::Attribute::Semantic semantic, int offset, int stride = 0)
添加形态目标属性描述。每个属性都有一个目标Id,属性所属的目标,一个语义,用于指定属性的用途及其具有的分量数,以及从顶点开始到顶点内部属性位置的开头处的偏移量,以及元素之间的字节数。
注意:目标Id应从0开始递增,且不允许跳过任何编号,所有目标应具有相同的属性。
注意:语义与顶点属性相同,但不允许在目标属性中使用索引语义、关节语义和权重语义。
注意:所有目标属性的数据类型必须是F32Type。
注意:如果未提供步进值或步进值小于或等于零,则认为属性是紧密打包的。
此功能自Qt 6.6引入。
另请参阅:addAttribute。
[since 6.6] void QQuick3DGeometry::addTargetAttribute(const QQuick3DGeometry::TargetAttribute &attribute)
这是一个重载函数。
添加形态目标属性描述。每个属性都有一个目标Id,属性所属的目标,一个语义,用于指定属性的用途及其具有的分量数,以及从顶点开始到顶点内部属性位置的开头处的偏移量,以及元素之间的字节数。
此功能自Qt 6.6引入。
QQuick3DGeometry::Attribute QQuick3DGeometry::attribute(int index) const
返回属性定义编号索引。
属性定义从0开始编号,到 attributeCount() - 1。
int QQuick3DGeometry::attributeCount() const
返回为该几何形状定义的属性数目。
另请参阅:attribute。
QVector3D QQuick3DGeometry::boundsMax() const
返回边界体积的最大坐标。
另请参阅:setBounds。
QVector3D QQuick3DGeometry::boundsMin() const
返回边界体积的最小坐标。
另请参阅:setBounds。
void QQuick3DGeometry::clear()
重置几何形状到初始状态,清除之前设定的顶点和索引数据以及属性。
QByteArray QQuick3DGeometry::indexData() const
返回索引缓冲区数据。
另请参阅setIndexData。
QQuick3DGeometry::PrimitiveType QQuick3DGeometry::primitiveType() const
返回用于渲染的原始类型。默认值为三角形。
另请参阅setPrimitiveType。
void QQuick3DGeometry::setBounds(const QVector3D &min, const QVector3D &max)
将几何形状的边界体积设置为点min和max定义的立方体。这用于选择。
void QQuick3DGeometry::setIndexData(const QByteArray &data)
将索引缓冲区设置为data。要使用索引绘制,添加一个具有IndexSemantic的属性
另请参阅indexData()和addAttribute。
void QQuick3DGeometry::setIndexData(int offset, const QByteArray &data)
这是一个重载函数。
更新索引缓冲区的一部分。offset指定字节偏移量,data指定大小和数据。
此函数不会调整缓冲区大小。如果offset + data.size()大于当前缓冲区的大小,则超出的数据将被忽略。
注意:顶点、索引和形变目标数据的部分更新函数不对内部实现方式提供任何保证。根据底层实现,即使在某种程度上进行了部分更改,也可能导致更新整个图形资源。
void QQuick3DGeometry::setPrimitiveType(QQuick3DGeometry::PrimitiveType type)
将用于渲染的原始类型设置为type。
| 常数 | 说明 | 
|---|---|
| 点 | 原始形状是点。 | 
| 线段 | 原始形状是线段中的线。 | 
| 线条 | 原始形状是线条列表。 | 
| 三角形条带 | 原始形状是条带中的三角形。 | 
| 三角形扇 | 原始形状是扇中的三角形。请注意,根据底层图形API,三角形扇可能在运行时不受支持。 | 
| 三角形 | 原始形状是列表中的三角形。 | 
初始值是三角形。
注意:根据底层图形API,三角形扇(TriangleFan)可能在运行时不受支持。例如,在Direct 3D中,这种拓扑结构根本不起作用。
注意:点的大小由材质控制。但是请注意,根据底层图形API,可能不支持除1之外的大小。
另请参阅primitiveType。
void QQuick3DGeometry::setStride(int stride)
设置顶点缓冲区的步幅为 步幅(以字节为单位)。这是缓冲区中连续两个顶点之间的距离。
例如,对于使用 PositionSemantic、IndexSemantic 和 ColorSemantic 的紧密排列、交错顶点缓冲区,步幅为 28(总共有七个浮点数:位置三个,颜色四个,索引没有,索引不在顶点缓冲区中。)
注意:QQuick3DGeometry 仅支持并期望具有交错属性布局的顶点数据。
另请参阅:stride() 和 addAttribute。
[since 6.6] void QQuick3DGeometry::setTargetData(const QByteArray &data)
设置形变目标缓冲区 data。该缓冲区应包含所有形变目标数据。
此功能自Qt 6.6引入。
另请参阅:targetData 和 addTargetAttribute。
[since 6.6] void QQuick3DGeometry::setTargetData(int offset, const QByteArray &data)
这是一个重载函数。
更新形变目标缓冲区的子集。 offset 指定字节偏移量,data 指定大小和数据。
此函数不会调整缓冲区大小。如果offset + data.size()大于当前缓冲区的大小,则超出的数据将被忽略。
注意:顶点、索引和形变目标数据的部分更新函数不对内部实现方式提供任何保证。根据底层实现,即使在某种程度上进行了部分更改,也可能导致更新整个图形资源。
此功能自Qt 6.6引入。
void QQuick3DGeometry::setVertexData(const QByteArray &data)
设置顶点缓冲区 data。缓冲区应包含由属性定义描述的数组中打包的所有顶点数据。请注意,这不包括属于索引缓冲区的具有 IndexSemantic 的属性。
另请参阅:vertexData、addAttribute、setStride 和 setIndexData。
void QQuick3DGeometry::setVertexData(int offset, const QByteArray &data)
这是一个重载函数。
更新顶点缓冲区的子集。 offset 指定字节偏移量,data 指定大小和数据。
此函数不会调整缓冲区大小。如果offset + data.size()大于当前缓冲区的大小,则超出的数据将被忽略。
注意:顶点、索引和形变目标数据的部分更新函数提供的内部实现保证较弱。根据底层实现,即使部分更改也可能导致更新整个图形资源。
int QQuick3DGeometry::stride() const
返回顶点缓冲区的字节步幅。
另请参阅:setStride。
QVector3D QQuick3DGeometry::subsetBoundsMax(int subset) const
返回子集的最大边界数。
另请参阅:subsetBoundsMin。
QVector3D QQuick3DGeometry::subsetBoundsMin(int subset) const
返回子集的最小边界数。
另请参阅:subsetBoundsMax。
int QQuick3DGeometry::subsetCount() const
返回子集数量。
int QQuick3DGeometry::subsetCount(int subset) const
返回子集原语数量。
另请参阅subsetOffset.
QString QQuick3DGeometry::subsetName(int subset) const
返回子集名称。
int QQuick3DGeometry::subsetOffset(int subset) const
返回子集到顶点或索引缓冲区的偏移量。
另请参阅subsetCount.
[since 6.6] QQuick3DGeometry::TargetAttribute QQuick3DGeometry::targetAttribute(int index) const
返回形态目标属性定义的索引 index
属性定义从0开始编号,到 attributeCount() - 1。
此功能自Qt 6.6引入。
[since 6.6] int QQuick3DGeometry::targetAttributeCount() const
返回为此几何形状定义的形态目标属性数量。
此功能自Qt 6.6引入。
另请参阅targetAttribute.
[since 6.6] QByteArray QQuick3DGeometry::targetData() const
返回由 setTargetData 设置的目标缓冲区数据集。
此功能自Qt 6.6引入。
另请参阅setTargetData().
QByteArray QQuick3DGeometry::vertexData() const
返回由 setVertexData 设置的顶点缓冲区数据。
另请参阅setVertexData().
© 2024 Qt公司有限公司。本文档的编纂贡献是其各自所有者的版权。提供的文档按照自由软件基金会发布的 GNU自由文档许可证版本1.3 的条款获得许可。Qt及其相应标志是芬兰和/或其他国家的Qt公司有限公司的商标。所有其他商标是各自所有者的财产。