summaryrefslogtreecommitdiffstats
path: root/src/render/frontend/qlevelofdetail.cpp
blob: fcc826f66592841de85f08ba8aa985b3e89ace79 (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
/****************************************************************************
**
** Copyright (C) 2017 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: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 "qlevelofdetail.h"
#include "qlevelofdetail_p.h"
#include "qcamera.h"

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

QLevelOfDetailPrivate::QLevelOfDetailPrivate()
    : QComponentPrivate()
    , m_camera(nullptr)
    , m_currentIndex(0)
    , m_thresholdType(QLevelOfDetail::DistanceToCameraThreshold)
    , m_volumeOverride()
{
}

void QLevelOfDetailPrivate::setCurrentIndex(int currentIndex)
{
    Q_Q(QLevelOfDetail);
    if (m_currentIndex != currentIndex) {
        m_currentIndex = currentIndex;
        emit q->currentIndexChanged(m_currentIndex);
    }
}

/*!
    \class Qt3DRender::QLevelOfDetail
    \inmodule Qt3DRender
    \since 5.9
    \brief The QLevelOfDetail class provides a way of controlling the complexity
    of rendered entities based on their size on the screen.

    QLevelOfDetail can be used to control the representation of an entity
    based on distance from the observer or size on the screen.

    In order to improve rendering performance, objects that are very small
    can be rendered using far fewer details, in geometry or texture.

    The component is controlled by specifying thresholds of values which are interpreted
    as either distances from the camera or screen size.

    As the point of view changes, the currentIndex property will change to
    reflect matching value in the range array.

    The currentIndex property can then be used, for example, to enable or
    disable entities, change material, etc.

    The LevelOfDetail component is not shareable between multiple \l [QML]{Entity}{entities}.

    \code
     #include <Qt3DCore/QEntity>
     #include <Qt3DRender/QGeometryRenderer>
     #include <Qt3DRender/QLevelOfDetail>

    // Scene
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::Qt3DCore::QEntity;

    Qt3DCore::QEntity *renderableEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QGeometryRenderer *geometryRenderer = new Qt3DCore::QGeometryRenderer(renderableEntity);
    renderableEntity->addComponent(geometryRenderer);
    Qt3DRender::QLevelOfDetail* lod = new Qt3Render::QLevelOfDetail(renderableEntity);
    QVector<qreal> thresholds = {20, 35, 50, 65};
    lod->setThresholds(thresholds);
    lod->setCamera(mainCamera);
    renderableEntity->addComponent(lod);

    // connect to QLevelOfDetail::currentIndexChanged to toggle rendering
    ...
    \endcode
*/

/*!
    \qmltype LevelOfDetail
    \instantiates Qt3DRender::QLevelOfDetail
    \inherits Component3D
    \inqmlmodule Qt3D.Render
    \since 5.9
    \brief LevelOfDetail provides a way of controlling the complexity of
    rendered entities based on their size on the screen.

    LevelOfDetail can be used to control the representation of an entity
    based on distance from the observer or size on the screen.

    In order to improve rendering performance, objects that are very small
    can be rendered using far fewer details, in geometry or texture.

    The component is controlled by specifying thresholds of values which are interpreted
    as either distances from the camera or screen size.

    As the point of view changes, the currentIndex property will change to
    reflect matching value in the range array.

    The currentIndex property can then be used, for example, to enable or
    disable entities, change source in an EntityLoader, or material.

    The LevelOfDetail component is not shareable between multiple Entity's.

    \code
    import Qt3D.Core 2.0
    import Qt3D.Render 2.0

    Entity {
        id: root

        // Scene
        Camera { id: mainCamera }

        LevelOfDetail {
            id: lod
            camera: mainCamera
            thresholds: [20, 35, 50, 65]
            thresholdType: LevelOfDetail.DistanceToCameraThreshold
        }

        CylinderMesh {
            id: mesh

            property var sliceValues: [20, 10, 6, 4]
            slices: sliceValues[lod.currentIndex]
        }

        Entity {
            id: renderableEntity
            components: [ mesh, lod ]
        }
    }
    \endcode
*/


/*!
 * \enum Qt3DRender::QLevelOfDetail::ThresholdType
 *
 * Specifies how the values in the thresholds are interpreted
 *
 * \value DistanceToCameraThreshold Distance from the entity to the selected camera
 * \value ProjectedScreenPixelSizeThreshold Size of the entity when projected on the
 *          screen as seen from the selected camera, expressed in number of
 *          pixels on the side of the bounding square in screen space.
 */

/*!
 * \qmlproperty enumeration LevelOfDetail::ThresholdType
 *
 * Specifies how the values in the thresholds are interpreted
 *
 * \list
 * \li DistanceToCameraThreshold Distance from the entity to the selected camera
 * \li ProjectedScreenPixelSizeThreshold Size of the entity when projected on the
 *     screen as seen from the selected camera, expressed in number of
 *     pixels on the side of the bounding square in screen space.
 * \endlist
 * \sa Qt3DRender::QLevelOfDetail::ThresholdType
 */

/*!
 * \qmlproperty Camera LevelOfDetail::camera
 *
 * Holds the id of the camera that is used to compute the actual distance or the screen size.
 */

/*!
 * \property QLevelOfDetail::camera
 *
 * Holds the id of the camera that is used to compute the actual distance or the screen size.
 */

/*!
 * \qmlproperty int LevelOfDetail::currentIndex
 *
 * The index in the range array which matches the current distance to the camera or screen size.
 */

/*!
 * \property QLevelOfDetail::currentIndex
 *
 * The index in the range array which matches the current distance to the camera or screen size.
 */

/*!
 * \qmlproperty int LevelOfDetail::thresholdType
 *
 * Specifies how range values are interpreted.
 */

/*!
 * \property QLevelOfDetail::thresholdType
 *
 * Specifies how range values are interpreted.
 * \sa Qt3DRender::QLevelOfDetail::ThresholdType
 */

/*!
 * \qmlproperty QVector<qreal> LevelOfDetail::thresholds
 *
 * Array of range values as float point numbers. The value for the most detailed representation
 * should be specified first.
 *
 * If LevelOfDetail::thresholdType is set to LevelOfDetail.DistanceToCameraThreshold, values should
 * be specified in ascending order, in camera space coordinates
 *
 * If LevelOfDetail::thresholdType is set to LevelOfDetail.ProjectedScreenPixelSizeThreshold, values
 * should be specified in descending order, in screen space pixels.
 */

/*!
 * \property QLevelOfDetail::thresholds
 *
 * Array of range values as float point numbers. The value for the most detailed representation
 * should be specified first.
 *
 * If Qt3DRender::QLevelOfDetail::thresholdType is set to
 * Qt3DRender::QLevelOfDetail::DistanceToCameraThreshold, values should be specified in
 * ascending order, in camera space coordinates
 *
 * If Qt3DRender::QLevelOfDetail::thresholdType is set to
 * Qt3DRender::QLevelOfDetail::ProjectedScreenPixelSizeThreshold, values should
 * be specified in descending order, in screen space pixels.
 *
 * \sa Qt3DRender::QLevelOfDetail::ThresholdType
 */

/*!
 * \qmlproperty int LevelOfDetail::volumeOverride
 *
 * Specifies what is used to evaluate the distance or screen size.
 *
 * The default is a sphere of radius 1 and centered at the local origin of the entity.
 * This proxy volume is used to compute the distance to the camera or the size
 * of the screen projection.
 *
 * If this value to null, the bounding volume of the entity is used. Care must be
 * taken that this bounding volume never becomes invalid.
 *
 * \sa Qt3DRender::QLevelOfDetailBoundingSphere
 */

/*!
 * \property QLevelOfDetail::volumeOverride
 *
 * The default is a sphere of radius 1 and centered at the local origin of the entity.
 * This proxy volume is used to compute the distance to the camera or the size
 * of the screen projection.
 *
 * If this value to nullptr, the bounding volume of the entity is used. Care must be
 * taken that this bounding volume never becomes invalid.
 *
 * \sa Qt3DRender::QLevelOfDetailBoundingSphere
 */


/*! \fn Qt3DRender::QLevelOfDetail::QLevelOfDetail(Qt3DCore::QNode *parent)
  Constructs a new QLevelOfDetail with the specified \a parent.
 */
QLevelOfDetail::QLevelOfDetail(QNode *parent)
    : QComponent(*new QLevelOfDetailPrivate, parent)
{
}

/*! \internal */
QLevelOfDetail::~QLevelOfDetail()
{
}

/*! \internal */
QLevelOfDetail::QLevelOfDetail(QLevelOfDetailPrivate &dd, QNode *parent)
    : QComponent(dd, parent)
{
}

QCamera *QLevelOfDetail::camera() const
{
    Q_D(const QLevelOfDetail);
    return d->m_camera;
}

/*!
 * Sets the \a camera relative to which distance and size are computed.
 */
void QLevelOfDetail::setCamera(QCamera *camera)
{
    Q_D(QLevelOfDetail);
    if (d->m_camera != camera) {
        d->m_camera = camera;
        emit cameraChanged(d->m_camera);
    }
}

int QLevelOfDetail::currentIndex() const
{
    Q_D(const QLevelOfDetail);
    return d->m_currentIndex;
}

/*!
 * Sets the \a currentIndex.
 *
 * \note This should not normally be set by the user.
 *
 * However, if the component is disabled, then changing the
 * current index is a simple way of switching between multiple
 * representations.
 */
void QLevelOfDetail::setCurrentIndex(int currentIndex)
{
    Q_D(QLevelOfDetail);
    d->setCurrentIndex(currentIndex);
}

QLevelOfDetail::ThresholdType QLevelOfDetail::thresholdType() const
{
    Q_D(const QLevelOfDetail);
    return d->m_thresholdType;
}

/*!
 * Sets the way thresholds values are interpreted
 * with parameter \a thresholdType
 * \sa Qt3DRender::QLevelOfDetail::ThresholdType
 */
void QLevelOfDetail::setThresholdType(QLevelOfDetail::ThresholdType thresholdType)
{
    Q_D(QLevelOfDetail);
    if (d->m_thresholdType != thresholdType) {
        d->m_thresholdType = thresholdType;
        emit thresholdTypeChanged(d->m_thresholdType);
    }
}

QVector<qreal> QLevelOfDetail::thresholds() const
{
    Q_D(const QLevelOfDetail);
    return d->m_thresholds;
}

QLevelOfDetailBoundingSphere QLevelOfDetail::createBoundingSphere(const QVector3D &center, float radius)
{
    return QLevelOfDetailBoundingSphere(center, radius);
}

/*!
 * Sets the range values in \a thresholds.
 * \sa Qt3DRender::QLevelOfDetail::thresholdType
 */
void QLevelOfDetail::setThresholds(const QVector<qreal> &thresholds)
{
    Q_D(QLevelOfDetail);
    if (d->m_thresholds != thresholds) {
        d->m_thresholds = thresholds;
        emit thresholdsChanged(d->m_thresholds);
    }
}

QLevelOfDetailBoundingSphere QLevelOfDetail::volumeOverride() const
{
    Q_D(const QLevelOfDetail);
    return d->m_volumeOverride;
}

void QLevelOfDetail::setVolumeOverride(const QLevelOfDetailBoundingSphere &volumeOverride)
{
    Q_D(QLevelOfDetail);
    if (d->m_volumeOverride != volumeOverride) {
        d->m_volumeOverride = volumeOverride;
        emit volumeOverrideChanged(volumeOverride);
    }
}

} // namespace Qt3DRender

QT_END_NAMESPACE

#include "moc_qlevelofdetail.cpp"