QSGGeometry Class

QSGGeometry 类为Qt Quick 场景图中的图形基元提供底层存储。更多

Header: #include <QSGGeometry>
CMake: find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)
qmake: QT += quick

公共类型

struct Attribute
struct AttributeSet
struct ColoredPoint2D
struct Point2D
struct TexturedPoint2D
enum AttributeType { UnknownAttribute, PositionAttribute, ColorAttribute, TexCoordAttribute, TexCoord1Attribute, TexCoord2Attribute }
enum DataPattern { AlwaysUploadPattern, DynamicPattern, StaticPattern, StreamPattern }
enum DrawingMode { DrawPoints, DrawLines, DrawLineStrip, DrawTriangles, DrawTriangleStrip }
enum Type { ByteType, UnsignedByteType, ShortType, UnsignedShortType, IntType, …, DoubleType }

公共函数

QSGGeometry(const QSGGeometry::AttributeSet &attributes, int vertexCount, int indexCount = 0, int indexType = UnsignedShortType)
virtual ~QSGGeometry()
void allocate(int vertexCount, int indexCount = 0)
int attributeCount() const
const QSGGeometry::Attribute *attributes() const
unsigned int drawingMode() const
int indexCount() const
void *indexData()
const void *indexData() const
uint *indexDataAsUInt()
const uint *indexDataAsUInt() const
quint16 *indexDataAsUShort()
const quint16 *indexDataAsUShort() const
QSGGeometry::DataPattern indexDataPattern() const
int indexType() const
float lineWidth() const
void markIndexDataDirty()
void markVertexDataDirty()
void setDrawingMode(unsigned int mode)
void setIndexDataPattern(QSGGeometry::DataPattern p)
void setLineWidth(float width)
void setVertexDataPattern(QSGGeometry::DataPattern p)
int sizeOfIndex() const
int sizeOfVertex() const
int vertexCount() const
void *vertexData()
const void *vertexData() const
QSGGeometry::ColoredPoint2D *vertexDataAsColoredPoint2D()
const QSGGeometry::ColoredPoint2D *vertexDataAsColoredPoint2D() const
QSGGeometry::Point2D *vertexDataAsPoint2D()
const QSGGeometry::Point2D *vertexDataAsPoint2D() const
QSGGeometry::TexturedPoint2D *vertexDataAsTexturedPoint2D()
const QSGGeometry::TexturedPoint2D *vertexDataAsTexturedPoint2D() const
QSGGeometry::DataPattern vertexDataPattern() const

静态公共成员

const QSGGeometry::AttributeSet &defaultAttributes_ColoredPoint2D()
const QSGGeometry::AttributeSet &defaultAttributes_Point2D()
const QSGGeometry::AttributeSet &defaultAttributes_TexturedPoint2D()
void updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect)
void updateRectGeometry(QSGGeometry *g, const QRectF &rect)
void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect)

详细说明

QSGGeometry 类存储用场景图渲染的基元几何图形。它包含顶点数据和可选的索引数据。用于绘制几何图形的模式(也称为基元拓扑)用setDrawingMode() 指定。

顶点可以是由 x 和 y 值定义的简单点,也可以是更复杂的点,每个顶点都包含法线、纹理坐标和三维位置。QSGGeometry::AttributeSet 用于描述顶点数据是如何建立的。属性集只能在构建时指定。QSGGeometry 类默认提供了一些方便的属性和属性集。defaultAttributes_Point2D() 函数返回用于普通纯色矩形的属性集,而defaultAttributes_TexturedPoint2D 函数返回用于纹理 2D 几何体的属性。顶点数据在内部存储为void * ,可通过vertexData() 函数访问。vertexDataAsPoint2D() 和vertexDataAsTexturedPoint2D() 提供了常用属性集的便捷访问器。顶点数据通过向构造函数传递顶点计数或稍后调用allocate() 进行分配。

QSGGeometry 可以选择包含无符号 32 位、无符号 16 位或无符号 8 位整数的索引。索引类型必须在创建时指定,且不能更改。

下面的代码段说明了如何创建由位置顶点和颜色顶点组成的几何体。

struct MyPoint2D {
    float x;
    float y;
    float r;
    float g;
    float b;
    float a;

