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
53 \brief Turbulence provides fluid like forces based on a noise image.
55 The Turbulence Element scales the noise source over the area it affects,
56 and uses the curl of that source to generate force vectors.
58 Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element
59 will affect no particles.
61 The source should be relatively smooth black and white noise, such as perlin noise.
64 \qmlproperty real QtQuick.Particles2::Turbulence::strength
66 The magnitude of the velocity vector at any point varies between zero and
67 the square root of two. It will then be multiplied by strength to get the
68 velocity per second for the particles affected by the turbulence.
71 \qmlproperty url QtQuick.Particles2::Turbulence::noiseSource
73 The source image to generate the turbulence from. It will be scaled to the size of the element,
74 so equal or larger sizes will give better results. Tweaking this image is the only way to tweak
75 behavior such as where vortices are or how many exist.
77 The source should be a relatively smooth black and white noise image, such as perlin noise.
78 A default image will be used if none is provided.
81 QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) :
82 QQuickParticleAffector(parent),
83 m_strength(10), m_lastT(0), m_gridSize(0), m_field(0), m_vectorField(0), m_inited(false)
87 void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
92 QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
95 for (int i=0; i<m_gridSize; i++)
100 for (int i=0; i<m_gridSize; i++)
101 free(m_vectorField[i]);
106 void QQuickTurbulenceAffector::initializeGrid()
111 int arg = qMax(width(), height());
112 if (m_gridSize != arg) {
113 if (m_field){ //deallocate and then reallocate grid
114 for (int i=0; i<m_gridSize; i++)
120 for (int i=0; i<m_gridSize; i++)
121 free(m_vectorField[i]);
127 m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
128 for (int i=0; i<m_gridSize; i++)
129 m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
130 m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
131 for (int i=0; i<m_gridSize; i++)
132 m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
135 if (!m_noiseSource.isEmpty())
136 image = QImage(m_noiseSource.toLocalFile()).scaled(QSize(m_gridSize, m_gridSize));
138 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
140 for (int i=0; i<m_gridSize; i++)
141 for (int j=0; j<m_gridSize; j++)
142 m_field[i][j] = qRed(image.pixel(QPoint(i,j)));//Red as proxy for Value
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(i,j) - boundsRespectingField(i,j-1));
146 m_vectorField[i][j].setY(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
151 qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
161 return m_field[x][y];
164 void QQuickTurbulenceAffector::ensureInit()
172 void QQuickTurbulenceAffector::affectSystem(qreal dt)
174 if (!m_system || !m_enabled)
180 updateOffsets();//### Needed if an ancestor is transformed.
182 QRect boundsRect(0,0,m_gridSize,m_gridSize);
183 foreach (QQuickParticleGroupData *gd, m_system->groupData){
184 if (!activeGroup(m_system->groupData.key(gd)))
186 foreach (QQuickParticleData *d, gd->data){
187 if (!shouldAffect(d))
189 QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint();
190 if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
194 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
195 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
197 d->setInstantaneousVX(d->curVX()+ fx * dt);
198 d->setInstantaneousVY(d->curVY()+ fy * dt);