我正在使用 Skia 和 C++,但问题实际上与框架和语言无关。
我有可以具有平移、旋转和缩放的对象。它们也可以有一个不是 (0,0) 的局部原点 - 例如,一个正方形的原点可能位于中心而不是左上角。
对象可以有子对象,并且这些子对象相对于父对象进行变换。矩阵在沿层次结构移动时被组合。
这是我如何做的示例代码,它按预期工作。
但我的问题是 - 我怎样才能改变它,这样我就不必将原点乘以比例?
// C++
void MyObject::update( SkMatrix parentTransform ) {
// Reset local transform to match parent
localTransform.getIdentity();
localTransform.postConcat( parentTransform );
// Pre-multiply origin by scale (how can I avoid this?)
ox = originX * scaleX;
oy = originY * scaleY;
// Now apply translation (taking origin in to account)
localTransform = localTransform.preConcat( SkMatrix::Translate( x - ox, y - oy ) );
// Rotate about the origin
localTransform = localTransform.preConcat( SkMatrix::RotateDeg( rotation, SkPoint::Make( ox, oy ) ) );
// Scale
localTransform = localTransform.preConcat( SkMatrix::Scale( scaleX, scaleY ) );
// Now update the children of this object recursively
for ( auto& child : children ) {
child.update( localTransform );
}
}
我迷失在前后连接(乘法)矩阵运算顺序中!
任何帮助表示赞赏