summaryrefslogtreecommitdiffstats
path: root/src/quick3d/qdeclarativeeffect.cpp
blob: 573fe4720f2f637be603196c15115d0f2144872b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/****************************************************************************
**
** 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 QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativeeffect.h"

#include "qglpainter.h"
#include "qglmaterial.h"

#include <QNetworkRequest>
#include <QNetworkReply>

#include <QtDeclarative/qdeclarativeengine.h>

#include <qdeclarativeinfo.h>
#include "qglscenenode.h"

/*!
    \qmlclass Effect QDeclarativeEffect
    \brief The Effect item defines simple effects within the QML/3D environment.  Examples
    of such effects include textures, simple material and lighting effects, and so on.
    \since 4.8
    \ingroup qt3d::qml3d

    \section1 Defining an Effect

    Effects can be defined within QML using the normal syntax for creating any QML/3d object.  To create a
    default effect with no extra information, for example, we can simply using the following:

    \code
    Effect {}
    \endcode

    More complex effects use the usual QML syntax for accessing and updating properties.  In order to specify
    a texture, for example, the following could be used:

    \code
    Effect {
        id: myTextureEffect
        texture: "texture.png"
    }
    \endcode
*/

QT_BEGIN_NAMESPACE

class QDeclarativeEffectPrivate
{
public:
    QDeclarativeEffectPrivate()
        : color(255, 255, 255, 255),
          useLighting(true),
          decal(false),
          blending(false),
          palette(0),
          materialIndex(-1),
          progress(0.0)
    {
    }

    ~QDeclarativeEffectPrivate()
    {
        delete palette;
    }

    QColor color;
    bool useLighting;
    bool decal;
    bool blending;
    QGLMaterialCollection *palette;
    int materialIndex;
    qreal progress;
    inline void ensureMaterial() {
        if (!palette)
        {
            palette = new QGLMaterialCollection();
        }
        if (materialIndex == -1)
        {
            materialIndex = palette->addMaterial(new QGLMaterial(palette));
            palette->material(materialIndex)->setColor(color);
        }
        // TODO: decal & blending
    }
};

/*!
    \internal
    Constructs the Effect object with \a parent as its parent.
*/
QDeclarativeEffect::QDeclarativeEffect(QObject *parent)
    : QObject(parent)
{
    d = new QDeclarativeEffectPrivate;
}

/*!
    \internal
    Destroy the \l Effect object and delete any unneeded data.
*/
QDeclarativeEffect::~QDeclarativeEffect()
{
    delete d;
}

/*!
    \qmlproperty color Effect::color

    The color of the object for simple non-texture effects.
    The default value for this property is white.
*/

QColor QDeclarativeEffect::color() const
{
    return d->color;
}

void QDeclarativeEffect::setColor(const QColor& value)
{
    d->color = value;
    d->ensureMaterial();
    material()->setColor(value);
    emit effectChanged();
}

/*!
    \qmlproperty bool Effect::useLighting

    The lighting control parameter; true if this effect should
    use lighting or false if this effect should use flat colors and
    textures.

    The default value for this property is true.
*/
bool QDeclarativeEffect::useLighting() const
{
    return d->useLighting;
}

void QDeclarativeEffect::setUseLighting(bool value)
{
    d->useLighting = value;
    emit effectChanged();
}

/*!
    \qmlproperty bool Effect::decal

    This property should be set to true if the \l texture should be
    combined with \l color to decal the texture onto the object.
    This property should be set to false to use the texture
    directly, combined with the \l material parameters.

    The default value for this property is false.
*/
bool QDeclarativeEffect::decal() const
{
    return d->decal;
}

void QDeclarativeEffect::setDecal(bool value)
{
    if (d->decal != value) {
        d->decal = value;
        emit effectChanged();
    }
}

/*!
    \qmlproperty bool Effect::blending

    This property should be set to true if alpha blending should be
    enabled when this effect is active, or false otherwise.  The
    default is false.

    This property overrides the viewport-specific blending setting
    that is specified by Viewport::blending.
*/
bool QDeclarativeEffect::blending() const
{
    return d->blending;
}

void QDeclarativeEffect::setBlending(bool value)
{
    if (d->blending != value) {
        d->blending = value;
        emit effectChanged();
    }
}

/*!
    \qmlproperty url Effect::texture

    Texture effects are defined by this property.  A texture is
    provided by means of a QUrl which is then used for texturing.

    Textures can also be defined directly as images using the textureImage
    property.

    If the value is non-empty, and the texture could not be loaded from
    the QUrl for any reason, then the property will not be changed.

    \sa textureImage
*/
QUrl QDeclarativeEffect::texture() const
{
    if (!material())
        return QUrl();
    return material()->textureUrl();
}

void QDeclarativeEffect::setTexture(const QUrl& value)
{
    if (material() && material()->textureUrl() == value)
        return;

    if (d->progress != 0.0)
    {
        d->progress = 0.0;
        emit progressChanged(d->progress);
    }

    if (value.isEmpty())
    {
        if (material())
        {
            material()->setTextureUrl(value);
            emit effectChanged();
        }
    }
    else
    {
        d->ensureMaterial();
        // Warning: This will trigger the deletion of the old texure.
        material()->setTextureUrl(value);
        emit effectChanged();
    }
}

