1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickturbulence_p.h"
41#include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
42#include <cmath>
43#include <cstdlib>
44#include <QDebug>
45#include <QQmlFile>
46QT_BEGIN_NAMESPACE
47
48/*!
49 \qmltype Turbulence
50 \instantiates QQuickTurbulenceAffector
51 \inqmlmodule QtQuick.Particles
52 \ingroup qtquick-particles
53 \inherits Affector
54 \brief Provides fluid-like forces from a noise image.
55
56 The Turbulence Element scales the noise source over the area it affects,
57 and uses the curl of that source to generate force vectors.
58
59 Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element
60 will affect no particles.
61
62 The source should be relatively smooth black and white noise, such as perlin noise.
63*/
64/*!
65 \qmlproperty real QtQuick.Particles::Turbulence::strength
66
67 The magnitude of the velocity vector at any point varies between zero and
68 the square root of two. It will then be multiplied by strength to get the
69 velocity per second for the particles affected by the turbulence.
70*/
71/*!
72 \qmlproperty url QtQuick.Particles::Turbulence::noiseSource
73
74 The source image to generate the turbulence from. It will be scaled to the size of the element,
75 so equal or larger sizes will give better results. Tweaking this image is the only way to tweak
76 behavior such as where vortices are or how many exist.
77
78 The source should be a relatively smooth black and white noise image, such as perlin noise.
79 A default image will be used if none is provided.
80*/
81
82QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) :
83 QQuickParticleAffector(parent),
84 m_strength(10), m_lastT(0), m_gridSize(0), m_field(nullptr), m_vectorField(nullptr), m_inited(false)
85{
86}
87
88void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
89{
90 initializeGrid();
91}
92
93QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
94{
95 if (m_field) {
96 for (int i=0; i<m_gridSize; i++)
97 free(ptr: m_field[i]);
98 free(ptr: m_field);
99 }
100 if (m_vectorField) {
101 for (int i=0; i<m_gridSize; i++)
102 free(ptr: m_vectorField[i]);
103 free(ptr: m_vectorField);
104 }
105}
106
107void QQuickTurbulenceAffector::initializeGrid()
108{
109 if (!m_inited)
110 return;
111
112 int arg = qMax(a: width(), b: height());
113 if (m_gridSize != arg) {
114 if (m_field){ //deallocate and then reallocate grid
115 for (int i=0; i<m_gridSize; i++)
116 free(ptr: m_field[i]);
117 free(ptr: m_field);
118 }
119 if (m_vectorField) {
120 for (int i=0; i<m_gridSize; i++)
121 free(ptr: m_vectorField[i]);
122 free(ptr: m_vectorField);
123 }
124 m_gridSize = arg;
125 }
126
127 m_field = (qreal**)malloc(size: m_gridSize * sizeof(qreal*));
128 for (int i=0; i<m_gridSize; i++)
129 m_field[i] = (qreal*)malloc(size: m_gridSize * sizeof(qreal));
130 m_vectorField = (QPointF**)malloc(size: m_gridSize * sizeof(QPointF*));
131 for (int i=0; i<m_gridSize; i++)
132 m_vectorField[i] = (QPointF*)malloc(size: m_gridSize * sizeof(QPointF));
133
134 QImage image;
135 if (!m_noiseSource.isEmpty())
136 image = QImage(QQmlFile::urlToLocalFileOrQrc(m_noiseSource)).scaled(s: QSize(m_gridSize, m_gridSize));
137 if (image.isNull())
138 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(s: QSize(m_gridSize, m_gridSize));
139
140 for (int i=0; i<m_gridSize; i++)
141 for (int j=0; j<m_gridSize; j++)
142 m_field[i][j] = qGray(rgb: image.pixel(pt: QPoint(i,j)));
143 for (int i=0; i<m_gridSize; i++){
144 for (int j=0; j<m_gridSize; j++){
145 m_vectorField[i][j].setX(boundsRespectingField(x: i-1,y: j) - boundsRespectingField(x: i,y: j));
146 m_vectorField[i][j].setY(boundsRespectingField(x: i,y: j) - boundsRespectingField(x: i,y: j-1));
147 }
148 }
149}
150
151qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
152{
153 if (x < 0)
154 x = 0;
155 if (x >= m_gridSize)
156 x = m_gridSize - 1;
157 if (y < 0)
158 y = 0;
159 if (y >= m_gridSize)
160 y = m_gridSize - 1;
161 return m_field[x][y];
162}
163
164void QQuickTurbulenceAffector::ensureInit()
165{
166 if (m_inited)
167 return;
168 m_inited = true;
169 initializeGrid();
170}
171
172void QQuickTurbulenceAffector::affectSystem(qreal dt)
173{
174 if (!m_system || !m_enabled)
175 return;
176 ensureInit();
177 if (!m_gridSize)
178 return;
179
180 updateOffsets();//### Needed if an ancestor is transformed.
181
182 QRect boundsRect(0,0,m_gridSize,m_gridSize);
183 foreach (QQuickParticleGroupData *gd, m_system->groupData){
184 if (!activeGroup(g: gd->index))
185 continue;
186 foreach (QQuickParticleData *d, gd->data){
187 if (!shouldAffect(datum: d))
188 continue;
189 QPoint pos = (QPointF(d->curX(particleSystem: m_system), d->curY(particleSystem: m_system)) - m_offset).toPoint();
190 if (!boundsRect.contains(p: pos,proper: true))//Need to redo bounds checking due to quantization.
191 continue;
192 qreal fx = 0.0;
193 qreal fy = 0.0;
194 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
195 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
196 if (fx || fy){
197 d->setInstantaneousVX(vx: d->curVX(particleSystem: m_system)+ fx * dt, particleSystem: m_system);
198 d->setInstantaneousVY(vy: d->curVY(particleSystem: m_system)+ fy * dt, particleSystem: m_system);
199 postAffect(datum: d);
200 }
201 }
202 }
203}
204
205QT_END_NAMESPACE
206
207#include "moc_qquickturbulence_p.cpp"
208

source code of qtdeclarative/src/particles/qquickturbulence.cpp