1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQuick module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qquickturbulence_p.h"
43 #include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
51 \instantiates QQuickTurbulenceAffector
52 \inqmlmodule QtQuick.Particles 2
53 \ingroup qtquick-particles
55 \brief Provides fluid-like forces from a noise image
57 The Turbulence Element scales the noise source over the area it affects,
58 and uses the curl of that source to generate force vectors.
60 Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element
61 will affect no particles.
63 The source should be relatively smooth black and white noise, such as perlin noise.
66 \qmlproperty real QtQuick.Particles2::Turbulence::strength
68 The magnitude of the velocity vector at any point varies between zero and
69 the square root of two. It will then be multiplied by strength to get the
70 velocity per second for the particles affected by the turbulence.
73 \qmlproperty url QtQuick.Particles2::Turbulence::noiseSource
75 The source image to generate the turbulence from. It will be scaled to the size of the element,
76 so equal or larger sizes will give better results. Tweaking this image is the only way to tweak
77 behavior such as where vortices are or how many exist.
79 The source should be a relatively smooth black and white noise image, such as perlin noise.
80 A default image will be used if none is provided.
83 QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) :
84 QQuickParticleAffector(parent),
85 m_strength(10), m_lastT(0), m_gridSize(0), m_field(0), m_vectorField(0), m_inited(false)
89 void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
94 QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
97 for (int i=0; i<m_gridSize; i++)
102 for (int i=0; i<m_gridSize; i++)
103 free(m_vectorField[i]);
108 void QQuickTurbulenceAffector::initializeGrid()
113 int arg = qMax(width(), height());
114 if (m_gridSize != arg) {
115 if (m_field){ //deallocate and then reallocate grid
116 for (int i=0; i<m_gridSize; i++)
122 for (int i=0; i<m_gridSize; i++)
123 free(m_vectorField[i]);
129 m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
130 for (int i=0; i<m_gridSize; i++)
131 m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
132 m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
133 for (int i=0; i<m_gridSize; i++)
134 m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
137 if (!m_noiseSource.isEmpty())
138 image = QImage(m_noiseSource.toLocalFile()).scaled(QSize(m_gridSize, m_gridSize));
140 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
142 for (int i=0; i<m_gridSize; i++)
143 for (int j=0; j<m_gridSize; j++)
144 m_field[i][j] = qRed(image.pixel(QPoint(i,j)));//Red as proxy for Value
145 for (int i=0; i<m_gridSize; i++){
146 for (int j=0; j<m_gridSize; j++){
147 m_vectorField[i][j].setX(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
148 m_vectorField[i][j].setY(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
153 qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
163 return m_field[x][y];
166 void QQuickTurbulenceAffector::ensureInit()
174 void QQuickTurbulenceAffector::affectSystem(qreal dt)
176 if (!m_system || !m_enabled)
182 updateOffsets();//### Needed if an ancestor is transformed.
184 QRect boundsRect(0,0,m_gridSize,m_gridSize);
185 foreach (QQuickParticleGroupData *gd, m_system->groupData){
186 if (!activeGroup(m_system->groupData.key(gd)))
188 foreach (QQuickParticleData *d, gd->data){
189 if (!shouldAffect(d))
191 QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint();
192 if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
196 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
197 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
199 d->setInstantaneousVX(d->curVX()+ fx * dt);
200 d->setInstantaneousVY(d->curVY()+ fy * dt);