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
qquickturbulence.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
5
7#include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
8#include <cmath>
9#include <cstdlib>
10#include <QDebug>
11#include <QQmlFile>
13
50 m_strength(10), m_gridSize(0), m_field(nullptr), m_vectorField(nullptr), m_inited(false)
51{
52}
53
55{
56 initializeGrid();
57}
58
60{
61 if (m_field) {
62 for (int i=0; i<m_gridSize; i++)
63 free(m_field[i]);
64 free(m_field);
65 }
66 if (m_vectorField) {
67 for (int i=0; i<m_gridSize; i++)
68 free(m_vectorField[i]);
69 free(m_vectorField);
70 }
71}
72
73void QQuickTurbulenceAffector::initializeGrid()
74{
75 if (!m_inited)
76 return;
77
78 int arg = qMax(width(), height());
79 if (m_gridSize != arg) {
80 if (m_field){ //deallocate and then reallocate grid
81 for (int i=0; i<m_gridSize; i++)
82 free(m_field[i]);
83 free(m_field);
84 }
85 if (m_vectorField) {
86 for (int i=0; i<m_gridSize; i++)
87 free(m_vectorField[i]);
88 free(m_vectorField);
89 }
90 m_gridSize = arg;
91 }
92
93 m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
94 for (int i=0; i<m_gridSize; i++)
95 m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
96 m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
97 for (int i=0; i<m_gridSize; i++)
98 m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
99
101 if (!m_noiseSource.isEmpty())
102 image = QImage(QQmlFile::urlToLocalFileOrQrc(m_noiseSource)).scaled(QSize(m_gridSize, m_gridSize));
103 if (image.isNull())
104 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
105
106 for (int i=0; i<m_gridSize; i++)
107 for (int j=0; j<m_gridSize; j++)
108 m_field[i][j] = qGray(image.pixel(QPoint(i,j)));
109 for (int i=0; i<m_gridSize; i++){
110 for (int j=0; j<m_gridSize; j++){
111 m_vectorField[i][j].setX(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
112 m_vectorField[i][j].setY(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
113 }
114 }
115}
116
117qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
118{
119 if (x < 0)
120 x = 0;
121 if (x >= m_gridSize)
122 x = m_gridSize - 1;
123 if (y < 0)
124 y = 0;
125 if (y >= m_gridSize)
126 y = m_gridSize - 1;
127 return m_field[x][y];
128}
129
130void QQuickTurbulenceAffector::ensureInit()
131{
132 if (m_inited)
133 return;
134 m_inited = true;
135 initializeGrid();
136}
137
139{
140 if (!m_system || !m_enabled)
141 return;
142 ensureInit();
143 if (!m_gridSize)
144 return;
145
146 updateOffsets();//### Needed if an ancestor is transformed.
147
148 QRect boundsRect(0,0,m_gridSize,m_gridSize);
150 if (!activeGroup(gd->index))
151 continue;
152 foreach (QQuickParticleData *d, gd->data){
153 if (!shouldAffect(d))
154 continue;
155 QPoint pos = (QPointF(d->curX(m_system), d->curY(m_system)) - m_offset).toPoint();
156 if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
157 continue;
158 qreal fx = 0.0;
159 qreal fy = 0.0;
160 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
161 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
162 if (fx || fy){
163 d->setInstantaneousVX(d->curVX(m_system)+ fx * dt, m_system);
164 d->setInstantaneousVY(d->curVY(m_system)+ fy * dt, m_system);
165 postAffect(d);
166 }
167 }
168 }
169}
170
172
173#include "moc_qquickturbulence_p.cpp"
\inmodule QtGui
Definition qimage.h:37
QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:209
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:358
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:353
\inmodule QtCore\reentrant
Definition qpoint.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
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:72
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:73
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
void postAffect(QQuickParticleData *datum)
QQuickParticleSystem * m_system
bool shouldAffect(QQuickParticleData *datum)
QVarLengthArray< QQuickParticleGroupData *, 32 > groupData
void affectSystem(qreal dt) override
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
QQuickTurbulenceAffector(QQuickItem *parent=nullptr)
\qmltype Turbulence \instantiates QQuickTurbulenceAffector \inqmlmodule QtQuick.Particles\inherits Af...
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
Combined button and popup list for selecting options.
Definition image.cpp:4
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLint GLint GLint GLint x
[0]
GLint y
constexpr int qGray(int r, int g, int b)
Definition qrgb.h:36
SSL_CTX int void * arg
#define QStringLiteral(str)
double qreal
Definition qtypes.h:187
QObject::connect nullptr