source: trunk/src/opengl/qgraphicsshadereffect.cpp@ 636

Last change on this file since 636 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 9.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
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"
43#if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
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
52QT_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();