| 1 | /**************************************************************************** |
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2018 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:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "raycaster_p.h" |
| 41 | #include "qpickevent.h" |
| 42 | #include <Qt3DRender/private/abstractrenderer_p.h> |
| 43 | #include <Qt3DRender/qabstractraycaster.h> |
| 44 | #include <Qt3DRender/qlayer.h> |
| 45 | #include <Qt3DRender/private/qabstractraycaster_p.h> |
| 46 | #include <Qt3DRender/private/raycastingjob_p.h> |
| 47 | #include <Qt3DRender/private/qrenderaspect_p.h> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | using namespace Qt3DCore; |
| 52 | |
| 53 | namespace Qt3DRender { |
| 54 | |
| 55 | namespace Render { |
| 56 | |
| 57 | RayCaster::RayCaster() |
| 58 | : BackendNode(QBackendNode::ReadWrite) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | RayCaster::~RayCaster() |
| 63 | { |
| 64 | notifyJob(); |
| 65 | } |
| 66 | |
| 67 | QAbstractRayCasterPrivate::RayCasterType RayCaster::type() const |
| 68 | { |
| 69 | return m_type; |
| 70 | } |
| 71 | |
| 72 | QAbstractRayCaster::RunMode RayCaster::runMode() const |
| 73 | { |
| 74 | return m_runMode; |
| 75 | } |
| 76 | |
| 77 | QVector3D RayCaster::origin() const |
| 78 | { |
| 79 | return m_origin; |
| 80 | } |
| 81 | |
| 82 | QVector3D RayCaster::direction() const |
| 83 | { |
| 84 | return m_direction; |
| 85 | } |
| 86 | |
| 87 | float RayCaster::length() const |
| 88 | { |
| 89 | return m_length; |
| 90 | } |
| 91 | |
| 92 | QPoint RayCaster::position() const |
| 93 | { |
| 94 | return m_position; |
| 95 | } |
| 96 | |
| 97 | Qt3DCore::QNodeIdVector RayCaster::layerIds() const |
| 98 | { |
| 99 | return m_layerIds; |
| 100 | } |
| 101 | |
| 102 | QAbstractRayCaster::FilterMode RayCaster::filterMode() const |
| 103 | { |
| 104 | return m_filterMode; |
| 105 | } |
| 106 | |
| 107 | void RayCaster::cleanup() |
| 108 | { |
| 109 | BackendNode::setEnabled(false); |
| 110 | m_type = QAbstractRayCasterPrivate::WorldSpaceRayCaster; |
| 111 | m_runMode = QAbstractRayCaster::SingleShot; |
| 112 | m_direction = QVector3D(0.f, 0.f, 1.f); |
| 113 | m_origin = {}; |
| 114 | m_length = 0.f; |
| 115 | m_position = {}; |
| 116 | m_filterMode = QAbstractRayCaster::AcceptAllMatchingLayers; |
| 117 | m_layerIds.clear(); |
| 118 | notifyJob(); |
| 119 | } |
| 120 | |
| 121 | void RayCaster::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
| 122 | { |
| 123 | const QAbstractRayCaster *node = qobject_cast<const QAbstractRayCaster *>(object: frontEnd); |
| 124 | if (!node) |
| 125 | return; |
| 126 | |
| 127 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
| 128 | |
| 129 | if (node->runMode() != m_runMode) { |
| 130 | m_runMode = node->runMode(); |
| 131 | notifyJob(); |
| 132 | markDirty(changes: AbstractRenderer::AllDirty); |
| 133 | } |
| 134 | |
| 135 | if (node->filterMode() != m_filterMode) { |
| 136 | m_filterMode = node->filterMode(); |
| 137 | notifyJob(); |
| 138 | markDirty(changes: AbstractRenderer::AllDirty); |
| 139 | } |
| 140 | |
| 141 | const Qt3DCore::QNodeIdVector layerIds = Qt3DCore::qIdsForNodes(nodes: node->layers()); |
| 142 | if (layerIds != m_layerIds) { |
| 143 | m_layerIds = layerIds; |
| 144 | markDirty(changes: AbstractRenderer::LayersDirty); |
| 145 | notifyJob(); |
| 146 | } |
| 147 | |
| 148 | const QAbstractRayCasterPrivate *d = static_cast<const QAbstractRayCasterPrivate *>(QNodePrivate::get(q: node)); |
| 149 | if (d->m_direction != m_direction) { |
| 150 | m_direction = d->m_direction; |
| 151 | notifyJob(); |
| 152 | markDirty(changes: AbstractRenderer::AllDirty); |
| 153 | } |
| 154 | |
| 155 | if (!qFuzzyCompare(p1: d->m_length, p2: m_length)) { |
| 156 | m_length = d->m_length; |
| 157 | notifyJob(); |
| 158 | markDirty(changes: AbstractRenderer::AllDirty); |
| 159 | } |
| 160 | |
| 161 | if (d->m_origin != m_origin) { |
| 162 | m_origin = d->m_origin; |
| 163 | notifyJob(); |
| 164 | markDirty(changes: AbstractRenderer::AllDirty); |
| 165 | } |
| 166 | |
| 167 | if (d->m_position != m_position) { |
| 168 | m_position = d->m_position; |
| 169 | notifyJob(); |
| 170 | markDirty(changes: AbstractRenderer::AllDirty); |
| 171 | } |
| 172 | |
| 173 | if (d->m_rayCasterType != m_type) { |
| 174 | m_type = d->m_rayCasterType; |
| 175 | notifyJob(); |
| 176 | markDirty(changes: AbstractRenderer::AllDirty); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void RayCaster::notifyJob() |
| 181 | { |
| 182 | if (m_renderer && m_renderer->aspect()) |
| 183 | QRenderAspectPrivate::get(q: m_renderer->aspect())->m_rayCastingJob->markCastersDirty(); |
| 184 | } |
| 185 | |
| 186 | } // Render |
| 187 | |
| 188 | } // Qt3DRender |
| 189 | |
| 190 | QT_END_NAMESPACE |
| 191 |
