[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qgraphicsshadereffect_p.h"
|
---|
[846] | 43 | #if !defined(QT_OPENGL_ES_1)
|
---|
[556] | 44 | #include "qglshaderprogram.h"
|
---|
| 45 | #include "gl2paintengineex/qglcustomshaderstage_p.h"
|
---|
| 46 | #define QGL_HAVE_CUSTOM_SHADERS 1
|
---|
| 47 | #endif
|
---|
| 48 | #include <QtGui/qpainter.h>
|
---|
| 49 | #include <QtGui/qgraphicsitem.h>
|
---|
| 50 | #include <QtGui/private/qgraphicseffect_p.h>
|
---|
| 51 |
|
---|
| 52 | QT_BEGIN_NAMESPACE
|
---|
| 53 |
|
---|
| 54 | /*#
|
---|
| 55 | \class QGraphicsShaderEffect
|
---|
| 56 | \brief The QGraphicsShaderEffect class is the base class for creating
|
---|
| 57 | custom GLSL shader effects in a QGraphicsScene.
|
---|
| 58 | \since 4.6
|
---|
| 59 | \ingroup multimedia
|
---|
| 60 | \ingroup graphicsview-api
|
---|
| 61 |
|
---|
| 62 | The specific effect is defined by a fragment of GLSL source code
|
---|
| 63 | supplied to setPixelShaderFragment(). This source code must define a
|
---|
| 64 | function with the signature
|
---|
| 65 | \c{lowp vec4 customShader(lowp sampler2D imageTexture, highp vec2 textureCoords)}
|
---|
| 66 | that returns the source pixel value
|
---|
| 67 | to use in the paint engine's shader program. The shader fragment
|
---|
| 68 | is linked with the regular shader code used by the GL2 paint engine
|
---|
| 69 | to construct a complete QGLShaderProgram.
|
---|
| 70 |
|
---|
| 71 | The following example shader converts the incoming pixmap to
|
---|
| 72 | grayscale and then applies a colorize operation using the
|
---|
| 73 | \c effectColor value:
|
---|
| 74 |
|
---|
| 75 | \code
|
---|
| 76 | static char const colorizeShaderCode[] =
|
---|
| 77 | "uniform lowp vec4 effectColor;\n"
|
---|
| 78 | "lowp vec4 customShader(lowp sampler2D imageTexture, highp vec2 textureCoords) {\n"
|
---|
| 79 | " vec4 src = texture2D(imageTexture, textureCoords);\n"
|
---|
| 80 | " float gray = dot(src.rgb, vec3(0.212671, 0.715160, 0.072169));\n"
|
---|
| 81 | " vec4 colorize = 1.0-((1.0-gray)*(1.0-effectColor));\n"
|
---|
| 82 | " return vec4(colorize.rgb, src.a);\n"
|
---|
| 83 | "}";
|
---|
| 84 | \endcode
|
---|
| 85 |
|
---|
| 86 | To use this shader code, it is necessary to define a subclass
|
---|
| 87 | of QGraphicsShaderEffect as follows:
|
---|
| 88 |
|
---|
| 89 | \code
|
---|
| 90 | class ColorizeEffect : public QGraphicsShaderEffect
|
---|
| 91 | {
|
---|
| 92 | Q_OBJECT
|
---|
| 93 | public:
|
---|
| 94 | ColorizeEffect(QObject *parent = 0)
|
---|
| 95 | : QGraphicsShaderEffect(parent), color(Qt::black)
|
---|
| 96 | {
|
---|
| 97 | setPixelShaderFragment(colorizeShaderCode);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | QColor effectColor() const { return color; }
|
---|
| 101 | void setEffectColor(const QColor& c)
|
---|
| 102 | {
|
---|
| 103 | color = c;
|
---|
| 104 | setUniformsDirty();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | protected:
|
---|
| 108 | void setUniforms(QGLShaderProgram *program)
|
---|
| 109 | {
|
---|
| 110 | program->setUniformValue("effectColor", color);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private:
|
---|
| 114 | QColor color;
|
---|
| 115 | };
|
---|
| 116 | \endcode
|
---|
| 117 |
|
---|
| 118 | The setUniforms() function is called when the effect is about
|
---|
| 119 | to be used for drawing to give the subclass the opportunity to
|
---|
| 120 | set effect-specific uniform variables.
|
---|
| 121 |
|
---|
| 122 | QGraphicsShaderEffect is only supported when the GL2 paint engine
|
---|
| 123 | is in use. When any other paint engine is in use (GL1, raster, etc),
|
---|
| 124 | the drawItem() method will draw its item argument directly with
|
---|
| 125 | no effect applied.
|
---|
| 126 |
|
---|
| 127 | \sa QGraphicsEffect
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | static const char qglslDefaultImageFragmentShader[] = "\
|
---|
| 131 | lowp vec4 customShader(lowp sampler2D imageTexture, highp vec2 textureCoords) { \
|
---|
| 132 | return texture2D(imageTexture, textureCoords); \
|
---|
| 133 | }\n";
|
---|
| 134 |
|
---|
| 135 | #ifdef QGL_HAVE_CUSTOM_SHADERS
|
---|
| 136 |
|
---|
| 137 | class QGLCustomShaderEffectStage : public QGLCustomShaderStage
|
---|
| 138 | {
|
---|
| 139 | public:
|
---|
| 140 | QGLCustomShaderEffectStage
|
---|
| 141 | (QGraphicsShaderEffect *e, const QByteArray& source)
|
---|
| 142 | : QGLCustomShaderStage(),
|
---|
| 143 | effect(e)
|
---|
| 144 | {
|
---|
| 145 | setSource(source);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void setUniforms(QGLShaderProgram *program);
|
---|
| 149 |
|
---|
| 150 | QGraphicsShaderEffect *effect;
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | void QGLCustomShaderEffectStage::setUniforms(QGLShaderProgram *program)
|
---|
| 154 | {
|
---|
| 155 | effect->setUniforms(program);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | #endif
|
---|
| 159 |
|
---|
| 160 | class QGraphicsShaderEffectPrivate : public QGraphicsEffectPrivate
|
---|
| 161 | {
|
---|
| 162 | Q_DECLARE_PUBLIC(QGraphicsShaderEffect)
|
---|
| 163 | public:
|
---|
| 164 | QGraphicsShaderEffectPrivate()
|
---|
| 165 | : pixelShaderFragment(qglslDefaultImageFragmentShader)
|
---|
| 166 | #ifdef QGL_HAVE_CUSTOM_SHADERS
|
---|
| 167 | , customShaderStage(0)
|
---|
| 168 | #endif
|
---|
| 169 | {
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | QByteArray pixelShaderFragment;
|
---|
| 173 | #ifdef QGL_HAVE_CUSTOM_SHADERS
|
---|
| 174 | QGLCustomShaderEffectStage *customShaderStage;
|
---|
| 175 | #endif
|
---|
| 176 | };
|
---|
| 177 |
|
---|
| 178 | /*#
|
---|
| 179 | Constructs a shader effect and attaches it to \a parent.
|
---|
| 180 | */
|
---|
| 181 | QGraphicsShaderEffect::QGraphicsShaderEffect(QObject *parent)
|
---|
| 182 | : QGraphicsEffect(*new QGraphicsShaderEffectPrivate(), parent)
|
---|
| 183 | {
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*#
|
---|
| 187 | Destroys this shader effect.
|
---|
| 188 | */
|
---|
| 189 | QGraphicsShaderEffect::~QGraphicsShaderEffect()
|
---|
| 190 | {
|
---|
| 191 | #ifdef QGL_HAVE_CUSTOM_SHADERS
|
---|
| 192 | Q_D(QGraphicsShaderEffect);
|
---|
| 193 | delete d->customShaderStage;
|
---|
| |
---|