/*!
    \qmlproperty image Effect::textureImage

    This property directly defines a texture using an existing QImage.

    Textures can also be defined based on a URL using the texture property.

    \sa texture
*/
QImage QDeclarativeEffect::textureImage() const
{
    return (material() && material()->texture()) ?
                material()->texture()->image() : QImage();
}

/*!
  \internal
  Sets this effect to use \value as the image for it's texture.
  
  OpenGL defaults have been overridden to clamp the texture both horizontally and vertically as per QGL::Clamp.
*/
void QDeclarativeEffect::setTextureImage(const QImage& value)
{
    QGLTexture2D * tex;
    d->ensureMaterial();
    if (!material()->texture())
    {
        // Should this texture be parented?
        tex = new QGLTexture2D();
        material()->setTexture(tex);
    } else
    {
        tex = material()->texture();
    }

    // Equality test of images can be very expensive, so always assign the
    // value and emit effect changed
    tex->setImage(value);
    
    // prevents artifacts due to texture smoothing wrapping around edges of texture 
    tex->setHorizontalWrap(QGL::Clamp);
    tex->setVerticalWrap(QGL::Clamp);
    
    emit effectChanged();
}

/*!
    \qmlproperty Material Effect::material

    Defines the material to apply to items that use this effect.
*/
QGLMaterial *QDeclarativeEffect::material() const
{
    if (!d->palette)
        return 0;
    return d->palette->material(d->materialIndex);
}

/*!
  \internal
  Sets the material for use with this effect.  Creates a QGLMaterialCollection
  to contain it if necessary.
*/
void QDeclarativeEffect::setMaterial(QGLMaterial *value)
{
    d->ensureMaterial();
    int newIndex = -1;
    if (value)
        newIndex = d->palette->addMaterial(value);
    if (newIndex != d->materialIndex)
    {
        d->materialIndex = newIndex;
        emit effectChanged();
        // TODO: deleting old materials
    }
}

/*!
    \qmlproperty real Effect::progress

    Tracks how much of a remote resource has been downloaded, where 0.0
    is no progress, and 1.0 is completion.
*/

/*!
    \internal
    Enable the effect on for a given \a painter.
*/
void QDeclarativeEffect::enableEffect(QGLPainter *painter)
{
    painter->setColor(d->color);
    if (d->materialIndex != -1 && d->palette->material(d->materialIndex))
    {
        painter->setFaceMaterial(QGL::FrontFaces, material()->front());
        painter->setFaceMaterial(QGL::BackFaces, material()->back());
    } else
        painter->setFaceColor(QGL::AllFaces, d->color);

    QGLTexture2D *tex = 0;
    if (material())
        tex = material()->texture();
    if (d->useLighting) {
        if (tex && !tex->isNull()) {
            if (d->decal)
                painter->setStandardEffect(QGL::LitDecalTexture2D);
            else
                painter->setStandardEffect(QGL::LitModulateTexture2D);
            tex->bind();
        } else {
            painter->setStandardEffect(QGL::LitMaterial);
        }
    } else {
        if (tex && !tex->isNull()) {
            if (d->decal)
                painter->setStandardEffect(QGL::FlatDecalTexture2D);
            else
                painter->setStandardEffect(QGL::FlatReplaceTexture2D);
//            painter->glActiveTexture(GL_TEXTURE0);
            tex->bind();
        } else {
            painter->setStandardEffect(QGL::FlatColor);
        }
    }
}

/*!
    \internal
    Disable the effect for a given \a painter.
*/
void QDeclarativeEffect::disableEffect(QGLPainter *painter)
{
    painter->setStandardEffect(QGL::FlatColor);
    painter->setColor(Qt::white);
    painter->glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

/*!
  \internal
  Set a scenenode's material and effect properties to enact this effect.

  The desired functionality should mirror the Mesh::draw(QGLPainter*)
  functionality as closely as possible.

  Subclasses can reimplement this function to set desired effects (e.g. user
  effects);
*/
void QDeclarativeEffect::applyTo(QGLSceneNode *node)
{
    d->ensureMaterial();

    node->setPalette(d->palette);
    node->setMaterialIndex(d->materialIndex);

    Q_ASSERT(node->material());

    QGLTexture2D *tex = material()->texture();
    if (tex && !tex->isNull())
        node->material()->setTexture(tex);

    // Determine which standard effect to use
    if (d->useLighting) {
        if (tex && !tex->isNull()) {
            if (d->decal)
                node->setEffect(QGL::LitDecalTexture2D);
            else
                node->setEffect(QGL::LitModulateTexture2D);
        } else {
            node->setEffect(QGL::LitMaterial);
        }
    } else {
        if (tex && !tex->isNull()) {
            if (d->decal)
                node->setEffect(QGL::FlatDecalTexture2D);
            else
                node->setEffect(QGL::FlatReplaceTexture2D);
        } else {
            node->setEffect(QGL::FlatColor);
        }
    }
}

QGLTexture2D *QDeclarativeEffect::texture2D()
{
    if (!material())
        return 0;
    return material()->texture();
}

/*!
  Returns the progress of remote resource loading.
  */
qreal QDeclarativeEffect::progress()
{
    return d->progress;
}

QT_END_NAMESPACE