diff options
author | Mike Krus <mike.krus@kdab.com> | 2018-01-20 07:31:33 +0000 |
---|---|---|
committer | Mike Krus <mike.krus@kdab.com> | 2018-02-01 22:21:57 +0000 |
commit | b4353c98ed242796cdd434b86cf1154a47ded125 (patch) | |
tree | 9a12ae3d948d8ff1ec0f2d981dc242b6c3c499c8 /tests/auto/render/raycastingjob | |
parent | 53ee42eca2faf76a61a591e5251602f64e00bc67 (diff) |
Unit test for ray casting job
Change-Id: I3b183bfe0231409facce40a7dacc3b923d65b73f
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests/auto/render/raycastingjob')
4 files changed, 381 insertions, 0 deletions
diff --git a/tests/auto/render/raycastingjob/raycastingjob.pro b/tests/auto/render/raycastingjob/raycastingjob.pro new file mode 100644 index 000000000..174de7fdc --- /dev/null +++ b/tests/auto/render/raycastingjob/raycastingjob.pro @@ -0,0 +1,17 @@ +TEMPLATE = app + +TARGET = tst_raycastingjob +QT += core-private 3dcore 3dcore-private 3drender 3drender-private testlib + +CONFIG += testcase + +SOURCES += tst_raycastingjob.cpp + +include(../commons/commons.pri) +include(../../core/common/common.pri) +include(../qmlscenereader/qmlscenereader.pri) +# Extra dependencies to build test scenes needed by the tests +QT += quick 3dquick 3dquick-private 3dextras 3dquickextras + +RESOURCES += \ + raycastingjob.qrc diff --git a/tests/auto/render/raycastingjob/raycastingjob.qrc b/tests/auto/render/raycastingjob/raycastingjob.qrc new file mode 100644 index 000000000..dc5a70c78 --- /dev/null +++ b/tests/auto/render/raycastingjob/raycastingjob.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>testscene_worldraycasting.qml</file> + </qresource> +</RCC> diff --git a/tests/auto/render/raycastingjob/testscene_worldraycasting.qml b/tests/auto/render/raycastingjob/testscene_worldraycasting.qml new file mode 100644 index 000000000..159c8b515 --- /dev/null +++ b/tests/auto/render/raycastingjob/testscene_worldraycasting.qml @@ -0,0 +1,131 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D.Core 2.0 +import Qt3D.Render 2.11 +import Qt3D.Extras 2.0 +import QtQuick.Window 2.0 + +Entity { + id: sceneRoot + + Window { + id: win + width: 600 + height: 600 + visible: true + } + + Camera { + id: camera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 45 + aspectRatio: 16/9 + nearPlane : 0.1 + farPlane : 1000.0 + position: Qt.vector3d( 0.0, 2.0, 20.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + } + + components: [ + RenderSettings { + pickingSettings.pickMethod: PickingSettings.TrianglePicking + + Viewport { + normalizedRect: Qt.rect(0.0, 0.0, 1.0, 1.0) + + RenderSurfaceSelector { + + surface: win + + ClearBuffers { + buffers : ClearBuffers.ColorDepthBuffer + NoDraw {} + } + + CameraSelector { + camera: camera + } + } + } + }, + RayCaster { + id: caster + objectName: "caster" + length: 20 + } + ] + + CylinderMesh { + id: mesh + radius: 1 + length: 3 + rings: 10 + slices: 10 + } + PhongMaterial { id: material } + + Entity { + property Transform transform: Transform { + translation: Qt.vector3d(-5, 0, 0) + } + + components: [mesh, material, transform] + } + + Entity { + property Transform transform: Transform { + translation: Qt.vector3d(5, 0, 0) + } + + components: [mesh, material, transform] + } +} diff --git a/tests/auto/render/raycastingjob/tst_raycastingjob.cpp b/tests/auto/render/raycastingjob/tst_raycastingjob.cpp new file mode 100644 index 000000000..d0f72b2e8 --- /dev/null +++ b/tests/auto/render/raycastingjob/tst_raycastingjob.cpp @@ -0,0 +1,228 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlscenereader.h" +#include "testpostmanarbiter.h" + +#include <QtTest/QTest> +#include <Qt3DCore/qentity.h> +#include <Qt3DCore/qtransform.h> +#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> +#include <Qt3DCore/private/qaspectjobmanager_p.h> +#include <QtQuick/qquickwindow.h> + +#include <Qt3DRender/QCamera> +#include <Qt3DRender/QRayCaster> +#include <Qt3DRender/QScreenRayCaster> +#include <Qt3DRender/private/nodemanagers_p.h> +#include <Qt3DRender/private/managers_p.h> +#include <Qt3DRender/private/entity_p.h> +#include <Qt3DRender/qrenderaspect.h> +#include <Qt3DRender/private/qrenderaspect_p.h> +#include <Qt3DRender/private/raycastingjob_p.h> +#include <Qt3DRender/private/pickboundingvolumeutils_p.h> +#include <Qt3DRender/private/updatemeshtrianglelistjob_p.h> +#include <Qt3DRender/private/updateworldboundingvolumejob_p.h> +#include <Qt3DRender/private/updateworldtransformjob_p.h> +#include <Qt3DRender/private/expandboundingvolumejob_p.h> +#include <Qt3DRender/private/calcboundingvolumejob_p.h> +#include <Qt3DRender/private/calcgeometrytrianglevolumes_p.h> +#include <Qt3DRender/private/loadbufferjob_p.h> +#include <Qt3DRender/private/buffermanager_p.h> +#include <Qt3DRender/private/geometryrenderermanager_p.h> + +#include <private/qpickevent_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +class TestAspect : public Qt3DRender::QRenderAspect +{ +public: + TestAspect(Qt3DCore::QNode *root) + : Qt3DRender::QRenderAspect(Qt3DRender::QRenderAspect::Synchronous) + , m_sceneRoot(nullptr) + { + QRenderAspect::onRegistered(); + + const Qt3DCore::QNodeCreatedChangeGenerator generator(root); + const QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = generator.creationChanges(); + + d_func()->setRootAndCreateNodes(qobject_cast<Qt3DCore::QEntity *>(root), creationChanges); + + Render::Entity *rootEntity = nodeManagers()->lookupResource<Render::Entity, Render::EntityManager>(rootEntityId()); + Q_ASSERT(rootEntity); + m_sceneRoot = rootEntity; + } + + ~TestAspect() + { + QRenderAspect::onUnregistered(); + } + + void onRegistered() { QRenderAspect::onRegistered(); } + void onUnregistered() { QRenderAspect::onUnregistered(); } + + Qt3DRender::Render::NodeManagers *nodeManagers() const { return d_func()->m_renderer->nodeManagers(); } + Qt3DRender::Render::FrameGraphNode *frameGraphRoot() const { return d_func()->m_renderer->frameGraphRoot(); } + Qt3DRender::Render::RenderSettings *renderSettings() const { return d_func()->m_renderer->settings(); } + Qt3DRender::Render::Entity *sceneRoot() const { return m_sceneRoot; } + +private: + Render::Entity *m_sceneRoot; +}; + +} // namespace Qt3DRender + +QT_END_NAMESPACE + +namespace { + +void runRequiredJobs(Qt3DRender::TestAspect *test) +{ + Qt3DRender::Render::UpdateWorldTransformJob updateWorldTransform; + updateWorldTransform.setRoot(test->sceneRoot()); + updateWorldTransform.run(); + + // For each buffer + QVector<Qt3DRender::Render::HBuffer> bufferHandles = test->nodeManagers()->bufferManager()->activeHandles(); + for (auto bufferHandle : bufferHandles) { + Qt3DRender::Render::LoadBufferJob loadBuffer(bufferHandle); + loadBuffer.setNodeManager(test->nodeManagers()); + loadBuffer.run(); + } + + Qt3DRender::Render::CalculateBoundingVolumeJob calcBVolume; + calcBVolume.setManagers(test->nodeManagers()); + calcBVolume.setRoot(test->sceneRoot()); + calcBVolume.run(); + + Qt3DRender::Render::UpdateWorldBoundingVolumeJob updateWorldBVolume; + updateWorldBVolume.setManager(test->nodeManagers()->renderNodesManager()); + updateWorldBVolume.run(); + + Qt3DRender::Render::ExpandBoundingVolumeJob expandBVolume; + expandBVolume.setRoot(test->sceneRoot()); + expandBVolume.run(); + + Qt3DRender::Render::UpdateMeshTriangleListJob updateTriangleList; + updateTriangleList.setManagers(test->nodeManagers()); + updateTriangleList.run(); + + // For each geometry id + QVector<Qt3DRender::Render::HGeometryRenderer> geometryRenderHandles = test->nodeManagers()->geometryRendererManager()->activeHandles(); + for (auto geometryRenderHandle : geometryRenderHandles) { + Qt3DCore::QNodeId geometryRendererId = test->nodeManagers()->geometryRendererManager()->data(geometryRenderHandle)->peerId(); + Qt3DRender::Render::CalcGeometryTriangleVolumes calcGeometryTriangles(geometryRendererId, test->nodeManagers()); + calcGeometryTriangles.run(); + } +} + +void initializeJob(Qt3DRender::Render::RayCastingJob *job, Qt3DRender::TestAspect *test) +{ + job->setFrameGraphRoot(test->frameGraphRoot()); + job->setRoot(test->sceneRoot()); + job->setManagers(test->nodeManagers()); + job->setRenderSettings(test->renderSettings()); +} + +} // anonymous + +class tst_RayCastingJob : public QObject +{ + Q_OBJECT +private Q_SLOTS: + + void checkEarlyReturnWhenNoProperFrameGraph_data() + { + QTest::addColumn<QVector3D>("rayOrigin"); + QTest::addColumn<QVector3D>("rayDirection"); + QTest::addColumn<int>("numIntersections"); + + QTest::newRow("left entity") << QVector3D(-5, 0, 4) << QVector3D(0, 0, -1) << 1; + QTest::newRow("no entity") << QVector3D(0, 0, 4) << QVector3D(0, 0, -1) << 0; + QTest::newRow("both entities") << QVector3D(-8, 0, 0) << QVector3D(1, 0, 0) << 2; + } + + void checkEarlyReturnWhenNoProperFrameGraph() + { + // GIVEN + QmlSceneReader sceneReader(QUrl("qrc:/testscene_worldraycasting.qml")); + QScopedPointer<Qt3DCore::QEntity> root(qobject_cast<Qt3DCore::QEntity *>(sceneReader.root())); + QVERIFY(root); + + Qt3DCore::QComponentVector rootComponents = root->components(); + Qt3DRender::QRayCaster *rayCaster = nullptr; + for (Qt3DCore::QComponent *c: qAsConst(rootComponents)) { + rayCaster = qobject_cast<Qt3DRender::QRayCaster *>(c); + if (rayCaster) + break; + } + QVERIFY(rayCaster); + + QFETCH(QVector3D, rayOrigin); + QFETCH(QVector3D, rayDirection); + QFETCH(int, numIntersections); + + rayCaster->trigger(rayOrigin, rayDirection); + + QScopedPointer<Qt3DRender::TestAspect> test(new Qt3DRender::TestAspect(root.data())); + TestArbiter arbiter; + + // Runs Required jobs + runRequiredJobs(test.data()); + + Qt3DRender::Render::RayCaster *backendRayCaster = test->nodeManagers()->rayCasterManager()->lookupResource(rayCaster->id()); + QVERIFY(backendRayCaster); + Qt3DCore::QBackendNodePrivate::get(backendRayCaster)->setArbiter(&arbiter); + + // WHEN + Qt3DRender::Render::RayCastingJob rayCastingJob; + initializeJob(&rayCastingJob, test.data()); + + bool earlyReturn = !rayCastingJob.runHelper(); + + // THEN + QVERIFY(!earlyReturn); + QVERIFY(!backendRayCaster->isEnabled()); + QCOMPARE(arbiter.events.count(), 2); // hits & disable + Qt3DCore::QPropertyUpdatedChangePtr change = arbiter.events.first().staticCast<Qt3DCore::QPropertyUpdatedChange>(); + QCOMPARE(change->propertyName(), "hits"); + Qt3DRender::QRayCaster::Hits hits = change->value().value<Qt3DRender::QRayCaster::Hits>(); + QCOMPARE(hits.size(), numIntersections); + + if (numIntersections) + QVERIFY(hits.first().entityId()); + } + +}; + +QTEST_MAIN(tst_RayCastingJob) + +#include "tst_raycastingjob.moc" |