summaryrefslogtreecommitdiffstats
path: root/src/render/jobs/pickboundingvolumejob.cpp
blob: 43fa5bf66b9c485af0b2e46592281421c1ca1445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/****************************************************************************
**
** Copyright (C) 2015 Paul Lemire paul.lemire350@gmail.com
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "pickboundingvolumejob_p.h"
#include "qpickevent.h"
#include <Qt3DRender/private/renderer_p.h>
#include <Qt3DRender/private/nodemanagers_p.h>
#include <Qt3DRender/private/framegraphnode_p.h>
#include <Qt3DRender/private/cameralens_p.h>
#include <Qt3DRender/private/cameraselectornode_p.h>
#include <Qt3DRender/private/viewportnode_p.h>
#include <Qt3DRender/private/entity_p.h>
#include <Qt3DRender/private/objectpicker_p.h>
#include <Qt3DRender/private/managers_p.h>
#include <Qt3DRender/private/sphere_p.h>
#include <Qt3DRender/private/geometryrenderer_p.h>
#include <Qt3DRender/private/trianglesvisitor_p.h>
#include <Qt3DRender/private/triangleboundingvolume_p.h>
#include <Qt3DRender/private/qraycastingservice_p.h>
#include <Qt3DRender/qgeometryrenderer.h>
#include <Qt3DCore/private/qservicelocator_p.h>
#include <Qt3DCore/qray3d.h>
#include <QSurface>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

namespace Render {

namespace {

struct ViewportCameraPair
{
    Qt3DCore::QNodeId cameraId;
    QRectF viewport;
};

class ViewportCameraGatherer
{
private:
    QVector<FrameGraphNode *> m_leaves;

    void visit(FrameGraphNode *node)
    {
        Q_FOREACH (Render::FrameGraphNode *n, node->children())
            visit(n);
        if (node->childrenIds().empty())
            m_leaves.push_back(node);
    }

    ViewportCameraPair gatherUpViewportCameras(Render::FrameGraphNode *node) const
    {
        ViewportCameraPair vc;
        vc.viewport = QRectF(0.0f, 0.0f, 1.0f, 1.0f);

        while (node) {
            if (node->isEnabled()) {
                switch (node->nodeType()) {
                case FrameGraphNode::CameraSelector:
                    vc.cameraId = static_cast<const CameraSelector *>(node)->cameraUuid();
                    break;
                case FrameGraphNode::Viewport:
                    vc.viewport = computeViewport(vc.viewport, static_cast<const ViewportNode *>(node));
                    break;
                default:
                    break;
                }
            }
            node = node->parent();
        }
        return vc;
    }

public:
    QVector<ViewportCameraPair> gather(FrameGraphNode *root)
    {
        // Retrieve all leaves
        visit(root);
        QVector<ViewportCameraPair> vcPairs;
        vcPairs.reserve(m_leaves.count());

        // Find all viewport/camera pairs by traversing from leaf to root
        Q_FOREACH (Render::FrameGraphNode *leaf, m_leaves) {
            ViewportCameraPair vcPair = gatherUpViewportCameras(leaf);
            if (!vcPair.cameraId.isNull())
                vcPairs.push_back(vcPair);
        }
        return vcPairs;
    }

};

QVector<Entity *> gatherEntities(Entity *entity)
{
    QVector<Entity *> entities;

    if (entity != Q_NULLPTR) {
        entities.push_back(entity);
        // Traverse children
        Q_FOREACH (Entity *child, entity->children())
            entities += gatherEntities(child);
    }
    return entities;
}

class EntityGatherer
{
public:
    explicit EntityGatherer(Entity *root)
        : m_root(root)
        , m_needsRefresh(true)
    {
    }

    QVector<Entity *> entities() const
    {
        if (m_needsRefresh) {
            m_entities = gatherEntities(m_root);
            m_needsRefresh = false;
        }
        return m_entities;
    }

private:
    Entity *m_root;
    mutable QVector<Entity *> m_entities;
    mutable bool m_needsRefresh;
};

class CollisionVisitor : public TrianglesVisitor
{
public:
    typedef QVector<QCollisionQueryResult::Hit> HitList;
    HitList hits;

    CollisionVisitor(NodeManagers* manager, const Entity *root, const Qt3DCore::QRay3D& ray) : TrianglesVisitor(manager), m_root(root), m_ray(ray), m_triangleIndex(0) { }
private:
    const Entity *m_root;
    Qt3DCore::QRay3D m_ray;
    Qt3DRender::QRayCastingService rayCasting;
    uint m_triangleIndex;

    void visit(uint andx, const QVector3D &a,
               uint bndx, const QVector3D &b,
               uint cndx, const QVector3D &c)
    {
        TriangleBoundingVolume volume(m_root->peerUuid(), a, b, c);
        volume = volume.transform(*m_root->worldTransform());

        QCollisionQueryResult::Hit queryResult = rayCasting.query(m_ray, &volume);
        if (queryResult.m_distance > 0.) {
            queryResult.m_triangleIndex = m_triangleIndex;
            queryResult.m_vertexIndex[0] = andx;
            queryResult.m_vertexIndex[1] = bndx;
            queryResult.m_vertexIndex[2] = cndx;
            hits.push_back(queryResult);
        }

        m_triangleIndex++;
    }
};

struct CollisionGathererFunctor
{
    CollisionGathererFunctor() : m_renderer(0) { }
    Renderer *m_renderer;
    Qt3DCore::QRay3D m_ray;

    typedef CollisionVisitor::HitList result_type;

    result_type operator ()(const Entity *entity) const
    {
        result_type result;

        HObjectPicker objectPickerHandle = entity->componentHandle<ObjectPicker, 16>();

        // If the Entity which actually received the hit doesn't have
        // an object picker component, we need to check the parent if it has one ...
        while (objectPickerHandle.isNull() && entity != Q_NULLPTR) {
            entity = entity->parent();
            if (entity != Q_NULLPTR)
                objectPickerHandle = entity->componentHandle<ObjectPicker, 16>();
        }

        ObjectPicker *objectPicker = m_renderer->nodeManagers()->objectPickerManager()->data(objectPickerHandle);
        if (objectPicker == Q_NULLPTR)
            return result;  // don't bother picking entities that don't have an object picker

        GeometryRenderer *gRenderer = entity->renderComponent<GeometryRenderer>();
        if (!gRenderer)
            return result;

        Qt3DRender::QRayCastingService rayCasting;

        if (rayHitsEntity(&rayCasting, entity)) {
            CollisionVisitor visitor(m_renderer->nodeManagers(), entity, m_ray);
            visitor.apply(gRenderer, entity->peerUuid());
            result = visitor.hits;

            struct
            {
                bool operator()(const result_type::value_type &a, const result_type::value_type &b)
                {
                    return a.m_distance < b.m_distance;
                }
            } compareHitsDistance;
            std::sort(result.begin(), result.end(), compareHitsDistance);
        }

        return result;
    }

    bool rayHitsEntity(QAbstractCollisionQueryService *rayCasting, const Entity *entity) const
    {
        const QCollisionQueryResult::Hit queryResult = rayCasting->query(m_ray, entity->worldBoundingVolume());
        return queryResult.m_distance >= 0.f;
    }
};

CollisionVisitor::HitList reduceToFirstHit(CollisionVisitor::HitList &result, const CollisionVisitor::HitList &intermediate)
{
    if (!intermediate.empty()) {
        if (result.empty())
            result.push_back(intermediate.front());
        float closest = result.front().m_distance;
        Q_FOREACH (const CollisionVisitor::HitList::value_type& v, intermediate) {
            if (v.m_distance < closest) {
                result.push_front(v);
                closest = v.m_distance;
            }
        }

        while (result.size() > 1)
            result.pop_back();
    }
    return result;
}

// Unordered
CollisionVisitor::HitList reduceToAllHits(CollisionVisitor::HitList &results, const CollisionVisitor::HitList &intermediate)
{
    if (!intermediate.empty())
        results << intermediate;
    return results;
}

} // anonymous

PickBoundingVolumeJob::PickBoundingVolumeJob(Renderer *renderer)
    : m_renderer(renderer)
    , m_manager(renderer->nodeManagers())
    , m_node(Q_NULLPTR)
{
}

void PickBoundingVolumeJob::setRoot(Entity *root)
{
    m_node = root;
}

Qt3DCore::QRay3D PickBoundingVolumeJob::intersectionRay(const QPoint &pos, const QMatrix4x4 &viewMatrix, const QMatrix4x4 &projectionMatrix, const QRect &viewport)
{
    QVector3D nearPos = QVector3D(pos.x(), pos.y(), 0.0f);
    nearPos = nearPos.unproject(viewMatrix, projectionMatrix, viewport);
    QVector3D farPos = QVector3D(pos.x(), pos.y(), 1.0f);
    farPos = farPos.unproject(viewMatrix, projectionMatrix, viewport);

    return Qt3DCore::QRay3D(nearPos,
                            (farPos - nearPos).normalized(),
                            (farPos - nearPos).length());
}

void PickBoundingVolumeJob::run()
{
    m_mouseEvents = m_renderer->pendingPickingEvents();
    if (m_mouseEvents.empty())
        return;

    ViewportCameraGatherer vcGatherer;
    const QVector<ViewportCameraPair> vcPairs = vcGatherer.gather(m_renderer->frameGraphRoot());

    EntityGatherer entitiesGatherer(m_node);

    if (!vcPairs.empty()) {
        Q_FOREACH (const QMouseEvent &event, m_mouseEvents) {
            m_hoveredPickersToClear = m_hoveredPickers;
            ObjectPicker *lastCurrentPicker = m_manager->objectPickerManager()->data(m_currentPicker);

            Q_FOREACH (const ViewportCameraPair &vc, vcPairs) {
                typedef CollisionGathererFunctor::result_type HitList;
                CollisionGathererFunctor gathererFunctor;
                gathererFunctor.m_renderer = m_renderer;
                gathererFunctor.m_ray = rayForViewportAndCamera(event.pos(), vc.viewport, vc.cameraId);
                HitList sphereHits = QtConcurrent::blockingMappedReduced<HitList>(entitiesGatherer.entities(), gathererFunctor, reduceToAllHits);

                // If we have hits
                if (!sphereHits.isEmpty()) {
                    // Note: how can we control that we want the first/last/all elements along the ray to be picked

                    // How do we differentiate betwnee an Entity with no GeometryRenderer and one with one, both having
                    // an ObjectPicker component when it comes

                    // We want to gather hits against triangles
                    // build a triangle based bounding volume

                    Q_FOREACH (const QCollisionQueryResult::Hit &hit, sphereHits) {
                        Entity *entity = m_manager->renderNodesManager()->lookupResource(hit.m_entityId);
                        HObjectPicker objectPickerHandle = entity->componentHandle<ObjectPicker, 16>();

                        // If the Entity which actually received the hit doesn't have
                        // an object picker component, we need to check the parent if it has one ...
                        while (objectPickerHandle.isNull() && entity != Q_NULLPTR) {
                            entity = entity->parent();
                            if (entity != Q_NULLPTR)
                                objectPickerHandle = entity->componentHandle<ObjectPicker, 16>();
                        }

                        ObjectPicker *objectPicker = m_manager->objectPickerManager()->data(objectPickerHandle);

                        if (objectPicker != Q_NULLPTR) {
                            // Send the corresponding event
                            QVector3D localIntersection = hit.m_intersection;
                            if (entity && entity->worldTransform())
                                localIntersection = hit.m_intersection * entity->worldTransform()->inverted();
                            QPickEventPtr pickEvent(new QPickEvent(hit.m_intersection, localIntersection, hit.m_distance,
                                                                   hit.m_triangleIndex, hit.m_vertexIndex[0], hit.m_vertexIndex[1], hit.m_vertexIndex[2]));

                            switch (event.type()) {
                            case QEvent::MouseButtonPress: {
                                // Store pressed object handle
                                m_currentPicker = objectPickerHandle;
                                // Send pressed event to m_currentPicker
                                objectPicker->onPressed(pickEvent);
                            }
                                break;

                            case QEvent::MouseButtonRelease: {
                                // Send release event to m_currentPicker
                                if (lastCurrentPicker != Q_NULLPTR) {
                                    lastCurrentPicker->onClicked(pickEvent);
                                    lastCurrentPicker->onReleased(pickEvent);
                                }
                                break;
                            }

                            case Qt::TapGesture: {
                                objectPicker->onClicked(pickEvent);
                                break;
                            }

                            case QEvent::HoverMove:
                            case QEvent::MouseMove: {
                                if (!m_hoveredPickers.contains(objectPickerHandle)) {
                                    if (objectPicker->hoverEnabled()) {
                                        // Send entered event to objectPicker
                                        objectPicker->onEntered();
                                        // and save it in the hoveredPickers
                                        m_hoveredPickers.push_back(objectPickerHandle);
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                            }
                        }

                        // The ObjectPicker was hit -> it is still being hovered
                        m_hoveredPickersToClear.removeAll(objectPickerHandle);

                        lastCurrentPicker = m_manager->objectPickerManager()->data(m_currentPicker);
                    }

                    // Otherwise no hits
                } else {
                    switch (event.type()) {
                    case QEvent::MouseButtonRelease: {
                        // Send release event to m_currentPicker
                        if (lastCurrentPicker != Q_NULLPTR) {
                            QPickEventPtr pickEvent(new QPickEvent);
                            lastCurrentPicker->onReleased(pickEvent);
                        }
                        break;
                    }
                    default:
                        break;
                    }
                }
            }
        }

        // Clear Hovered elements that needs to be cleared
        // Send exit event to object pickers on which we
        // had set the hovered flag for a previous frame
        // and that aren't being hovered any longer
        clearPreviouslyHoveredPickers();
    }

    // Clear mouse events so that they aren't processed again for the next frame
    m_mouseEvents.clear();
}

void PickBoundingVolumeJob::viewMatrixForCamera(const Qt3DCore::QNodeId &cameraId,
                                                QMatrix4x4 &viewMatrix,
                                                QMatrix4x4 &projectionMatrix) const
{
    Render::CameraLens *lens = Q_NULLPTR;
    Entity *camNode = m_manager->renderNodesManager()->lookupResource(cameraId);
    if (camNode != Q_NULLPTR &&
            (lens = camNode->renderComponent<CameraLens>()) != Q_NULLPTR &&
            lens->isEnabled()) {
        viewMatrix = *camNode->worldTransform();
        projectionMatrix = lens->projection();
    }
}

QRect PickBoundingVolumeJob::windowViewport(const QRectF &relativeViewport) const
{
    // TO DO: find another way to retrieve the size since this won't work with Scene3D
    // const QSize s = m_renderer->surfaceSize();
    // if (s.isValid()) {
    //     const int surfaceWidth = s.width();
    //     const int surfaceHeight = s.height();
    //     return QRect(relativeViewport.x() * surfaceWidth,
    //                  (1.0 - relativeViewport.y() - relativeViewport.height()) * surfaceHeight,
    //                  relativeViewport.width() * surfaceWidth,
    //                  relativeViewport.height() * surfaceHeight);
    // }
    // return relativeViewport.toRect();
    return QRect();
}

Qt3DCore::QRay3D PickBoundingVolumeJob::rayForViewportAndCamera(const QPoint &pos,
                                                                const QRectF &relativeViewport,
                                                                const Qt3DCore::QNodeId &cameraId) const
{
    QMatrix4x4 viewMatrix;
    QMatrix4x4 projectionMatrix;
    viewMatrixForCamera(cameraId, viewMatrix, projectionMatrix);
    const QRect viewport = windowViewport(relativeViewport);

    // TO DO: find another way to retrieve the size since this won't work with Scene3D
    // const QSize s = m_renderer->surfaceSize();
    // // In GL the y is inverted compared to Qt
    // const QPoint glCorrectPos = s.isValid() ? QPoint(pos.x(), s.height() - pos.y()) : pos;
    const QPoint glCorrectPos = pos;
    const Qt3DCore::QRay3D ray = intersectionRay(glCorrectPos, viewMatrix, projectionMatrix, viewport);
    return ray;
}

void PickBoundingVolumeJob::clearPreviouslyHoveredPickers()
{
    Q_FOREACH (const HObjectPicker &pickHandle, m_hoveredPickersToClear) {
        ObjectPicker *pick = m_manager->objectPickerManager()->data(pickHandle);
        if (pick)
            pick->onExited();
        m_hoveredPickers.removeAll(pickHandle);
    }
    m_hoveredPickersToClear.clear();
}

} // namespace Render

} // namespace Qt3DRender

QT_END_NAMESPACE