diff options
Diffstat (limited to 'src/imports/particles/superparticle.cpp')
-rw-r--r-- | src/imports/particles/superparticle.cpp | 511 |
1 files changed, 0 insertions, 511 deletions
diff --git a/src/imports/particles/superparticle.cpp b/src/imports/particles/superparticle.cpp deleted file mode 100644 index 811b6a4ba8..0000000000 --- a/src/imports/particles/superparticle.cpp +++ /dev/null @@ -1,511 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the Declarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <private/qsgcontext_p.h> -#include <private/qsgadaptationlayer_p.h> -#include <qsgnode.h> -#include <qsgtexturematerial.h> -#include <qsgtexture.h> -#include <QFile> -#include "superparticle.h" -#include "particleemitter.h" -#include <QGLFunctions> -#include <qsgengine.h> - -QT_BEGIN_NAMESPACE - -const float CONV = 0.017453292519943295; -class SuperMaterial : public QSGMaterial -{ -public: - SuperMaterial() - : timestamp(0) - { - setFlag(Blending, true); - } - - ~SuperMaterial() - { - delete texture; - delete colortable; - delete sizetable; - delete opacitytable; - } - - virtual QSGMaterialType *type() const { static QSGMaterialType type; return &type; } - virtual QSGMaterialShader *createShader() const; - virtual int compare(const QSGMaterial *other) const - { - return this - static_cast<const SuperMaterial *>(other); - } - - QSGTexture *texture; - QSGTexture *colortable; - QSGTexture *sizetable; - QSGTexture *opacitytable; - - qreal timestamp; -}; - - -class SuperMaterialData : public QSGMaterialShader -{ -public: - SuperMaterialData(const char *vertexFile = 0, const char *fragmentFile = 0) - { - QFile vf(vertexFile ? vertexFile : ":resources/supervertex.shader"); - vf.open(QFile::ReadOnly); - m_vertex_code = vf.readAll(); - - QFile ff(fragmentFile ? fragmentFile : ":resources/superfragment.shader"); - ff.open(QFile::ReadOnly); - m_fragment_code = ff.readAll(); - - Q_ASSERT(!m_vertex_code.isNull()); - Q_ASSERT(!m_fragment_code.isNull()); - } - - void deactivate() { - QSGMaterialShader::deactivate(); - - for (int i=0; i<8; ++i) { - program()->setAttributeArray(i, GL_FLOAT, chunkOfBytes, 1, 0); - } - } - - virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *) - { - SuperMaterial *m = static_cast<SuperMaterial *>(newEffect); - state.context()->functions()->glActiveTexture(GL_TEXTURE0); - m->texture->bind(); - - state.context()->functions()->glActiveTexture(GL_TEXTURE1); - m->colortable->bind(); - program()->setUniformValue(m_colortable_id, 1); - - state.context()->functions()->glActiveTexture(GL_TEXTURE2); - m->sizetable->bind(); - program()->setUniformValue(m_sizetable_id, 2); - - state.context()->functions()->glActiveTexture(GL_TEXTURE3); - m->opacitytable->bind(); - program()->setUniformValue(m_opacitytable_id, 3); - - program()->setUniformValue(m_opacity_id, state.opacity()); - program()->setUniformValue(m_timestamp_id, (float) m->timestamp); - - if (state.isMatrixDirty()) - program()->setUniformValue(m_matrix_id, state.combinedMatrix()); - } - - virtual void initialize() { - m_colortable_id = program()->uniformLocation("colortable"); - m_sizetable_id = program()->uniformLocation("sizetable"); - m_opacitytable_id = program()->uniformLocation("opacitytable"); - m_matrix_id = program()->uniformLocation("matrix"); - m_opacity_id = program()->uniformLocation("opacity"); - m_timestamp_id = program()->uniformLocation("timestamp"); - } - - virtual const char *vertexShader() const { return m_vertex_code.constData(); } - virtual const char *fragmentShader() const { return m_fragment_code.constData(); } - - virtual char const *const *attributeNames() const { - static const char *attr[] = { - "vPos", - "vTex", - "vData", - "vVec", - "vColor", - "vDeformVec", - "vRotation", - 0 - }; - return attr; - } - - virtual bool isColorTable() const { return false; } - - int m_matrix_id; - int m_opacity_id; - int m_timestamp_id; - int m_colortable_id; - int m_sizetable_id; - int m_opacitytable_id; - - QByteArray m_vertex_code; - QByteArray m_fragment_code; - - static float chunkOfBytes[1024]; -}; -float SuperMaterialData::chunkOfBytes[1024]; - - -QSGMaterialShader *SuperMaterial::createShader() const -{ - return new SuperMaterialData; -} - -SuperParticle::SuperParticle(QSGItem* parent) - : ParticleType(parent) - , m_do_reset(false) - , m_color(Qt::white) - , m_color_variation(0.5) - , m_node(0) - , m_material(0) - , m_alphaVariation(0.0) - , m_alpha(1.0) - , m_redVariation(0.0) - , m_greenVariation(0.0) - , m_blueVariation(0.0) -{ - setFlag(ItemHasContents); -} - -void SuperParticle::setImage(const QUrl &image) -{ - if (image == m_image_name) - return; - m_image_name = image; - emit imageChanged(); - reset(); -} - - -void SuperParticle::setColortable(const QUrl &table) -{ - if (table == m_colortable_name) - return; - m_colortable_name = table; - emit colortableChanged(); - reset(); -} - -void SuperParticle::setSizetable(const QUrl &table) -{ - if (table == m_sizetable_name) - return; - m_sizetable_name = table; - emit sizetableChanged(); - reset(); -} - -void SuperParticle::setOpacitytable(const QUrl &table) -{ - if (table == m_opacitytable_name) - return; - m_opacitytable_name = table; - emit opacitytableChanged(); - reset(); -} - -void SuperParticle::setColor(const QColor &color) -{ - if (color == m_color) - return; - m_color = color; - emit colorChanged(); - //m_system->pleaseReset();//XXX -} - -void SuperParticle::setColorVariation(qreal var) -{ - if (var == m_color_variation) - return; - m_color_variation = var; - emit colorVariationChanged(); - //m_system->pleaseReset();//XXX -} - -void SuperParticle::setCount(int c) -{ - ParticleType::setCount(c); - m_pleaseReset = true; -} - -void SuperParticle::reset() -{ - ParticleType::reset(); - m_pleaseReset = true; -} - -static QSGGeometry::Attribute SuperParticle_Attributes[] = { - { 0, 2, GL_FLOAT }, // Position - { 1, 2, GL_FLOAT }, // TexCoord - { 2, 4, GL_FLOAT }, // Data - { 3, 4, GL_FLOAT }, // Vectors - { 4, 4, GL_UNSIGNED_BYTE }, // Colors - { 5, 4, GL_FLOAT }, // DeformationVectors - { 6, 3, GL_FLOAT } // Rotation -}; - -static QSGGeometry::AttributeSet SuperParticle_AttributeSet = -{ - 7, // Attribute Count - (2 + 2 + 4 + 4 + 4 + 3) * sizeof(float) + 4 * sizeof(uchar), - SuperParticle_Attributes -}; - -QSGGeometryNode* SuperParticle::buildParticleNode() -{ - if (m_count * 4 > 0xffff) { - printf("SuperParticle: Too many particles... \n"); - return 0; - } - - if(m_count <= 0) { - printf("SuperParticle: Too few particles... \n"); - return 0; - } - - QImage image(m_image_name.toLocalFile()); - if (image.isNull()) { - printf("SuperParticle: loading image failed... '%s'\n", qPrintable(m_image_name.toLocalFile())); - return 0; - } - - int vCount = m_count * 4; - int iCount = m_count * 6; - - QSGGeometry *g = new QSGGeometry(SuperParticle_AttributeSet, vCount, iCount); - g->setDrawingMode(GL_TRIANGLES); - - SuperVertex *vertices = (SuperVertex *) g->vertexData(); - for (int p=0; p<m_count; ++p) { - - for (int i=0; i<4; ++i) { - vertices[i].x = 0; - vertices[i].y = 0; - vertices[i].t = -1; - vertices[i].lifeSpan = 0; - vertices[i].size = 0; - vertices[i].endSize = 0; - vertices[i].sx = 0; - vertices[i].sy = 0; - vertices[i].ax = 0; - vertices[i].ay = 0; - vertices[i].xx = 1; - vertices[i].xy = 0; - vertices[i].yx = 0; - vertices[i].yy = 1; - vertices[i].rotation = 0; - vertices[i].rotationSpeed = 0; - vertices[i].autoRotate = 0; - } - - 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<m_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; - } - - if (m_material) { - delete m_material; - m_material = 0; - } - - QImage colortable(m_colortable_name.toLocalFile()); - QImage sizetable(m_sizetable_name.toLocalFile()); - QImage opacitytable(m_opacitytable_name.toLocalFile()); - m_material = new SuperMaterial(); - if(colortable.isNull()) - colortable = QImage(":resources/identitytable.png"); - if(sizetable.isNull()) - sizetable = QImage(":resources/identitytable.png"); - if(opacitytable.isNull()) - opacitytable = QImage(":resources/defaultFadeInOut.png"); - Q_ASSERT(!colortable.isNull()); - Q_ASSERT(!sizetable.isNull()); - Q_ASSERT(!opacitytable.isNull()); - m_material->colortable = sceneGraphEngine()->createTextureFromImage(colortable); - m_material->sizetable = sceneGraphEngine()->createTextureFromImage(sizetable); - m_material->opacitytable = sceneGraphEngine()->createTextureFromImage(opacitytable); - - m_material->texture = sceneGraphEngine()->createTextureFromImage(image); - m_material->texture->setFiltering(QSGTexture::Linear); - - m_node = new QSGGeometryNode(); - m_node->setGeometry(g); - m_node->setMaterial(m_material); - - m_last_particle = 0; - - return m_node; -} - -QSGNode *SuperParticle::updatePaintNode(QSGNode *, UpdatePaintNodeData *) -{ - if(m_pleaseReset){ - if(m_node) - delete m_node; - if(m_material) - delete m_material; - - m_node = 0; - m_material = 0; - m_pleaseReset = false; - } - - if(m_system && m_system->isRunning()) - prepareNextFrame(); - if (m_node){ - update(); - m_node->markDirty(QSGNode::DirtyMaterial); - } - - return m_node; -} - -void SuperParticle::prepareNextFrame() -{ - if (m_node == 0){ //TODO: Staggered loading (as emitted) - m_node = buildParticleNode(); - if(m_node == 0) - return; - } - qint64 timeStamp = m_system->systemSync(this); - - qreal time = timeStamp / 1000.; - m_material->timestamp = time; -} - -void SuperParticle::reloadColor(const Color4ub &c, ParticleData* d) -{ - SuperVertices *particles = (SuperVertices *) m_node->geometry()->vertexData(); - int pos = particleTypeIndex(d); - SuperVertices &p = particles[pos]; - p.v1.color = p.v2.color = p.v3.color = p.v4.color = c; -} - -void SuperParticle::vertexCopy(SuperVertex &b,const ParticleVertex& a) -{ - b.x = a.x - m_systemOffset.x(); - b.y = a.y - m_systemOffset.y(); - b.t = a.t; - b.lifeSpan = a.lifeSpan; - b.size = a.size; - b.endSize = a.endSize; - b.sx = a.sx; - b.sy = a.sy; - b.ax = a.ax; - b.ay = a.ay; -} - -void SuperParticle::reload(ParticleData *d) -{ - if (m_node == 0) - return; - - SuperVertices *particles = (SuperVertices *) m_node->geometry()->vertexData(); - - int pos = particleTypeIndex(d); - - SuperVertices &p = particles[pos]; - - //Perhaps we could be more efficient? - vertexCopy(p.v1, d->pv); - vertexCopy(p.v2, d->pv); - vertexCopy(p.v3, d->pv); - vertexCopy(p.v4, d->pv); -} - -void SuperParticle::load(ParticleData *d) -{ - if (m_node == 0) - return; - - //Color initialization - // Particle color - Color4ub color; - qreal redVariation = m_color_variation + m_redVariation; - qreal greenVariation = m_color_variation + m_greenVariation; - qreal blueVariation = m_color_variation + m_blueVariation; - color.r = m_color.red() * (1 - redVariation) + rand() % 256 * redVariation; - color.g = m_color.green() * (1 - greenVariation) + rand() % 256 * greenVariation; - color.b = m_color.blue() * (1 - blueVariation) + rand() % 256 * blueVariation; - color.a = m_alpha * m_color.alpha() * (1 - m_alphaVariation) + rand() % 256 * m_alphaVariation; - SuperVertices *particles = (SuperVertices *) m_node->geometry()->vertexData(); - SuperVertices &p = particles[particleTypeIndex(d)]; - p.v1.color = p.v2.color = p.v3.color = p.v4.color = color; - if(m_xVector){ - const QPointF &ret = m_xVector->sample(QPointF(d->pv.x, d->pv.y)); - p.v1.xx = p.v2.xx = p.v3.xx = p.v4.xx = ret.x(); - p.v1.xy = p.v2.xy = p.v3.xy = p.v4.xy = ret.y(); - } - if(m_yVector){ - const QPointF &ret = m_yVector->sample(QPointF(d->pv.x, d->pv.y)); - p.v1.yx = p.v2.yx = p.v3.yx = p.v4.yx = ret.x(); - p.v1.yy = p.v2.yy = p.v3.yy = p.v4.yy = ret.y(); - } - p.v1.rotation = p.v2.rotation = p.v3.rotation = p.v4.rotation = - (m_rotation + (m_rotationVariation - 2*((qreal)rand()/RAND_MAX)*m_rotationVariation) ) * CONV; - p.v1.rotationSpeed = p.v2.rotationSpeed = p.v3.rotationSpeed = p.v4.rotationSpeed = - (m_rotationSpeed + (m_rotationSpeedVariation - 2*((qreal)rand()/RAND_MAX)*m_rotationSpeedVariation) ) * CONV; - p.v1.autoRotate = p.v2.autoRotate = p.v3.autoRotate = p.v4.autoRotate = m_autoRotation?1.0:0.0; - - vertexCopy(p.v1, d->pv); - vertexCopy(p.v2, d->pv); - vertexCopy(p.v3, d->pv); - vertexCopy(p.v4, d->pv); -} - -QT_END_NAMESPACE |