| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/private/qnode_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | |
| 8 | #include <Qt3DRender/qabstractlight.h> |
| 9 | #include <Qt3DRender/private/qabstractlight_p.h> |
| 10 | #include <Qt3DRender/qpointlight.h> |
| 11 | #include <Qt3DRender/qdirectionallight.h> |
| 12 | #include <Qt3DRender/qspotlight.h> |
| 13 | #include <Qt3DRender/private/qpointlight_p.h> |
| 14 | #include <Qt3DRender/private/qdirectionallight_p.h> |
| 15 | #include <Qt3DRender/private/qspotlight_p.h> |
| 16 | |
| 17 | #include "testarbiter.h" |
| 18 | |
| 19 | class DummyLight : public Qt3DRender::QAbstractLight |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | |
| 23 | public: |
| 24 | explicit DummyLight(Qt3DCore::QNode *parent = nullptr) |
| 25 | : QAbstractLight(*new Qt3DRender::QAbstractLightPrivate(QAbstractLight::PointLight), parent) |
| 26 | {} |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | // We need to call QNode::clone which is protected |
| 31 | // So we sublcass QNode instead of QObject |
| 32 | class tst_QAbstractLight: public Qt3DCore::QNode |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | private Q_SLOTS: |
| 37 | void checkLightPropertyUpdates() |
| 38 | { |
| 39 | TestArbiter arbiter; |
| 40 | QScopedPointer<Qt3DRender::QAbstractLight> light(new DummyLight); |
| 41 | arbiter.setArbiterOnNode(light.data()); |
| 42 | |
| 43 | light->setColor(Qt::red); |
| 44 | light->setIntensity(0.8f); // change from the default of 0.5f |
| 45 | |
| 46 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 47 | QVERIFY(arbiter.dirtyNodes()[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); |
| 48 | QCOMPARE(arbiter.dirtyNodes()[1], light.data()); |
| 49 | |
| 50 | arbiter.clear(); |
| 51 | } |
| 52 | |
| 53 | void checkPointLightPropertyUpdates() |
| 54 | { |
| 55 | TestArbiter arbiter; |
| 56 | QScopedPointer<Qt3DRender::QPointLight> pointLight(new Qt3DRender::QPointLight); |
| 57 | arbiter.setArbiterOnNode(pointLight.data()); |
| 58 | |
| 59 | pointLight->setColor(Qt::green); |
| 60 | pointLight->setIntensity(0.8f); |
| 61 | pointLight->setConstantAttenuation(0.5f); |
| 62 | pointLight->setLinearAttenuation(0.0f); // No actual event triggered as 0.0f is default |
| 63 | pointLight->setQuadraticAttenuation(1.0f); |
| 64 | |
| 65 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 66 | QVERIFY(arbiter.dirtyNodes()[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); |
| 67 | QCOMPARE(arbiter.dirtyNodes()[1], pointLight.data()); |
| 68 | |
| 69 | arbiter.clear(); |
| 70 | } |
| 71 | |
| 72 | void checkDirectionalLightPropertyUpdates() |
| 73 | { |
| 74 | TestArbiter arbiter; |
| 75 | QScopedPointer<Qt3DRender::QDirectionalLight> dirLight(new Qt3DRender::QDirectionalLight); |
| 76 | arbiter.setArbiterOnNode(dirLight.data()); |
| 77 | |
| 78 | dirLight->setColor(Qt::blue); |
| 79 | dirLight->setIntensity(0.8f); |
| 80 | dirLight->setWorldDirection(QVector3D(0.5f, 0.0f, -1.0f)); |
| 81 | |
| 82 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 83 | QVERIFY(arbiter.dirtyNodes()[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); |
| 84 | QCOMPARE(arbiter.dirtyNodes()[1], dirLight.data()); |
| 85 | |
| 86 | arbiter.clear(); |
| 87 | } |
| 88 | |
| 89 | void checkSpotLightPropertyUpdates() |
| 90 | { |
| 91 | TestArbiter arbiter; |
| 92 | QScopedPointer<Qt3DRender::QSpotLight> spotLight(new Qt3DRender::QSpotLight); |
| 93 | arbiter.setArbiterOnNode(spotLight.data()); |
| 94 | |
| 95 | spotLight->setColor(Qt::lightGray); |
| 96 | spotLight->setIntensity(0.8f); |
| 97 | spotLight->setLocalDirection(QVector3D(0.5f, 0.0f, -1.0f)); |
| 98 | spotLight->setCutOffAngle(0.75f); |
| 99 | |
| 100 | QCOMPARE(arbiter.dirtyNodes().size(), 2); |
| 101 | QVERIFY(arbiter.dirtyNodes()[0]->metaObject()->inherits(&Qt3DRender::QShaderData::staticMetaObject)); |
| 102 | QCOMPARE(arbiter.dirtyNodes()[1], spotLight.data()); |
| 103 | |
| 104 | arbiter.clear(); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | QTEST_MAIN(tst_QAbstractLight) |
| 109 | |
| 110 | #include "tst_qabstractlight.moc" |