Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquick3dparticlecustomshape.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
9#include <QtQml/qqmlcontext.h>
10#include <QtQml/qqmlfile.h>
11#include <QtCore/qfile.h>
12
14
57
65{
66 return m_source;
67}
68
78{
79 return m_random;
80}
81
83{
84 if (m_source == source)
85 return;
86
87 m_source = source;
88
89 loadFromSource();
91}
92
94{
95 if (m_random == random)
96 return;
97
98 m_random = random;
99 if (m_random)
100 m_randomizeDirty = true;
102}
103
104void QQuick3DParticleCustomShape::loadFromSource()
105{
106 m_positions.clear();
107
108 // Get path to file
109 const QQmlContext *context = qmlContext(this);
110 QString dataFilePath = QQmlFile::urlToLocalFileOrQrc(context ? context->resolvedUrl(m_source) : m_source);
111
112 QFile dataFile(dataFilePath);
113 if (!dataFile.open(QIODevice::ReadOnly)) {
114 // Invalid file
115 qWarning() << "Unable to open file:" << dataFilePath;
116 return;
117 }
118 QCborStreamReader reader(&dataFile);
119
120 // Check that file is proper CBOR and get the version
122
123 if (version == -1) {
124 // Invalid file
125 qWarning() << "Invalid shape data version:" << version;
126 return;
127 }
128
129 // Start positions array
130 reader.enterContainer();
131
132 while (reader.lastError() == QCborError::NoError && reader.hasNext()) {
133 QVector3D pos = QQuick3DParticleShapeDataUtils::readValue(reader, QMetaType::QVector3D).value<QVector3D>();
134 m_positions.append(pos);
135 }
136
137 // Leave positions array
138 reader.leaveContainer();
139
140 // Leave root array
141 reader.leaveContainer();
142
143 if (m_random)
144 m_randomizeDirty = true;
145}
146
147void QQuick3DParticleCustomShape::doRandomizeData()
148{
149 if (!m_system || m_positions.isEmpty())
150 return;
151
152 auto rand = m_system->rand();
153 int seed = rand->get(0, QPRand::Shape1) * float(INT_MAX);
154 std::shuffle(m_positions.begin(), m_positions.end(), std::default_random_engine(seed));
155
156 m_randomizeDirty = false;
157}
158
160{
161 auto *parent = parentNode();
162 if (!parent || m_positions.isEmpty())
163 return QVector3D();
164
165 if (m_randomizeDirty)
166 doRandomizeData();
167
168 int index = particleIndex % m_positions.size();
169 return m_positions.at(index) * parent->scale();
170}
171
\inmodule QtCore\reentrant
\inmodule QtCore
Definition qfile.h:93
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
iterator end()
Definition qlist.h:626
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
iterator begin()
Definition qlist.h:625
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
QQuick3DParticleCustomShape(QObject *parent=nullptr)
\qmltype ParticleCustomShape3D \inherits ParticleAbtractShape3D \inqmlmodule QtQuick3D....
QVector3D getPosition(int particleIndex) override
static int readShapeHeader(QCborStreamReader &reader)
static QVariant readValue(QCborStreamReader &reader, QMetaType::Type type)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qurl.h:94
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
Combined button and popup list for selecting options.
QImageReader reader("image.png")
[1]
static void * context
#define qWarning
Definition qlogging.h:166
GLuint index
[2]
GLsizei GLsizei GLchar * source
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_EMIT