1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "transform_p.h" |
5 | |
6 | #include <Qt3DCore/private/qchangearbiter_p.h> |
7 | #include <Qt3DCore/qtransform.h> |
8 | #include <Qt3DCore/private/qtransform_p.h> |
9 | #include <Qt3DRender/qcamera.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | |
14 | namespace Qt3DRender { |
15 | namespace Render { |
16 | |
17 | using namespace Qt3DCore; |
18 | |
19 | Transform::Transform() |
20 | : BackendNode(ReadWrite) |
21 | , m_rotation() |
22 | , m_scale(1.0f, 1.0f, 1.0f) |
23 | , m_translation() |
24 | { |
25 | } |
26 | |
27 | void Transform::cleanup() |
28 | { |
29 | m_rotation = QQuaternion(); |
30 | m_scale = QVector3D(); |
31 | m_translation = QVector3D(); |
32 | m_transformMatrix = Matrix4x4(); |
33 | QBackendNode::setEnabled(false); |
34 | } |
35 | |
36 | Matrix4x4 Transform::transformMatrix() const |
37 | { |
38 | return m_transformMatrix; |
39 | } |
40 | |
41 | QVector3D Transform::scale() const |
42 | { |
43 | return m_scale; |
44 | } |
45 | |
46 | QQuaternion Transform::rotation() const |
47 | { |
48 | return m_rotation; |
49 | } |
50 | |
51 | QVector3D Transform::translation() const |
52 | { |
53 | return m_translation; |
54 | } |
55 | |
56 | void Transform::syncFromFrontEnd(const QNode *frontEnd, bool firstTime) |
57 | { |
58 | const Qt3DCore::QTransform *transform = qobject_cast<const Qt3DCore::QTransform *>(object: frontEnd); |
59 | if (!transform) |
60 | return; |
61 | |
62 | bool dirty = m_rotation != transform->rotation(); |
63 | m_rotation = transform->rotation(); |
64 | dirty |= m_scale != transform->scale3D(); |
65 | m_scale = transform->scale3D(); |
66 | dirty |= m_translation != transform->translation(); |
67 | m_translation = transform->translation(); |
68 | |
69 | if (dirty || firstTime) { |
70 | auto camera = qobject_cast<const Qt3DRender::QCamera *>(object: transform->parentNode()); |
71 | if (camera) { |
72 | m_viewMatrix = Matrix4x4(camera->viewMatrix()); |
73 | m_hasViewMatrix = true; |
74 | } else { |
75 | m_hasViewMatrix = false; |
76 | } |
77 | updateMatrix(); |
78 | markDirty(changes: AbstractRenderer::TransformDirty); |
79 | } |
80 | |
81 | if (transform->isEnabled() != isEnabled()) |
82 | markDirty(changes: AbstractRenderer::TransformDirty); |
83 | |
84 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
85 | } |
86 | |
87 | void Transform::updateMatrix() |
88 | { |
89 | QMatrix4x4 m; |
90 | m.translate(vector: m_translation); |
91 | m.rotate(quaternion: m_rotation); |
92 | m.scale(vector: m_scale); |
93 | m_transformMatrix = Matrix4x4(m); |
94 | } |
95 | |
96 | } // namespace Render |
97 | } // namespace Qt3DRender |
98 | |
99 | QT_END_NAMESPACE |
100 |