2011-06-07 05:12:53 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing/
|
2011-06-07 05:12:53 +00:00
|
|
|
**
|
2012-02-16 04:43:03 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2011-06-07 05:12:53 +00:00
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-09-20 05:21:40 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
2012-09-20 05:21:40 +00:00
|
|
|
**
|
2011-06-07 05:12:53 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-20 05:21:40 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-09-20 05:21:40 +00:00
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-06-07 05:12:53 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
#include "qquickcustomparticle_p.h"
|
Say hello to QtQuick module
This change moves the QtQuick 2 types and C++ API (including
SceneGraph) to a new module (AKA library), QtQuick.
99% of this change is moving files from src/declarative to
src/quick, and from tests/auto/declarative to
tests/auto/qtquick2.
The loading of QtQuick 2 ("import QtQuick 2.0") is now delegated to
a plugin, src/imports/qtquick2, just like it's done for QtQuick 1.
All tools, examples, and tests that use QtQuick C++ API have gotten
"QT += quick" or "QT += quick-private" added to their .pro file.
A few additional internal QtDeclarative classes had to be exported
(via Q_DECLARATIVE_PRIVATE_EXPORT) since they're needed by the
QtQuick 2 implementation.
The old header locations (e.g. QtDeclarative/qquickitem.h) will
still be supported for some time, but will produce compile-time
warnings. (To avoid the QtQuick implementation using the
compatibility headers (since QtDeclarative's includepath comes
first), a few include statements were modified, e.g. from
"#include <qsgnode.h>" to "#include <QtQuick/qsgnode.h>".)
There's a change in qtbase that automatically adds QtQuick to the
module list if QtDeclarative is used. Together with the compatibility
headers, this should help reduce the migration pain for existing
projects.
In theory, simply getting an existing QtDeclarative-based project
to compile and link shouldn't require any changes for now -- but
porting to the new scheme is of course recommended, and will
eventually become mandatory.
Task-number: QTBUG-22889
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Change-Id: Ia52be9373172ba2f37e7623231ecb060316c96a7
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-11-23 14:14:07 +00:00
|
|
|
#include <QtQuick/private/qquickshadereffectmesh_p.h>
|
2013-10-26 17:48:56 +00:00
|
|
|
#include <QtQuick/private/qsgshadersourcebuilder_p.h>
|
2011-06-07 05:12:53 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
2011-06-15 08:52:26 +00:00
|
|
|
|
2011-06-07 05:12:53 +00:00
|
|
|
static QSGGeometry::Attribute PlainParticle_Attributes[] = {
|
2011-09-05 05:50:58 +00:00
|
|
|
QSGGeometry::Attribute::create(0, 2, GL_FLOAT, true), // Position
|
|
|
|
QSGGeometry::Attribute::create(1, 2, GL_FLOAT), // TexCoord
|
|
|
|
QSGGeometry::Attribute::create(2, 4, GL_FLOAT), // Data
|
|
|
|
QSGGeometry::Attribute::create(3, 4, GL_FLOAT), // Vectors
|
|
|
|
QSGGeometry::Attribute::create(4, 1, GL_FLOAT) // r
|
2011-06-07 05:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static QSGGeometry::AttributeSet PlainParticle_AttributeSet =
|
|
|
|
{
|
|
|
|
5, // Attribute Count
|
|
|
|
(2 + 2 + 4 + 4 + 1) * sizeof(float),
|
|
|
|
PlainParticle_Attributes
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PlainVertex {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float tx;
|
|
|
|
float ty;
|
|
|
|
float t;
|
|
|
|
float lifeSpan;
|
|
|
|
float size;
|
|
|
|
float endSize;
|
2011-07-22 07:45:59 +00:00
|
|
|
float vx;
|
|
|
|
float vy;
|
2011-06-07 05:12:53 +00:00
|
|
|
float ax;
|
|
|
|
float ay;
|
|
|
|
float r;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PlainVertices {
|
|
|
|
PlainVertex v1;
|
|
|
|
PlainVertex v2;
|
|
|
|
PlainVertex v3;
|
|
|
|
PlainVertex v4;
|
|
|
|
};
|
|
|
|
|
2011-07-11 01:20:58 +00:00
|
|
|
/*!
|
2012-07-27 15:18:12 +00:00
|
|
|
\qmltype CustomParticle
|
|
|
|
\instantiates QQuickCustomParticle
|
2014-12-05 19:14:20 +00:00
|
|
|
\inqmlmodule QtQuick.Particles
|
2011-07-11 01:20:58 +00:00
|
|
|
\inherits ParticlePainter
|
2012-05-18 13:19:55 +00:00
|
|
|
\brief For specifying shaders to paint particles
|
2012-05-28 17:40:02 +00:00
|
|
|
\ingroup qtquick-particles
|
2011-07-11 01:20:58 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickCustomParticle::QQuickCustomParticle(QQuickItem* parent)
|
|
|
|
: QQuickParticlePainter(parent)
|
2012-03-21 13:21:51 +00:00
|
|
|
, m_dirtyUniforms(true)
|
|
|
|
, m_dirtyUniformValues(true)
|
|
|
|
, m_dirtyTextureProviders(true)
|
|
|
|
, m_dirtyProgram(true)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
setFlag(QQuickItem::ItemHasContents);
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 15:14:45 +00:00
|
|
|
void QQuickCustomParticle::sceneGraphInvalidated()
|
|
|
|
{
|
|
|
|
m_nodes.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickCustomParticle::~QQuickCustomParticle()
|
2011-08-22 11:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
void QQuickCustomParticle::componentComplete()
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.updateShader(this, Key::FragmentShader);
|
|
|
|
updateVertexShader();
|
2011-06-07 05:12:53 +00:00
|
|
|
reset();
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickParticlePainter::componentComplete();
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Trying to keep the shader conventions the same as in qsgshadereffectitem
|
|
|
|
/*!
|
2013-10-01 11:03:28 +00:00
|
|
|
\qmlproperty string QtQuick.Particles::CustomParticle::fragmentShader
|
2011-06-07 05:12:53 +00:00
|
|
|
|
|
|
|
This property holds the fragment shader's GLSL source code.
|
2011-08-30 04:35:09 +00:00
|
|
|
The default shader expects the texture coordinate to be passed from the
|
2011-09-06 00:40:21 +00:00
|
|
|
vertex shader as "varying highp vec2 qt_TexCoord0", and it samples from a
|
2011-08-30 04:35:09 +00:00
|
|
|
sampler2D named "source".
|
2011-06-07 05:12:53 +00:00
|
|
|
*/
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
void QQuickCustomParticle::setFragmentShader(const QByteArray &code)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
if (m_common.source.sourceCode[Key::FragmentShader].constData() == code.constData())
|
2011-06-07 05:12:53 +00:00
|
|
|
return;
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.source.sourceCode[Key::FragmentShader] = code;
|
|
|
|
m_dirtyProgram = true;
|
2011-06-07 05:12:53 +00:00
|
|
|
if (isComponentComplete()) {
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.updateShader(this, Key::FragmentShader);
|
2011-06-07 05:12:53 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
emit fragmentShaderChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-01 11:03:28 +00:00
|
|
|
\qmlproperty string QtQuick.Particles::CustomParticle::vertexShader
|
2011-06-07 05:12:53 +00:00
|
|
|
|
|
|
|
This property holds the vertex shader's GLSL source code.
|
2011-08-30 04:35:09 +00:00
|
|
|
|
|
|
|
The default shader passes the texture coordinate along to the fragment
|
2011-09-06 00:40:21 +00:00
|
|
|
shader as "varying highp vec2 qt_TexCoord0".
|
2011-08-30 04:35:09 +00:00
|
|
|
|
|
|
|
To aid writing a particle vertex shader, the following GLSL code is prepended
|
|
|
|
to your vertex shader:
|
|
|
|
\code
|
2011-09-06 00:40:21 +00:00
|
|
|
attribute highp vec2 qt_ParticlePos;
|
|
|
|
attribute highp vec2 qt_ParticleTex;
|
|
|
|
attribute highp vec4 qt_ParticleData; // x = time, y = lifeSpan, z = size, w = endSize
|
2012-06-25 02:01:46 +00:00
|
|
|
attribute highp vec4 qt_ParticleVec; // x,y = constant velocity, z,w = acceleration
|
2011-09-06 00:40:21 +00:00
|
|
|
attribute highp float qt_ParticleR;
|
2011-08-30 04:35:09 +00:00
|
|
|
uniform highp mat4 qt_Matrix;
|
2011-09-06 00:40:21 +00:00
|
|
|
uniform highp float qt_Timestamp;
|
|
|
|
varying highp vec2 qt_TexCoord0;
|
2011-08-30 04:35:09 +00:00
|
|
|
void defaultMain() {
|
2011-09-06 00:40:21 +00:00
|
|
|
qt_TexCoord0 = qt_ParticleTex;
|
|
|
|
highp float size = qt_ParticleData.z;
|
|
|
|
highp float endSize = qt_ParticleData.w;
|
|
|
|
highp float t = (qt_Timestamp - qt_ParticleData.x) / qt_ParticleData.y;
|
2011-08-30 04:35:09 +00:00
|
|
|
highp float currentSize = mix(size, endSize, t * t);
|
|
|
|
if (t < 0. || t > 1.)
|
|
|
|
currentSize = 0.;
|
2011-09-06 00:40:21 +00:00
|
|
|
highp vec2 pos = qt_ParticlePos
|
|
|
|
- currentSize / 2. + currentSize * qt_ParticleTex // adjust size
|
2012-06-25 02:01:46 +00:00
|
|
|
+ qt_ParticleVec.xy * t * qt_ParticleData.y // apply velocity vector..
|
2011-09-06 00:40:21 +00:00
|
|
|
+ 0.5 * qt_ParticleVec.zw * pow(t * qt_ParticleData.y, 2.);
|
2011-08-30 04:35:09 +00:00
|
|
|
gl_Position = qt_Matrix * vec4(pos.x, pos.y, 0, 1);
|
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
defaultMain() is the same code as in the default shader, you can call this for basic
|
|
|
|
particle functions and then add additional variables for custom effects. Note that
|
|
|
|
the vertex shader for particles is responsible for simulating the movement of particles
|
|
|
|
over time, the particle data itself only has the starting position and spawn time.
|
2011-06-07 05:12:53 +00:00
|
|
|
*/
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
void QQuickCustomParticle::setVertexShader(const QByteArray &code)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
if (m_common.source.sourceCode[Key::VertexShader].constData() == code.constData())
|
2011-06-07 05:12:53 +00:00
|
|
|
return;
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.source.sourceCode[Key::VertexShader] = code;
|
|
|
|
|
|
|
|
m_dirtyProgram = true;
|
2011-06-07 05:12:53 +00:00
|
|
|
if (isComponentComplete()) {
|
2012-03-21 13:21:51 +00:00
|
|
|
updateVertexShader();
|
2011-06-07 05:12:53 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
emit vertexShaderChanged();
|
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
void QQuickCustomParticle::updateVertexShader()
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.disconnectPropertySignals(this, Key::VertexShader);
|
|
|
|
qDeleteAll(m_common.signalMappers[Key::VertexShader]);
|
|
|
|
m_common.uniformData[Key::VertexShader].clear();
|
|
|
|
m_common.signalMappers[Key::VertexShader].clear();
|
|
|
|
m_common.attributes.clear();
|
|
|
|
m_common.attributes.append("qt_ParticlePos");
|
|
|
|
m_common.attributes.append("qt_ParticleTex");
|
|
|
|
m_common.attributes.append("qt_ParticleData");
|
|
|
|
m_common.attributes.append("qt_ParticleVec");
|
|
|
|
m_common.attributes.append("qt_ParticleR");
|
|
|
|
|
|
|
|
UniformData d;
|
|
|
|
d.name = "qt_Matrix";
|
|
|
|
d.specialType = UniformData::Matrix;
|
|
|
|
m_common.uniformData[Key::VertexShader].append(d);
|
|
|
|
m_common.signalMappers[Key::VertexShader].append(0);
|
|
|
|
|
|
|
|
d.name = "qt_Timestamp";
|
|
|
|
d.specialType = UniformData::None;
|
|
|
|
m_common.uniformData[Key::VertexShader].append(d);
|
|
|
|
m_common.signalMappers[Key::VertexShader].append(0);
|
|
|
|
|
|
|
|
const QByteArray &code = m_common.source.sourceCode[Key::VertexShader];
|
|
|
|
if (!code.isEmpty())
|
|
|
|
m_common.lookThroughShaderCode(this, Key::VertexShader, code);
|
|
|
|
|
|
|
|
m_common.connectPropertySignals(this, Key::VertexShader);
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
void QQuickCustomParticle::reset()
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickParticlePainter::reset();
|
2011-06-07 05:12:53 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
QSGNode *QQuickCustomParticle::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickShaderEffectNode *rootNode = static_cast<QQuickShaderEffectNode *>(oldNode);
|
2011-07-04 08:15:28 +00:00
|
|
|
if (m_pleaseReset){
|
2012-03-21 13:21:51 +00:00
|
|
|
delete rootNode;//Automatically deletes children
|
|
|
|
rootNode = 0;
|
2011-07-04 08:15:28 +00:00
|
|
|
m_nodes.clear();
|
2011-06-07 05:12:53 +00:00
|
|
|
m_pleaseReset = false;
|
2012-03-21 13:21:51 +00:00
|
|
|
m_dirtyProgram = true;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 05:27:05 +00:00
|
|
|
if (m_system && m_system->isRunning() && !m_system->isPaused()){
|
2012-03-21 13:21:51 +00:00
|
|
|
rootNode = prepareNextFrame(rootNode);
|
2013-10-29 10:37:57 +00:00
|
|
|
if (rootNode) {
|
|
|
|
foreach (QSGGeometryNode* node, m_nodes)
|
|
|
|
node->markDirty(QSGNode::DirtyGeometry);
|
2011-09-01 05:27:05 +00:00
|
|
|
update();
|
2013-10-29 10:37:57 +00:00
|
|
|
}
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
return rootNode;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickShaderEffectNode *QQuickCustomParticle::prepareNextFrame(QQuickShaderEffectNode *rootNode)
|
|
|
|
{
|
|
|
|
if (!rootNode)
|
|
|
|
rootNode = buildCustomNodes();
|
|
|
|
|
|
|
|
if (!rootNode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (m_dirtyProgram) {
|
2014-04-24 15:14:41 +00:00
|
|
|
const bool isES = QOpenGLContext::currentContext()->isOpenGLES();
|
2014-02-03 16:36:55 +00:00
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickShaderEffectMaterial *material = static_cast<QQuickShaderEffectMaterial *>(rootNode->material());
|
|
|
|
Q_ASSERT(material);
|
|
|
|
|
|
|
|
Key s = m_common.source;
|
2013-10-26 17:48:56 +00:00
|
|
|
QSGShaderSourceBuilder builder;
|
|
|
|
if (s.sourceCode[Key::FragmentShader].isEmpty()) {
|
|
|
|
builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticle.frag"));
|
2014-02-03 16:36:55 +00:00
|
|
|
if (isES)
|
|
|
|
builder.removeVersion();
|
2013-10-26 17:48:56 +00:00
|
|
|
s.sourceCode[Key::FragmentShader] = builder.source();
|
|
|
|
builder.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticletemplate.vert"));
|
2014-02-03 16:36:55 +00:00
|
|
|
if (isES)
|
|
|
|
builder.removeVersion();
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
if (s.sourceCode[Key::VertexShader].isEmpty())
|
2013-10-26 17:48:56 +00:00
|
|
|
builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticle.vert"));
|
|
|
|
s.sourceCode[Key::VertexShader] = builder.source() + s.sourceCode[Key::VertexShader];
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
s.className = metaObject()->className();
|
|
|
|
|
|
|
|
material->setProgramSource(s);
|
|
|
|
material->attributes = m_common.attributes;
|
|
|
|
foreach (QQuickShaderEffectNode* node, m_nodes)
|
|
|
|
node->markDirty(QSGNode::DirtyMaterial);
|
|
|
|
|
|
|
|
m_dirtyProgram = false;
|
|
|
|
m_dirtyUniforms = true;
|
|
|
|
}
|
2011-06-07 05:12:53 +00:00
|
|
|
|
|
|
|
m_lastTime = m_system->systemSync(this) / 1000.;
|
2012-03-21 13:21:51 +00:00
|
|
|
if (true) //Currently this is how we update timestamp... potentially over expensive.
|
|
|
|
buildData(rootNode);
|
|
|
|
return rootNode;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickShaderEffectNode* QQuickCustomParticle::buildCustomNodes()
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2015-02-23 12:45:44 +00:00
|
|
|
typedef QHash<int, QQuickShaderEffectNode*>::const_iterator NodeHashConstIt;
|
|
|
|
|
2014-10-14 13:31:05 +00:00
|
|
|
if (!QOpenGLContext::currentContext())
|
|
|
|
return 0;
|
|
|
|
|
2014-04-24 15:14:41 +00:00
|
|
|
if (QOpenGLContext::currentContext()->isOpenGLES() && m_count * 4 > 0xffff) {
|
2011-08-23 03:08:08 +00:00
|
|
|
printf("CustomParticle: Too many particles... \n");
|
2011-06-07 05:12:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-04 08:15:28 +00:00
|
|
|
if (m_count <= 0) {
|
2011-06-07 05:12:53 +00:00
|
|
|
printf("CustomParticle: Too few particles... \n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
if (m_groups.isEmpty())
|
|
|
|
return 0;
|
2011-08-22 11:48:13 +00:00
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickShaderEffectNode *rootNode = 0;
|
|
|
|
QQuickShaderEffectMaterial *material = new QQuickShaderEffectMaterial;
|
|
|
|
m_dirtyProgram = true;
|
2011-08-22 11:48:13 +00:00
|
|
|
|
2011-09-13 10:23:27 +00:00
|
|
|
foreach (const QString &str, m_groups){
|
2011-09-29 01:22:50 +00:00
|
|
|
int gIdx = m_system->groupIds[str];
|
|
|
|
int count = m_system->groupData[gIdx]->size();
|
2011-09-23 04:59:06 +00:00
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickShaderEffectNode* node = new QQuickShaderEffectNode();
|
2011-09-23 04:59:06 +00:00
|
|
|
m_nodes.insert(gIdx, node);
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
node->setMaterial(material);
|
2011-09-23 04:59:06 +00:00
|
|
|
|
2011-07-04 08:15:28 +00:00
|
|
|
//Create Particle Geometry
|
|
|
|
int vCount = count * 4;
|
|
|
|
int iCount = count * 6;
|
|
|
|
QSGGeometry *g = new QSGGeometry(PlainParticle_AttributeSet, vCount, iCount);
|
|
|
|
g->setDrawingMode(GL_TRIANGLES);
|
2011-09-23 04:59:06 +00:00
|
|
|
node->setGeometry(g);
|
2012-03-21 13:21:51 +00:00
|
|
|
node->setFlag(QSGNode::OwnsGeometry, true);
|
2011-07-04 08:15:28 +00:00
|
|
|
PlainVertex *vertices = (PlainVertex *) g->vertexData();
|
|
|
|
for (int p=0; p < count; ++p) {
|
|
|
|
commit(gIdx, p);
|
|
|
|
vertices[0].tx = 0;
|
|
|
|
vertices[0].ty = 0;
|
|
|
|
|
|
|
|
vertices[1].tx = 1;
|
|
|
|
vertices[1].ty = 0;
|
|
|
|
|
|
|
|
vertices[2].tx = 0;
|
|
|
|
vertices[2].ty = 1;
|
|
|
|
|
|
|
|
vertices[3].tx = 1;
|
|
|
|
vertices[3].ty = 1;
|
|
|
|
vertices += 4;
|
|
|
|
}
|
|
|
|
quint16 *indices = g->indexDataAsUShort();
|
|
|
|
for (int i=0; i < count; ++i) {
|
|
|
|
int o = i * 4;
|
|
|
|
indices[0] = o;
|
|
|
|
indices[1] = o + 1;
|
|
|
|
indices[2] = o + 2;
|
|
|
|
indices[3] = o + 1;
|
|
|
|
indices[4] = o + 3;
|
|
|
|
indices[5] = o + 2;
|
|
|
|
indices += 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 12:45:44 +00:00
|
|
|
NodeHashConstIt it = m_nodes.begin();
|
2012-03-21 13:21:51 +00:00
|
|
|
rootNode = it.value();
|
|
|
|
rootNode->setFlag(QSGNode::OwnsMaterial, true);
|
2015-02-23 12:45:44 +00:00
|
|
|
const NodeHashConstIt cend = m_nodes.end();
|
|
|
|
for (++it; it != cend; ++it)
|
2012-03-21 13:21:51 +00:00
|
|
|
rootNode->appendChildNode(it.value());
|
|
|
|
|
|
|
|
return rootNode;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
void QQuickCustomParticle::sourceDestroyed(QObject *object)
|
|
|
|
{
|
|
|
|
m_common.sourceDestroyed(object);
|
|
|
|
}
|
2011-06-07 05:12:53 +00:00
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
void QQuickCustomParticle::propertyChanged(int mappedId)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2012-03-21 13:21:51 +00:00
|
|
|
bool textureProviderChanged;
|
|
|
|
m_common.propertyChanged(this, mappedId, &textureProviderChanged);
|
|
|
|
m_dirtyTextureProviders |= textureProviderChanged;
|
|
|
|
m_dirtyUniformValues = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QQuickCustomParticle::buildData(QQuickShaderEffectNode *rootNode)
|
|
|
|
{
|
|
|
|
if (!rootNode)
|
2011-06-07 05:12:53 +00:00
|
|
|
return;
|
2012-03-21 13:21:51 +00:00
|
|
|
for (int shaderType = 0; shaderType < Key::ShaderTypeCount; ++shaderType) {
|
|
|
|
for (int i = 0; i < m_common.uniformData[shaderType].size(); ++i) {
|
|
|
|
if (m_common.uniformData[shaderType].at(i).name == "qt_Timestamp")
|
|
|
|
m_common.uniformData[shaderType][i].value = qVariantFromValue(m_lastTime);
|
|
|
|
}
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
2012-03-21 13:21:51 +00:00
|
|
|
m_common.updateMaterial(rootNode, static_cast<QQuickShaderEffectMaterial *>(rootNode->material()),
|
|
|
|
m_dirtyUniforms, true, m_dirtyTextureProviders);
|
2011-10-14 08:51:42 +00:00
|
|
|
foreach (QQuickShaderEffectNode* node, m_nodes)
|
2011-07-04 08:15:28 +00:00
|
|
|
node->markDirty(QSGNode::DirtyMaterial);
|
2012-03-21 13:21:51 +00:00
|
|
|
m_dirtyUniforms = m_dirtyUniformValues = m_dirtyTextureProviders = false;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
void QQuickCustomParticle::initialize(int gIdx, int pIdx)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickParticleData* datum = m_system->groupData[gIdx]->data[pIdx];
|
2011-07-04 08:15:28 +00:00
|
|
|
datum->r = rand()/(qreal)RAND_MAX;
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
void QQuickCustomParticle::commit(int gIdx, int pIdx)
|
2011-06-07 05:12:53 +00:00
|
|
|
{
|
2011-07-04 08:15:28 +00:00
|
|
|
if (m_nodes[gIdx] == 0)
|
2011-06-07 05:12:53 +00:00
|
|
|
return;
|
|
|
|
|
2011-10-24 21:57:54 +00:00
|
|
|
QQuickParticleData* datum = m_system->groupData[gIdx]->data[pIdx];
|
2011-07-04 08:15:28 +00:00
|
|
|
PlainVertices *particles = (PlainVertices *) m_nodes[gIdx]->geometry()->vertexData();
|
|
|
|
PlainVertex *vertices = (PlainVertex *)&particles[pIdx];
|
2011-06-21 02:02:08 +00:00
|
|
|
for (int i=0; i<4; ++i) {
|
2011-07-04 08:15:28 +00:00
|
|
|
vertices[i].x = datum->x - m_systemOffset.x();
|
|
|
|
vertices[i].y = datum->y - m_systemOffset.y();
|
|
|
|
vertices[i].t = datum->t;
|
|
|
|
vertices[i].lifeSpan = datum->lifeSpan;
|
|
|
|
vertices[i].size = datum->size;
|
|
|
|
vertices[i].endSize = datum->endSize;
|
2011-07-22 07:45:59 +00:00
|
|
|
vertices[i].vx = datum->vx;
|
|
|
|
vertices[i].vy = datum->vy;
|
2011-07-04 08:15:28 +00:00
|
|
|
vertices[i].ax = datum->ax;
|
|
|
|
vertices[i].ay = datum->ay;
|
|
|
|
vertices[i].r = datum->r;
|
2011-06-21 02:02:08 +00:00
|
|
|
}
|
2011-06-07 05:12:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 13:21:51 +00:00
|
|
|
void QQuickCustomParticle::itemChange(ItemChange change, const ItemChangeData &value)
|
|
|
|
{
|
|
|
|
if (change == QQuickItem::ItemSceneChange)
|
2012-07-11 07:32:16 +00:00
|
|
|
m_common.updateWindow(value.window);
|
2012-03-21 13:21:51 +00:00
|
|
|
QQuickParticlePainter::itemChange(change, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-07 05:12:53 +00:00
|
|
|
QT_END_NAMESPACE
|