    void set(float x_, float y_, float r_, float g_, float b_, float a_) {
        x = x_;
        y = y_;
        r = r_;
        g = g_;
        b = b_;
        a = a_;
    }
};

QSGGeometry::Attribute MyPoint2D_Attributes[] = {
    QSGGeometry::Attribute::create(0, 2, FloatType, true),
    QSGGeometry::Attribute::create(1, 4, FloatType, false)
};

QSGGeometry::AttributeSet MyPoint2D_AttributeSet = {
    2,
    sizeof(MyPoint2D),
    MyPoint2D_Attributes
};

...

geometry = new QSGGeometry(MyPoint2D_AttributeSet, 2);
geometry->setDrawingMode(DrawLines);

MyPoint2D *vertices = static_cast<MyPoint2D *>(geometry->vertexData());
vertices[0].set(0, 0, 1, 0, 0, 1);
vertices[1].set(width(), height(), 0, 0, 1, 1);

QSGGeometry 是一个软件缓冲区,就加速渲染而言是客户端的,因为二维图形中使用的缓冲区通常由许多小缓冲区组成,这些缓冲区每帧都会发生变化,上传到图形内存并不会带来好处。不过,QSGGeometry 支持使用setVertexDataPattern() 和setIndexDataPattern() 函数向渲染器提示应上传缓冲区。至于是否遵守这一提示,则取决于具体实现。

注意: 所有带有 QSG 前缀的类都只能在场景图的渲染线程中使用。更多信息请参见场景图和渲染

另请参阅 QSGGeometryNode场景图 - 自定义几何体

成员类型文档

enum QSGGeometry::AttributeType

该枚举标识了几种属性类型。

常量描述
QSGGeometry::UnknownAttribute0无所谓
QSGGeometry::PositionAttribute1位置
QSGGeometry::ColorAttribute2颜色
QSGGeometry::TexCoordAttribute3纹理坐标
QSGGeometry::TexCoord1Attribute4纹理坐标 1
QSGGeometry::TexCoord2Attribute5纹理坐标 2

enum QSGGeometry::DataPattern

DataPattern 枚举用于指定几何体对象中顶点和索引数据的使用模式。

常量说明
QSGGeometry::AlwaysUploadPattern0始终上传数据。这意味着用户在更改索引和顶点数据后无需明确将其标记为脏数据。这是默认值。
QSGGeometry::DynamicPattern2数据被反复修改并多次绘制。这是一个可以提供更好性能的提示。设置后,用户必须确保在更改数据后将其标记为脏数据。
QSGGeometry::StaticPattern3数据修改一次,绘制多次。此提示可能会提供更好的性能。设置后,用户必须确保在更改数据后将其标记为脏数据。
QSGGeometry::StreamPattern1数据几乎每次绘制都会被修改。这是一个可以提供更好性能的提示。设置后,用户必须确保在更改数据后将其标记为脏数据。

enum QSGGeometry::DrawingMode

指定绘制模式,也称为原始拓扑。

注意: 从 Qt 6 开始,场景图形只公开所有受支持的 3D 图形 API 都支持的拓扑结构。因此,在 Qt XML 6 中,尽管枚举值本身仍然存在,但运行时不再支持DrawLineLoopDrawTriangleFan

常量
QSGGeometry::DrawPoints0x0000
QSGGeometry::DrawLines0x0001
QSGGeometry::DrawLineStrip0x0003
QSGGeometry::DrawTriangles0x0004
QSGGeometry::DrawTriangleStrip0x0005

enum QSGGeometry::Type

指定顶点数据中的组件类型。

常数说明
QSGGeometry::ByteType0x1400
QSGGeometry::UnsignedByteType0x1401
QSGGeometry::ShortType0x1402
QSGGeometry::UnsignedShortType0x1403
QSGGeometry::IntType0x1404
QSGGeometry::UnsignedIntType0x1405
QSGGeometry::FloatType0x1406
QSGGeometry::Bytes2Type0x1407在 Qt 5.14 中添加。
QSGGeometry::Bytes3Type0x1408已在 Qt 5.14 中添加。
QSGGeometry::Bytes4Type0x1409已在 Qt 5.14 中添加。
QSGGeometry::DoubleType0x140A已在 Qt 5.14 中添加。

