How can I get transform matrix for QQuickItem?
我在
实际上,
QML允许实例化几个
要获得所需的单个转换矩阵,必须从单位矩阵开始,并顺序应用给定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | QMatrix4x4 transformOfItem(QQuickItem *item) { QQmlListProperty transformations = item->transform(); const int count = transformations.couamp;transformations); // Prepare result structure, it will be default-initialized to be an identity matrix QMatrix4x4 transformMatrix; // Apply sequentially all transformation from the item for(int i = 0; i applyamp;transformMatrix); } return transformMatrix; } |
请注意,该函数返回的转换矩阵为
编辑
还有一点需要注意:我发布的方法仅适用于通过分配给
这是一个正确的解决方案,基于Michael先前提供的代码,但实际上已经过固定,因此您不必花20分钟的时间就可以知道如何使用QQmlListProperty
1 2 3 4 5 6 7 8 9 10 11 12 13 | QMatrix4x4 YourQQuickItem::get_model_matrix() { QMatrix4x4 result; // Compose model matrix from our transform properties in the QML QQmlListPropeamp;lt;QQuickTransfamp;gt; transformations = transform(); const int count = transformations.couamp;transformations); for (int i=0amp;lt;count; i++) { QQuickTransform *transform = transformations.amp;transformations, i); transfoamp;gt;applyamp;result); } return result; } |
在我的用例中,我使用它来获取对象的模型矩阵,然后将其与视图和投影矩阵相乘以计算出模型-视图-投影矩阵。
QSGTransformNode类在场景图中实现转换。在
1 2 3 4 | QSGNode *MyQuickItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *data) { QSGTransformNode *transformNode = data->transformNode; qDebug() << transformNode->matrix(); |