diff options
author | Andy Nichols <andy.nichols@theqtcompany.com> | 2015-12-10 17:37:00 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2015-12-11 20:37:11 +0000 |
commit | 31ca4a7d17d84deb05f80053f0f1874da894b840 (patch) | |
tree | bae081ccc1795544acf4d6905351e03d17193a40 /src/core/transforms/qtransform.cpp | |
parent | 2ea648f08a4b935002d20abfafebf81f7b44a1ee (diff) |
QTransform: Add convenience properties for Euler angle rotations
Since refactoring the QTransform API to enable matrix decomposition, it
has become more inconvenient to apply rotation transforms through the
QML API. This is due to the need to set QQuaternion values for the
rotation property, which makes using the Qt Quick animation framework
difficult.
To remedy this, there are now some additional properties added to
Qt3DCore::QTransform which enable direct usage of Euler angles instead
of just QQuaternion. Internally the rotation property is still used by
translating the Euler angle transforms to a QQuaternion.
Change-Id: Iad48542a7b125f7615f7a69516fb10a973d8395b
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/core/transforms/qtransform.cpp')
-rw-r--r-- | src/core/transforms/qtransform.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/core/transforms/qtransform.cpp b/src/core/transforms/qtransform.cpp index ea640b802..e54c11bec 100644 --- a/src/core/transforms/qtransform.cpp +++ b/src/core/transforms/qtransform.cpp @@ -53,6 +53,7 @@ QTransformPrivate::QTransformPrivate() , m_rotation() , m_scale(1.0f, 1.0f, 1.0f) , m_translation() + , m_eulerRotationAngles() , m_matrixDirty(false) { } @@ -83,6 +84,7 @@ void QTransform::copy(const QNode *ref) d_func()->m_rotation = transform->rotation(); d_func()->m_scale = transform->scale3D(); d_func()->m_translation = transform->translation(); + d_func()->m_eulerRotationAngles = transform->d_func()->m_eulerRotationAngles; d_func()->m_matrixDirty = transform->d_func()->m_matrixDirty; } @@ -100,16 +102,79 @@ void QTransform::setMatrix(const QMatrix4x4 &m) d->m_scale = s; d->m_rotation = r; d->m_translation = t; + d->m_eulerRotationAngles = d->m_rotation.toEulerAngles(); emit scale3DChanged(s); emit rotationChanged(r); emit translationChanged(t); const bool wasBlocked = blockNotifications(true); emit matrixChanged(m); + emit rotationXChanged(d->m_eulerRotationAngles.x()); + emit rotationYChanged(d->m_eulerRotationAngles.y()); + emit rotationZChanged(d->m_eulerRotationAngles.z()); blockNotifications(wasBlocked); } } +void QTransform::setRotationX(float rotationX) +{ + Q_D(QTransform); + + if (d->m_eulerRotationAngles.x() == rotationX) + return; + + d->m_eulerRotationAngles.setX(rotationX); + QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles); + if (rotation != d->m_rotation) { + d->m_rotation = rotation; + d->m_matrixDirty = true; + emit rotationChanged(rotation); + } + + const bool wasBlocked = blockNotifications(true); + emit rotationXChanged(rotationX); + blockNotifications(wasBlocked); +} + +void QTransform::setRotationY(float rotationY) +{ + Q_D(QTransform); + + if (d->m_eulerRotationAngles.y() == rotationY) + return; + + d->m_eulerRotationAngles.setY(rotationY); + QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles); + if (rotation != d->m_rotation) { + d->m_rotation = rotation; + d->m_matrixDirty = true; + emit rotationChanged(rotation); + } + + const bool wasBlocked = blockNotifications(true); + emit rotationYChanged(rotationY); + blockNotifications(wasBlocked); +} + +void QTransform::setRotationZ(float rotationZ) +{ + Q_D(QTransform); + if (d->m_eulerRotationAngles.z() == rotationZ) + return; + + d->m_eulerRotationAngles.setZ(rotationZ); + QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles); + if (rotation != d->m_rotation) { + d->m_rotation = rotation; + d->m_matrixDirty = true; + emit rotationChanged(rotation); + } + + const bool wasBlocked = blockNotifications(true); + emit rotationZChanged(rotationZ); + blockNotifications(wasBlocked); +} + QMatrix4x4 QTransform::matrix() const { Q_D(const QTransform); @@ -120,6 +185,24 @@ QMatrix4x4 QTransform::matrix() const return d->m_matrix; } +float QTransform::rotationX() const +{ + Q_D(const QTransform); + return d->m_eulerRotationAngles.x(); +} + +float QTransform::rotationY() const +{ + Q_D(const QTransform); + return d->m_eulerRotationAngles.y(); +} + +float QTransform::rotationZ() const +{ + Q_D(const QTransform); + return d->m_eulerRotationAngles.z(); +} + void QTransform::setScale3D(const QVector3D &scale) { Q_D(QTransform); @@ -158,6 +241,7 @@ void QTransform::setRotation(const QQuaternion &rotation) Q_D(QTransform); if (rotation != d->m_rotation) { d->m_rotation = rotation; + d->m_eulerRotationAngles = d->m_rotation.toEulerAngles(); d->m_matrixDirty = true; emit rotationChanged(rotation); } |