成员函数文档

QSGGeometry::QSGGeometry(const QSGGeometry::AttributeSet &attributes, int vertexCount, int indexCount = 0, int indexType = UnsignedShortType)

根据attributes 构建几何对象。

该对象根据attributes 中的累积大小为vertexCount 顶点分配空间,并为indexCount 分配空间。

indexType 可以是UnsignedShortTypeUnsignedIntType 。对后者的支持取决于运行时使用的图形 API 实现,可能并不总是可用。

几何对象默认以DrawTriangleStrip 作为绘制模式。

注意: attributes 及其引用的Attribute 对象必须在 QSGGeometry 的整个生命周期内保持有效。QSGGeometry 会存储对attributes 的引用,不会删除Attribute 对象。

[virtual noexcept] QSGGeometry::~QSGGeometry()

销毁几何对象及其分配的顶点和索引数据。

void QSGGeometry::allocate(int vertexCount, int indexCount = 0)

调整此几何对象顶点和索引数据的大小,以适应vertexCount 顶点和indexCount 索引。

调用此函数后,顶点和索引数据将失效,调用者必须通过调用 node->markDirty(QSGNode::DirtyGeometry) 将相关几何体节点标记为脏节点,以确保渲染器有机会更新内部缓冲区。

int QSGGeometry::attributeCount() const

返回此几何体使用的属性集中的属性数量。

const QSGGeometry::Attribute *QSGGeometry::attributes() const

返回包含该几何体属性的数组。数组的大小用attributeCount() 表示。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_ColoredPoint2D()

返回用于每个顶点彩色二维绘图的属性的方便函数。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_Point2D()

返回二维纯色绘图属性的方便函数。

[static] const QSGGeometry::AttributeSet &QSGGeometry::defaultAttributes_TexturedPoint2D()

返回二维纹理绘图属性的方便函数。

unsigned int QSGGeometry::drawingMode() const

返回此几何体的绘图模式。

默认值为DrawTriangleStrip

另请参阅 setDrawingMode()。

int QSGGeometry::indexCount() const

返回此几何对象中的索引数。

void *QSGGeometry::indexData()

返回指向此几何对象原始索引数据的指针。

另请参阅 indexDataAsUShort() 和indexDataAsUInt()。

const void *QSGGeometry::indexData() const

返回指向此几何对象原始索引数据的指针。

另请参阅 indexDataAsUShort() 和indexDataAsUInt()。

uint *QSGGeometry::indexDataAsUInt()

方便函数,以 32 位无符号整数的可变数组形式访问索引数据。

const uint *QSGGeometry::indexDataAsUInt() const

方便函数,用于以不可变的 32 位无符号整数数组形式访问索引数据。

quint16 *QSGGeometry::indexDataAsUShort()

方便函数,以 16 位无符号整数的可变数组形式访问索引数据。

const quint16 *QSGGeometry::indexDataAsUShort() const

方便函数,用于以不可变的 16 位无符号整数数组形式访问索引数据。

QSGGeometry::DataPattern QSGGeometry::indexDataPattern() const

返回此几何体中索引的使用模式。默认模式为AlwaysUploadPattern

另请参阅 setIndexDataPattern() 。

int QSGGeometry::indexType() const

返回此几何对象中用于索引的基元类型。

float QSGGeometry::lineWidth() const

获取该几何体当前使用的线宽或点宽。此属性仅适用于drawingModeDrawLinesDrawLineStrip 时的线宽。如果支持,也适用于drawingModeDrawPoints 时的点尺寸。

默认值为1.0

注: 根据平台和图形 API 的不同,运行时对点和线绘制的支持可能会受到限制。例如,某些 API 不支持点精灵,因此无法设置 1 以外的尺寸。

注: 始终支持1.0 的宽度。

另请参阅 setLineWidth() 和drawingMode()。

void QSGGeometry::markIndexDataDirty()

标记此几何体中的顶点已更改,必须重新上传。

只有当顶点的使用模式为静态数据(StaticData),且渲染此几何体的渲染器将几何体上载到顶点缓冲对象(VBO)中时,此函数才会生效。

void QSGGeometry::markVertexDataDirty()

标记此几何体中的顶点已更改,必须重新上传。

