1// Copyright (C) 2018 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 "qabstractraycaster.h"
5#include "qabstractraycaster_p.h"
6#include <Qt3DCore/qaspectengine.h>
7#include <Qt3DCore/qentity.h>
8#include <Qt3DCore/private/qcomponent_p.h>
9#include <Qt3DCore/private/qscene_p.h>
10#include <Qt3DCore/private/qaspectengine_p.h>
11#include <Qt3DRender/qlayer.h>
12#include <Qt3DRender/qrenderaspect.h>
13#include <Qt3DRender/private/qrenderaspect_p.h>
14
15QT_BEGIN_NAMESPACE
16
17namespace Qt3DRender {
18
19QAbstractRayCasterPrivate::QAbstractRayCasterPrivate()
20 : QComponentPrivate()
21{
22 m_enabled = false;
23 m_shareable = false;
24}
25
26QAbstractRayCasterPrivate *QAbstractRayCasterPrivate::get(QAbstractRayCaster *obj)
27{
28 return obj->d_func();
29}
30
31const QAbstractRayCasterPrivate *QAbstractRayCasterPrivate::get(const QAbstractRayCaster *obj)
32{
33 return obj->d_func();
34}
35
36void QAbstractRayCasterPrivate::updateHitEntites(QAbstractRayCaster::Hits &hits, Qt3DCore::QScene *scene)
37{
38 for (int i = 0; i < hits.size(); i++)
39 hits[i].setEntity(qobject_cast<Qt3DCore::QEntity *>(object: scene->lookupNode(id: hits[i].entityId())));
40}
41
42QAbstractRayCaster::Hits QAbstractRayCasterPrivate::pick()
43{
44 Qt3DRender::QRenderAspect *renderAspect = nullptr;
45 const auto aspects = m_scene->engine()->aspects();
46 for (auto a: aspects) {
47 renderAspect = qobject_cast<Qt3DRender::QRenderAspect *>(object: a);
48 if (renderAspect)
49 break;
50 }
51 if (!renderAspect)
52 return {};
53
54 Q_Q(QAbstractRayCaster);
55 auto dRenderAspect = Qt3DRender::QRenderAspectPrivate::get(q: renderAspect);
56 dispatchHits(hits: dRenderAspect->m_rayCastingJob->pick(rayCaster: q));
57 return m_hits;
58}
59
60void QAbstractRayCasterPrivate::dispatchHits(const QAbstractRayCaster::Hits &hits)
61{
62 Q_Q(QAbstractRayCaster);
63 m_hits = hits;
64 updateHitEntites(hits&: m_hits, scene: m_scene);
65 bool v = q->blockNotifications(block: true);
66 emit q->hitsChanged(hits: m_hits);
67 q->blockNotifications(block: v);
68}
69
70/*!
71 \class Qt3DRender::QAbstractRayCaster
72 \brief An abstract base class for ray casting in 3d scenes.
73 \inmodule Qt3DRender
74 \since 5.11
75 \inherits QComponent
76
77 Qt3DRender::QAbstractRayCaster is an abstract base class for casting rays into a 3d scene.
78 Qt3DRender::QAbstractRayCaster can not be directly instantiated, but rather
79 through its subclasses. QAbstractRayCaster specifies common properties
80 for all ray casters, such as run mode and layer handling, while leaving the actual
81 ray casting details to the subclasses.
82
83 Ray castings differs from picking (using Qt3DRender::QObjectPicker) in that it does not
84 require mouse events to trigger.
85
86 By default, the instances of Qt3DRender::QAbstractRayCaster are disabled. When enabled,
87 the specified ray will be tested for intersecting objects at every frame. The
88 QAbstractRayCaster::hits property will be updated with the results of the ray casting,
89 even if no objects are found.
90
91 The Qt3DRender::QPickingSettings can be used to control the ray casting, such as which
92 primitives are tested and how the results are returned.
93
94 Furthermore, Qt3DRender::QLayer components can be used to control how entities, or entity
95 sub-graphs, react to ray casting.
96
97 \note Components derived from QAbstractRayCaster should not be shared amount multiple entities.
98
99 \sa Qt3DRender::QRayCaster, Qt3DRender::QScreenRayCaster, Qt3DRender::QObjectPicker,
100 Qt3DRender::QPickingSettings, Qt3DRender::QNoPicking
101*/
102/*!
103 \qmltype AbstractRayCaster
104 \brief An abstract base class for ray casting in 3d scenes.
105 \inqmlmodule Qt3D.Render
106 \since 5.11
107 \instantiates Qt3DRender::QAbstractRayCaster
108
109 AbstractRayCaster is an abstract base class for casting rays into a 3d scene.
110 AbstractRayCaster can not be directly instantiated, but rather
111 through its subclasses. QAbstractRayCaster specifies common properties
112 for all ray casters, such as run mode and layer handling, while leaving the actual
113 ray casting details to the subclasses.
114
115 Ray castings differs from picking (using ObjectPicker) in that it does not
116 require mouse events to trigger.
117
118 By default, the instances of AbstractRayCaster are disabled. When enabled,
119 the specified ray will be tested for intersecting objects at every frame. The
120 AbstractRayCaster.hits property will be updated with the results of the ray casting,
121 even if no objects are found.
122
123 The Qt3D.Render::PickingSettings can be used to control the ray casting, such as which
124 primitives are tested and how the results are returned.
125
126 Furthermore, Qt3D.Render::Layer components can be used to control how entities, or entity
127 sub-graphs, react to ray casting.
128
129 Note: components derived from AbstractRayCaster should not be shared amount multiple entities.
130
131 \sa Qt3D.Render::RayCaster, Qt3D.Render::ScreenRayCaster, Qt3D.Render::ObjectPicker,
132 Qt3D.Render::PickingSettings, Qt3D.Render::NoPicking
133*/
134
135/*!
136 \enum QAbstractRayCaster::RunMode
137
138 This enumeration specifies how often ray casting is performed
139 \value Continuous Ray casting is performed at every frame as long as the component is enabled.
140 \value SingleShot Ray casting is done once then the component disables itself. This is the default
141*/
142/*!
143 \enum QAbstractRayCaster::FilterMode
144
145 Specifies the rules for selecting entities to test for raycasting.
146
147 \value AcceptAnyMatchingLayers
148 Accept entities that reference one or more \l QLayer objects added to this
149 QAbstractRayCaster. This is the default
150
151 \value AcceptAllMatchingLayers
152 Accept entities that reference all the \l QLayer objects added to this QAbstractRayCaster
153
154 \value DiscardAnyMatchingLayers
155 Discard entities that reference one or more \l QLayer objects added to this QAbstractRayCaster
156
157 \value DiscardAllMatchingLayers
158 Discard entities that reference all \l QLayer objects added to this QAbstractRayCaster
159*/
160
161/*!
162 \property Qt3DRender::QAbstractRayCaster::filterMode
163
164 Holds the filter mode specifying the entities to select for ray casting tests.
165
166 The default value is AcceptMatchingLayers.
167*/
168/*!
169 \qmlproperty enumeration AbstractRayCaster::filterMode
170
171 Holds the filter mode specifying the entities to select for ray casting tests.
172
173 The default value is \c {AbstractRayCaster.AcceptMatchingLayers}.
174
175 \value AcceptAnyMatchingLayers
176 Accept entities that reference one or more \l Layer objects added to this
177 AbstractRayCaster. This is the default
178
179 \value AcceptAllMatchingLayers
180 Accept entities that reference all the \l Layer objects added to this AbstractRayCaster
181
182 \value DiscardAnyMatchingLayers
183 Discard entities that reference one or more \l Layer objects added to this AbstractRayCaster
184
185 \value DiscardAllMatchingLayers
186 Discard entities that reference all \l Layer objects added to this AbstractRayCaster
187*/
188
189/*!
190 \property Qt3DRender::QAbstractRayCaster::runMode
191
192 Holds the run mode controlling how often ray casting tests are performed.
193
194 If set to SingleShot (the default), when the component is enabled, a single ray casting
195 test will be performed and the component will automatically disable itself.
196
197 If set to Continuous, ray casting tests will be performed at every frame as long as
198 the component is enabled.
199*/
200/*!
201 \qmlproperty enumeration AbstractRayCaster::runMode
202 Holds the run mode controlling how often ray casting tests are performed.
203
204 \value Continuous Ray casting is performed at every frame as long as the component is enabled.
205
206 \value SingleShot Ray casting is done once then the component disables itself. This is the default
207*/
208
209/*!
210 \property Qt3DRender::QAbstractRayCaster::hits
211
212 Holds the results of last ray casting test as a vector of Qt3DRender::QRayCasterHit instances.
213
214 Note that even if successive tests return the exact same results (or empty results), a
215 change notification will be emitted at every test.
216*/
217
218/*!
219 \qmlproperty array AbstractRayCaster::hits
220
221 Holds the results of last ray casting test as an array of javascript objects. The fields
222 defined on the objects are defined below.
223
224 \code
225 {
226 type // enum value of RayCasterHit.HitType
227 entity // entity that was intersected
228 distance // distance from ray origin to intersection
229 localIntersection.x: // coordinate of intersection in the entity's coordinate system
230 localIntersection.y
231 localIntersection.z
232 worldIntersection.x // coordinate of intersection in the model's coordinate system
233 worldIntersection.y
234 worldIntersection.z
235 primitiveIndex // index of the primitive (triangle, line, point) that was intersected;
236 // (undefined if the picking mode is set to bounding volume)
237 vertex1Index // index of the first point of the triangle or line that was intersected
238 // (undefined if the picking mode is set to bounding volume or points)
239 vertex2Index // index of the second point of the triangle or line that was intersected
240 // (undefined if the picking mode is set to bounding volume or points)
241 vertex3Index // index of the second point of the triangle that was intersected
242 // (undefined if the picking mode is set to bounding volume, points or lines)
243 }
244 \endcode
245
246 Note that even if successive tests return the exact same results (or empty results), a
247 change notification will be emitted at every test.
248*/
249
250
251QAbstractRayCaster::QAbstractRayCaster(Qt3DCore::QNode *parent)
252 : Qt3DCore::QComponent(*new QAbstractRayCasterPrivate(), parent)
253{
254}
255
256/*! \internal */
257QAbstractRayCaster::QAbstractRayCaster(QAbstractRayCasterPrivate &dd, Qt3DCore::QNode *parent)
258 : Qt3DCore::QComponent(dd, parent)
259{
260}
261
262/*! \internal */
263QAbstractRayCaster::~QAbstractRayCaster()
264{
265}
266
267QAbstractRayCaster::RunMode QAbstractRayCaster::runMode() const
268{
269 Q_D(const QAbstractRayCaster);
270 return d->m_runMode;
271}
272
273void QAbstractRayCaster::setRunMode(QAbstractRayCaster::RunMode runMode)
274{
275 Q_D(QAbstractRayCaster);
276 if (d->m_runMode != runMode) {
277 d->m_runMode = runMode;
278 emit runModeChanged(runMode: d->m_runMode);
279 }
280}
281
282QAbstractRayCaster::FilterMode QAbstractRayCaster::filterMode() const
283{
284 Q_D(const QAbstractRayCaster);
285 return d->m_filterMode;
286}
287
288void QAbstractRayCaster::setFilterMode(QAbstractRayCaster::FilterMode filterMode)
289{
290 Q_D(QAbstractRayCaster);
291 if (d->m_filterMode != filterMode) {
292 d->m_filterMode = filterMode;
293 emit filterModeChanged(filterMode: d->m_filterMode);
294 }
295}
296
297QAbstractRayCaster::Hits QAbstractRayCaster::hits() const
298{
299 Q_D(const QAbstractRayCaster);
300 return d->m_hits;
301}
302
303/*!
304 Add \a layer to the current list of layers
305 */
306void QAbstractRayCaster::addLayer(QLayer *layer)
307{
308 Q_ASSERT(layer);
309 Q_D(QAbstractRayCaster);
310 if (!d->m_layers.contains(t: layer)) {
311 d->m_layers.append(t: layer);
312
313 // Ensures proper bookkeeping
314 d->registerDestructionHelper(node: layer, func: &QAbstractRayCaster::removeLayer, d->m_layers);
315
316 // We need to add it as a child of the current node if it has been declared inline
317 // Or not previously added as a child of the current node so that
318 // 1) The backend gets notified about it's creation
319 // 2) When the current node is destroyed, it gets destroyed as well
320 if (!layer->parent())
321 layer->setParent(this);
322
323 d->update();
324 }
325}
326
327/*!
328 Remove \a layer from the current list of layers
329 */
330void QAbstractRayCaster::removeLayer(QLayer *layer)
331{
332 Q_ASSERT(layer);
333 Q_D(QAbstractRayCaster);
334 if (!d->m_layers.removeOne(t: layer))
335 return;
336 d->update();
337 // Remove bookkeeping connection
338 d->unregisterDestructionHelper(node: layer);
339}
340
341/*!
342 \return the current list of layers
343 */
344QList<QLayer *> QAbstractRayCaster::layers() const
345{
346 Q_D(const QAbstractRayCaster);
347 return d->m_layers;
348}
349
350} // Qt3DRender
351
352QT_END_NAMESPACE
353
354#include "moc_qabstractraycaster.cpp"
355

source code of qt3d/src/render/picking/qabstractraycaster.cpp