1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qsgflatcolormaterial.h"
44 #include <qopenglshaderprogram.h>
48 class FlatColorMaterialShader : public QSGMaterialShader
51 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
52 virtual char const *const *attributeNames() const;
54 static QSGMaterialType type;
57 virtual void initialize();
58 virtual const char *vertexShader() const;
59 virtual const char *fragmentShader() const;
65 QSGMaterialType FlatColorMaterialShader::type;
67 void FlatColorMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
69 Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
71 QSGFlatColorMaterial *oldMaterial = static_cast<QSGFlatColorMaterial *>(oldEffect);
72 QSGFlatColorMaterial *newMaterial = static_cast<QSGFlatColorMaterial *>(newEffect);
74 const QColor &c = newMaterial->color();
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);
85 if (state.isMatrixDirty())
86 program()->setUniformValue(m_matrix_id, state.combinedMatrix());
89 char const *const *FlatColorMaterialShader::attributeNames() const
91 static char const *const attr[] = { "vCoord", 0 };
95 void FlatColorMaterialShader::initialize()
97 m_matrix_id = program()->uniformLocation("matrix");
98 m_color_id = program()->uniformLocation("color");
101 const char *FlatColorMaterialShader::vertexShader() const {
103 "attribute highp vec4 vCoord; \n"
104 "uniform highp mat4 matrix; \n"
106 " gl_Position = matrix * vCoord; \n"
110 const char *FlatColorMaterialShader::fragmentShader() const {
112 "uniform lowp vec4 color; \n"
114 " gl_FragColor = color; \n"
121 \class QSGFlatColorMaterial
123 \brief The QSGFlatColorMaterial class provides a convenient way of rendering
124 solid colored geometry in the scene graph.
128 The flat color material will fill every pixel in a geometry using
129 a solid color. The color can contain transparency.
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.
136 The flat color material respects both current opacity and current matrix
137 when updating its rendering state.
142 Constructs a new flat color material.
144 The default color is white.
147 QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
154 \fn const QColor &QSGFlatColorMaterial::color() const
156 Returns this flat color material's color.
158 The default color is white.
164 Sets this flat color material's color to \a color.
167 void QSGFlatColorMaterial::setColor(const QColor &color)
170 setFlag(Blending, m_color.alpha() != 0xff);
179 QSGMaterialType *QSGFlatColorMaterial::type() const
181 return &FlatColorMaterialShader::type;
190 QSGMaterialShader *QSGFlatColorMaterial::createShader() const
192 return new FlatColorMaterialShader;
200 int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
202 const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
203 return m_color.rgba() - flat->color().rgba();