只有当顶点的使用模式为静态数据(StaticData),且渲染此几何体的渲染器将几何体上载到顶点缓冲对象(VBO)中时,此函数才会生效。

void QSGGeometry::setDrawingMode(unsigned int mode)

设置用于绘制该几何体的mode

默认值为QSGGeometry::DrawTriangleStrip

另请参阅 drawingMode() 和DrawingMode

void QSGGeometry::setIndexDataPattern(QSGGeometry::DataPattern p)

将索引的使用模式设置为p

默认值为AlwaysUploadPattern 。当设置为默认值以外的其他值时,用户必须在更改索引数据后调用markIndexDataDirty() ,此外还必须使用QSGNode::DirtyGeometry 调用QSGNode::markDirty() 。

另请参阅 indexDataPattern()。

void QSGGeometry::setLineWidth(float width)

将此几何体使用的线宽或点宽设置为width 。该属性仅适用于drawingModeDrawLinesDrawLineStrip 时的线宽。如果支持,也适用于drawingModeDrawPoints 时的点尺寸。

注: 根据平台和图形 API 的不同,运行时对点和线绘制的支持可能会受到限制。例如,有些 API 不支持点精灵,因此无法设置 1 以外的尺寸。

注: 始终支持1.0 的宽度。

另请参阅 lineWidth() 和drawingMode()。

void QSGGeometry::setVertexDataPattern(QSGGeometry::DataPattern p)

将顶点的使用模式设置为p

默认值为AlwaysUploadPattern 。当设置为默认值以外的其他值时,用户必须在更改顶点数据后调用markVertexDataDirty() ,此外还必须调用QSGNode::markDirty() 和QSGNode::DirtyGeometry

另请参见 vertexDataPattern()。

int QSGGeometry::sizeOfIndex() const

返回索引类型的字节大小。

索引类型为UnsignedShortType 时,该值为2 ;索引类型为UnsignedIntType 时,该值为4

int QSGGeometry::sizeOfVertex() const

以字节为单位返回一个顶点的大小。

该值来自属性。

[static] void QSGGeometry::updateColoredRectGeometry(QSGGeometry *g, const QRectF &rect)

使用rect 中的坐标更新几何图形g

该函数假定几何对象包含一个由QSGGeometry::ColoredPoint2D 顶点组成的三角形条带

[static] void QSGGeometry::updateRectGeometry(QSGGeometry *g, const QRectF &rect)

使用rect 中的坐标更新几何图形g

该函数假定几何对象包含一个由QSGGeometry::Point2D 顶点组成的三角形条带

[static] void QSGGeometry::updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &textureRect)

使用rect 中的坐标和textureRect 中的纹理坐标更新几何图形g

textureRect 应使用归一化坐标。

g 假定为一个由 类型的四个顶点组成的三角形条带。QSGGeometry::TexturedPoint2D

int QSGGeometry::vertexCount() const

返回此几何对象的顶点数。

void *QSGGeometry::vertexData()

返回指向此几何体原始顶点数据的指针。

另请参阅 vertexDataAsPoint2D() 和vertexDataAsTexturedPoint2D()。

const void *QSGGeometry::vertexData() const

返回指向此几何体原始顶点数据的指针。

另请参阅 vertexDataAsPoint2D() 和vertexDataAsTexturedPoint2D()。

QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()

方便函数,用于以QSGGeometry::ColoredPoint2D 的可变数组形式访问顶点数据。

const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const

方便函数,用于以QSGGeometry::ColoredPoint2D 的不可变数组形式访问顶点数据。

QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()

方便函数,用于以QSGGeometry::Point2D 的可变数组形式访问顶点数据。

const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const

方便函数,用于以QSGGeometry::Point2D 的不可变数组形式访问顶点数据。

QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()

方便函数,用于以QSGGeometry::TexturedPoint2D 的可变数组形式访问顶点数据。

const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const

方便函数,用于以QSGGeometry::TexturedPoint2D 的不可变数组形式访问顶点数据。

QSGGeometry::DataPattern QSGGeometry::vertexDataPattern() const

返回此几何体中顶点的使用模式。默认模式为AlwaysUploadPattern

另请参阅 setVertexDataPattern().

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.