summaryrefslogtreecommitdiffstats
path: root/src/render/picking/raycaster.cpp
blob: 999e71a22c157d7232fd11550c37e91725df9d04 (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
/****************************************************************************
**
** 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:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "raycaster_p.h"
#include "qpickevent.h"
#include <Qt3DRender/private/abstractrenderer_p.h>
#include <Qt3DRender/qabstractraycaster.h>
#include <Qt3DRender/qlayer.h>
#include <Qt3DRender/private/qabstractraycaster_p.h>
#include <Qt3DRender/private/raycastingjob_p.h>
#include <Qt3DRender/private/qrenderaspect_p.h>

QT_BEGIN_NAMESPACE

using namespace Qt3DCore;

namespace Qt3DRender {

namespace Render {

RayCaster::RayCaster()
    : BackendNode(QBackendNode::ReadWrite)
{
}

RayCaster::~RayCaster()
{
    notifyJob();
}

QAbstractRayCasterPrivate::RayCasterType RayCaster::type() const
{
    return m_type;
}

QAbstractRayCaster::RunMode RayCaster::runMode() const
{
    return m_runMode;
}

QVector3D RayCaster::origin() const
{
    return m_origin;
}

QVector3D RayCaster::direction() const
{
    return m_direction;
}

float RayCaster::length() const
{
    return m_length;
}

QPoint RayCaster::position() const
{
    return m_position;
}

Qt3DCore::QNodeIdVector RayCaster::layerIds() const
{
    return m_layerIds;
}

QAbstractRayCaster::FilterMode RayCaster::filterMode() const
{
    return m_filterMode;
}

void RayCaster::cleanup()
{
    BackendNode::setEnabled(false);
    m_type = QAbstractRayCasterPrivate::WorldSpaceRayCaster;
    m_runMode = QAbstractRayCaster::SingleShot;
    m_direction = QVector3D(0.f, 0.f, 1.f);
    m_origin = {};
    m_length = 0.f;
    m_position = {};
    m_filterMode = QAbstractRayCaster::AcceptAllMatchingLayers;
    m_layerIds.clear();
    notifyJob();
}

void RayCaster::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
{
    const QAbstractRayCaster *node = qobject_cast<const QAbstractRayCaster *>(frontEnd);
    if (!node)
        return;

    BackendNode::syncFromFrontEnd(frontEnd, firstTime);

    if (node->runMode() != m_runMode) {
        m_runMode = node->runMode();
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    if (node->filterMode() != m_filterMode) {
        m_filterMode = node->filterMode();
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    const Qt3DCore::QNodeIdVector layerIds = Qt3DCore::qIdsForNodes(node->layers());
    if (layerIds != m_layerIds) {
        m_layerIds = layerIds;
        markDirty(AbstractRenderer::LayersDirty);
        notifyJob();
    }

    const QAbstractRayCasterPrivate *d = static_cast<const QAbstractRayCasterPrivate *>(QNodePrivate::get(node));
    if (d->m_direction != m_direction) {
        m_direction = d->m_direction;
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    if (!qFuzzyCompare(d->m_length, m_length)) {
        m_length = d->m_length;
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    if (d->m_origin != m_origin) {
        m_origin = d->m_origin;
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    if (d->m_position != m_position) {
        m_position = d->m_position;
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }

    if (d->m_rayCasterType != m_type) {
        m_type = d->m_rayCasterType;
        notifyJob();
        markDirty(AbstractRenderer::AllDirty);
    }
}

void RayCaster::notifyJob()
{
    if (m_renderer && m_renderer->aspect())
        QRenderAspectPrivate::get(m_renderer->aspect())->m_rayCastingJob->markCastersDirty();
}

} // Render

} // Qt3DRender

QT_END_NAMESPACE