Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / util / qsgflatcolormaterial.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgflatcolormaterial.h"
43
44 #include <qopenglshaderprogram.h>
45
46 QT_BEGIN_NAMESPACE
47
48 class FlatColorMaterialShader : public QSGMaterialShader
49 {
50 public:
51     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
52     virtual char const *const *attributeNames() const;
53
54     static QSGMaterialType type;
55
56 private:
57     virtual void initialize();
58     virtual const char *vertexShader() const;
59     virtual const char *fragmentShader() const;
60
61     int m_matrix_id;
62     int m_color_id;
63 };
64
65 QSGMaterialType FlatColorMaterialShader::type;
66
67 void FlatColorMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
68 {
69     Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
70
71     QSGFlatColorMaterial *oldMaterial = static_cast<QSGFlatColorMaterial *>(oldEffect);
72     QSGFlatColorMaterial *newMaterial = static_cast<QSGFlatColorMaterial *>(newEffect);
73
74     const QColor &c = newMaterial->color();
75
76     if (oldMaterial == 0 || c != oldMaterial->color() || state.isOpacityDirty()) {
77         float opacity = state.opacity();
78         QVector4D v(c.redF() * c.alphaF() * opacity,
79                     c.greenF() * c.alphaF() * opacity,
80                     c.blueF() * c.alphaF() * opacity,
81                     c.alphaF() * opacity);
82         program()->setUniformValue(m_color_id, v);
83     }
84
85     if (state.isMatrixDirty())
86         program()->setUniformValue(m_matrix_id, state.combinedMatrix());
87 }
88
89 char const *const *FlatColorMaterialShader::attributeNames() const
90 {
91     static char const *const attr[] = { "vCoord", 0 };
92     return attr;
93 }
94
95 void FlatColorMaterialShader::initialize()
96 {
97     m_matrix_id = program()->uniformLocation("matrix");
98     m_color_id = program()->uniformLocation("color");
99 }
100
101 const char *FlatColorMaterialShader::vertexShader() const {
102     return
103         "attribute highp vec4 vCoord;                   \n"
104         "uniform highp mat4 matrix;                     \n"
105         "void main() {                                  \n"
106         "    gl_Position = matrix * vCoord;             \n"
107         "}";
108 }
109
110 const char *FlatColorMaterialShader::fragmentShader() const {
111     return
112         "uniform lowp vec4 color;                       \n"
113         "void main() {                                  \n"
114         "    gl_FragColor = color;                      \n"
115         "}";
116 }
117
118
119
120 /*!
121     \class QSGFlatColorMaterial
122     \brief The QSGFlatColorMaterial class provides a convenient way of rendering
123     solid colored geometry in the scene graph.
124
125     \inmodule QtQuick
126
127     The flat color material will fill every pixel in a geometry using
128     a solid color. The color can contain transparency.
129
130     The geometry to be rendered with a flat color material requires
131     vertices in attribute location 0 in the QSGGeometry object to render
132     correctly. The QSGGeometry::defaultAttributes_Point2D() returns an attribute
133     set compatible with this material.
134
135     The flat color material respects both current opacity and current matrix
136     when updating it's rendering state.
137  */
138
139
140 /*!
141     Constructs a new flat color material.
142
143     The default color is white.
144  */
145
146 QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
147 {
148 }
149
150
151
152 /*!
153     \fn QColor QSGFlatColorMaterial::color() const
154
155     Returns this flat color material's color.
156
157     The default color is white.
158  */
159
160
161
162 /*!
163     Sets this flat color material's color to \a color.
164  */
165
166 void QSGFlatColorMaterial::setColor(const QColor &color)
167 {
168     m_color = color;
169     setFlag(Blending, m_color.alpha() != 0xff);
170 }
171
172
173
174 /*!
175     \internal
176  */
177
178 QSGMaterialType *QSGFlatColorMaterial::type() const
179 {
180     return &FlatColorMaterialShader::type;
181 }
182
183
184
185 /*!
186     \internal
187  */
188
189 QSGMaterialShader *QSGFlatColorMaterial::createShader() const
190 {
191     return new FlatColorMaterialShader;
192 }
193
194
195 int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
196 {
197     const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
198     return m_color.rgba() - flat->color().rgba();
199
200 }
201
202 QT_END_NAMESPACE