1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
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
122 \brief The QSGFlatColorMaterial class provides a convenient way of rendering
123 solid colored geometry in the scene graph.
127 The flat color material will fill every pixel in a geometry using
128 a solid color. The color can contain transparency.
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.
135 The flat color material respects both current opacity and current matrix
136 when updating it's rendering state.
141 Constructs a new flat color material.
143 The default color is white.
146 QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
153 \fn QColor QSGFlatColorMaterial::color() const
155 Returns this flat color material's color.
157 The default color is white.
163 Sets this flat color material's color to \a color.
166 void QSGFlatColorMaterial::setColor(const QColor &color)
169 setFlag(Blending, m_color.alpha() != 0xff);
178 QSGMaterialType *QSGFlatColorMaterial::type() const
180 return &FlatColorMaterialShader::type;
189 QSGMaterialShader *QSGFlatColorMaterial::createShader() const
191 return new FlatColorMaterialShader;
195 int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
197 const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
198 return m_color.rgba() - flat->color().rgba();