1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickturbulence_p.h"
43 #include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
50 \qmlclass Turbulence QQuickTurbulenceAffector
51 \inqmlmodule QtQuick.Particles 2
52 \ingroup qtquick-particles
54 \brief Provides fluid-like forces from a noise image
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.
59 Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element
60 will affect no particles.
62 The source should be relatively smooth black and white noise, such as perlin noise.
65 \qmlproperty real QtQuick.Particles2::Turbulence::strength
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.
72 \qmlproperty url QtQuick.Particles2::Turbulence::noiseSource
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.
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.
82 QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) :
83 QQuickParticleAffector(parent),
84 m_strength(10), m_lastT(0), m_gridSize(0), m_field(0), m_vectorField(0), m_inited(false)
88 void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
93 QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
96 for (int i=0; i<m_gridSize; i++)
101 for (int i=0; i<m_gridSize; i++)
102 free(m_vectorField[i]);
107 void QQuickTurbulenceAffector::initializeGrid()
112 int arg = qMax(width(), height());
113 if (m_gridSize != arg) {
114 if (m_field){ //deallocate and then reallocate grid
115 for (int i=0; i<m_gridSize; i++)
121 for (int i=0; i<m_gridSize; i++)
122 free(m_vectorField[i]);
128 m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
129 for (int i=0; i<m_gridSize; i++)
130 m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
131 m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
132 for (int i=0; i<m_gridSize; i++)
133 m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
136 if (!m_noiseSource.isEmpty())
137 image = QImage(m_noiseSource.toLocalFile()).scaled(QSize(m_gridSize, m_gridSize));
139 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
141 for (int i=0; i<m_gridSize; i++)
142 for (int j=0; j<m_gridSize; j++)
143 m_field[i][j] = qRed(image.pixel(QPoint(i,j)));//Red as proxy for Value
144 for (int i=0; i<m_gridSize; i++){
145 for (int j=0; j<m_gridSize; j++){
146 m_vectorField[i][j].setX(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
147 m_vectorField[i][j].setY(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
152 qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
162 return m_field[x][y];
165 void QQuickTurbulenceAffector::ensureInit()
173 void QQuickTurbulenceAffector::affectSystem(qreal dt)
175 if (!m_system || !m_enabled)
181 updateOffsets();//### Needed if an ancestor is transformed.
183 QRect boundsRect(0,0,m_gridSize,m_gridSize);
184 foreach (QQuickParticleGroupData *gd, m_system->groupData){
185 if (!activeGroup(m_system->groupData.key(gd)))
187 foreach (QQuickParticleData *d, gd->data){
188 if (!shouldAffect(d))
190 QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint();
191 if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
195 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
196 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
198 d->setInstantaneousVX(d->curVX()+ fx * dt);
199 d->setInstantaneousVY(d->curVY()+ fy * dt);