Qt 6.x
The Qt SDK
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
5#include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
6#include <cmath>
7#include <cstdlib>
8#include <QDebug>
9#include <QQmlFile>
11
48 m_strength(10), m_gridSize(0), m_field(nullptr), m_vectorField(nullptr), m_inited(false)
49{
50}
51
53{
54 initializeGrid();
55}
56
58{
59 if (m_field) {
60 for (int i=0; i<m_gridSize; i++)
61 free(m_field[i]);
62 free(m_field);
63 }
64 if (m_vectorField) {
65 for (int i=0; i<m_gridSize; i++)
66 free(m_vectorField[i]);
67 free(m_vectorField);
68 }
69}
70
71void QQuickTurbulenceAffector::initializeGrid()
72{
73 if (!m_inited)
74 return;
75
76 int arg = qMax(width(), height());
77 if (m_gridSize != arg) {
78 if (m_field){ //deallocate and then reallocate grid
79 for (int i=0; i<m_gridSize; i++)
80 free(m_field[i]);
81 free(m_field);
82 }
83 if (m_vectorField) {
84 for (int i=0; i<m_gridSize; i++)
85 free(m_vectorField[i]);
86 free(m_vectorField);
87 }
88 m_gridSize = arg;
89 }
90
91 m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
92 for (int i=0; i<m_gridSize; i++)
93 m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
94 m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
95 for (int i=0; i<m_gridSize; i++)
96 m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
97
99 if (!m_noiseSource.isEmpty())
100 image = QImage(QQmlFile::urlToLocalFileOrQrc(m_noiseSource)).scaled(QSize(m_gridSize, m_gridSize));
101 if (image.isNull())
102 image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
103
104 for (int i=0; i<m_gridSize; i++)
105 for (int j=0; j<m_gridSize; j++)
106 m_field[i][j] = qGray(image.pixel(QPoint(i,j)));
107 for (int i=0; i<m_gridSize; i++){
108 for (int j=0; j<m_gridSize; j++){
109 m_vectorField[i][j].setX(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
110 m_vectorField[i][j].setY(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
111 }
112 }
113}
114
115qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
116{
117 if (x < 0)
118 x = 0;
119 if (x >= m_gridSize)
120 x = m_gridSize - 1;
121 if (y < 0)
122 y = 0;
123 if (y >= m_gridSize)
124 y = m_gridSize - 1;
125 return m_field[x][y];
126}
127
128void QQuickTurbulenceAffector::ensureInit()
129{
130 if (m_inited)
131 return;
132 m_inited = true;
133 initializeGrid();
134}
135
137{
138 if (!m_system || !m_enabled)
139 return;
140 ensureInit();
141 if (!m_gridSize)
142 return;
143
144 updateOffsets();//### Needed if an ancestor is transformed.
145
146 QRect boundsRect(0,0,m_gridSize,m_gridSize);
148 if (!activeGroup(gd->index))
149 continue;
150 foreach (QQuickParticleData *d, gd->data){
151 if (!shouldAffect(d))
152 continue;
153 QPoint pos = (QPointF(d->curX(m_system), d->curY(m_system)) - m_offset).toPoint();
154 if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
155 continue;
156 qreal fx = 0.0;
157 qreal fy = 0.0;
158 fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
159 fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
160 if (fx || fy){
161 d->setInstantaneousVX(d->curVX(m_system)+ fx * dt, m_system);
162 d->setInstantaneousVY(d->curVY(m_system)+ fy * dt, m_system);
163 postAffect(d);
164 }
165 }
166 }
167}
168
170
171#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:208
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:348
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:343
\inmodule QtCore\reentrant
Definition qpoint.h:23
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to QFile.
Definition qqmlfile.cpp:643
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:73
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:74
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
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:483
\inmodule QtCore\reentrant
Definition qrect.h:30
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:851
\inmodule QtCore
Definition qsize.h:25
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1888
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:92
QObject::connect nullptr
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent