1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QTest>
31#include <Qt3DCore/qtransform.h>
32#include <Qt3DCore/private/qtransform_p.h>
33#include <Qt3DRender/private/transform_p.h>
34#include <Qt3DRender/private/updateworldtransformjob_p.h>
35#include <private/qbackendnode_p.h>
36#include "qbackendnodetester.h"
37#include "testrenderer.h"
38#include "testpostmanarbiter.h"
39
40class tst_Transform : public Qt3DCore::QBackendNodeTester
41{
42 Q_OBJECT
43
44private Q_SLOTS:
45
46 void checkInitialState()
47 {
48 // GIVEN
49 Qt3DRender::Render::Transform backendTransform;
50
51 // THEN
52 QCOMPARE(backendTransform.isEnabled(), false);
53 QVERIFY(backendTransform.peerId().isNull());
54 QCOMPARE(convertToQMatrix4x4(backendTransform.transformMatrix()), QMatrix4x4());
55 }
56
57 void checkCleanupState()
58 {
59 // GIVEN
60 TestRenderer renderer;
61 Qt3DRender::Render::Transform backendTransform;
62 backendTransform.setRenderer(&renderer);
63
64 // WHEN
65 {
66 Qt3DCore::QTransform transform;
67 transform.setScale3D(QVector3D(1.0f, 2.0f, 3.0f));
68 transform.setTranslation(QVector3D(-1.0, 5.0f, -2.0f));
69 transform.setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1.0f, 0.0f, 0.0), angle: 30.0f));
70 backendTransform.setRenderer(&renderer);
71 simulateInitializationSync(frontend: &transform, backend: &backendTransform);
72 }
73 backendTransform.setEnabled(true);
74
75 backendTransform.cleanup();
76
77 // THEN
78 QCOMPARE(backendTransform.isEnabled(), false);
79 QCOMPARE(convertToQMatrix4x4(backendTransform.transformMatrix()), QMatrix4x4());
80 QCOMPARE(backendTransform.rotation(), QQuaternion());
81 QCOMPARE(backendTransform.scale(), QVector3D());
82 QCOMPARE(backendTransform.translation(), QVector3D());
83 }
84
85 void checkInitializeFromPeer()
86 {
87 // GIVEN
88 TestRenderer renderer;
89 Qt3DCore::QTransform transform;
90 transform.setScale3D(QVector3D(1.0f, 2.0f, 3.0f));
91 transform.setTranslation(QVector3D(-1.0, 5.0f, -2.0f));
92 transform.setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1.0f, 0.0f, 0.0), angle: 30.0f));
93
94 {
95 // WHEN
96 Qt3DRender::Render::Transform backendTransform;
97 backendTransform.setRenderer(&renderer);
98 TestRenderer renderer;
99 backendTransform.setRenderer(&renderer);
100 simulateInitializationSync(frontend: &transform, backend: &backendTransform);
101
102 // THEN
103 QCOMPARE(backendTransform.isEnabled(), true);
104 QCOMPARE(backendTransform.peerId(), transform.id());
105 QCOMPARE(convertToQMatrix4x4(backendTransform.transformMatrix()), transform.matrix());
106 QCOMPARE(backendTransform.rotation(), transform.rotation());
107 QCOMPARE(backendTransform.scale(), transform.scale3D());
108 QCOMPARE(backendTransform.translation(), transform.translation());
109 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
110 }
111 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
112 {
113 // WHEN
114 Qt3DRender::Render::Transform backendTransform;
115 TestRenderer renderer;
116 backendTransform.setRenderer(&renderer);
117 transform.setEnabled(false);
118 simulateInitializationSync(frontend: &transform, backend: &backendTransform);
119
120 // THEN
121 QCOMPARE(backendTransform.peerId(), transform.id());
122 QCOMPARE(backendTransform.isEnabled(), false);
123 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
124 }
125 }
126
127 void checkSceneChangeEvents()
128 {
129 // GIVEN
130 Qt3DCore::QTransform frontendTranform;
131 Qt3DRender::Render::Transform backendTransform;
132 TestRenderer renderer;
133 backendTransform.setRenderer(&renderer);
134 backendTransform.syncFromFrontEnd(frontEnd: &frontendTranform, firstTime: true);
135 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
136
137 {
138 // WHEN
139 const bool newValue = false;
140 frontendTranform.setEnabled(newValue);
141 backendTransform.syncFromFrontEnd(frontEnd: &frontendTranform, firstTime: false);
142
143 // THEN
144 QCOMPARE(backendTransform.isEnabled(), newValue);
145 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
146 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
147 }
148 {
149 // WHEN
150 const QQuaternion newValue = QQuaternion::fromAxisAndAngle(axis: QVector3D(0.0f, 1.0f, 0.0f), angle: 45.0f);
151 frontendTranform.setRotation(newValue);
152 backendTransform.syncFromFrontEnd(frontEnd: &frontendTranform, firstTime: false);
153
154 // THEN
155 QCOMPARE(backendTransform.rotation(), newValue);
156 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
157 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
158 }
159 {
160 // WHEN
161 const QVector3D newValue(454.0f, 355.0f, 0.0f);
162 frontendTranform.setScale3D(newValue);
163 backendTransform.syncFromFrontEnd(frontEnd: &frontendTranform, firstTime: false);
164
165 // THEN
166 QCOMPARE(backendTransform.scale(), newValue);
167 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
168 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
169 }
170 {
171 // WHEN
172 const QVector3D newValue(383.0f, 0.0f, 427.0f);
173 frontendTranform.setTranslation(newValue);
174 backendTransform.syncFromFrontEnd(frontEnd: &frontendTranform, firstTime: false);
175
176 // THEN
177 QCOMPARE(backendTransform.translation(), newValue);
178 QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::TransformDirty);
179 renderer.clearDirtyBits(changes: Qt3DRender::Render::AbstractRenderer::AllDirty);
180 }
181 }
182};
183
184QTEST_MAIN(tst_Transform)
185
186#include "tst_transform.moc"
187

source code of qt3d/tests/auto/render/transform/tst_transform.cpp