Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / util / qsgflatcolormaterial.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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
123     \brief The QSGFlatColorMaterial class provides a convenient way of rendering
124     solid colored geometry in the scene graph.
125
126     \inmodule QtQuick
127
128     The flat color material will fill every pixel in a geometry using
129     a solid color. The color can contain transparency.
130
131     The geometry to be rendered with a flat color material requires
132     vertices in attribute location 0 in the QSGGeometry object to render
133     correctly. The QSGGeometry::defaultAttributes_Point2D() returns an attribute
134     set compatible with this material.
135
136     The flat color material respects both current opacity and current matrix
137     when updating its rendering state.
138  */
139
140
141 /*!
142     Constructs a new flat color material.
143
144     The default color is white.
145  */
146
147 QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
148 {
149 }
150
151
152
153 /*!
154     \fn const QColor &QSGFlatColorMaterial::color() const
155
156     Returns this flat color material's color.
157
158     The default color is white.
159  */
160
161
162
163 /*!
164     Sets this flat color material's color to \a color.
165  */
166
167 void QSGFlatColorMaterial::setColor(const QColor &color)
168 {
169     m_color = color;
170     setFlag(Blending, m_color.alpha() != 0xff);
171 }
172
173
174
175 /*!
176     \internal
177  */
178
179 QSGMaterialType *QSGFlatColorMaterial::type() const
180 {
181     return &FlatColorMaterialShader::type;
182 }
183
184
185
186 /*!
187     \internal
188  */
189
190 QSGMaterialShader *QSGFlatColorMaterial::createShader() const
191 {
192     return new FlatColorMaterialShader;
193 }
194
195
196 /*!
197     \internal
198  */
199
200 int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
201 {
202     const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
203     return m_color.rgba() - flat->color().rgba();
204
205 }
206
207 QT_END_NAMESPACE