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 "qsgvertexcolormaterial.h"
44 #include <qopenglshaderprogram.h>
48 class QSGVertexColorMaterialShader : 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 QSGVertexColorMaterialShader::type;
67 void QSGVertexColorMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *)
69 if (!(newEffect->flags() & QSGMaterial::Blending) || state.isOpacityDirty())
70 program()->setUniformValue(m_opacity_id, state.opacity());
72 if (state.isMatrixDirty())
73 program()->setUniformValue(m_matrix_id, state.combinedMatrix());
76 char const *const *QSGVertexColorMaterialShader::attributeNames() const
78 static const char *const attr[] = { "vertexCoord", "vertexColor", 0 };
82 void QSGVertexColorMaterialShader::initialize()
84 m_matrix_id = program()->uniformLocation("matrix");
85 m_opacity_id = program()->uniformLocation("opacity");
88 const char *QSGVertexColorMaterialShader::vertexShader() const {
90 "attribute highp vec4 vertexCoord; \n"
91 "attribute highp vec4 vertexColor; \n"
92 "uniform highp mat4 matrix; \n"
93 "uniform highp float opacity; \n"
94 "varying lowp vec4 color; \n"
96 " gl_Position = matrix * vertexCoord; \n"
97 " color = vertexColor * opacity; \n"
101 const char *QSGVertexColorMaterialShader::fragmentShader() const {
103 "varying lowp vec4 color; \n"
105 " gl_FragColor = color; \n"
112 \class QSGVertexColorMaterial
113 \brief The QSGVertexColorMaterial class provides a convenient way of rendering per-vertex
114 colored geometry in the scene graph.
118 The vertex color material will give each vertex in a geometry a color. Pixels between
119 vertices will be linearly interpolated. The colors can contain transparency.
121 The geometry to be rendered with vertex color must have the following layout. Attribute
122 position 0 must contain vertices. Attribute position 1 must contain colors, a tuple of
123 4 values with RGBA layout. Both floats in the range of 0 to 1 and unsigned bytes in
124 the range 0 to 255 are valid for the color values. The
125 QSGGeometry::defaultAttributes_ColoredPoint2D() constructs an attribute set
126 compatible with this material.
128 The vertex color material respects both current opacity and current matrix when
129 updating it's rendering state.
133 QSGVertexColorMaterial::QSGVertexColorMaterial()
135 setFlag(Blending, true);
140 int QSGVertexColorMaterial::compare() const
142 As the vertex color material has all its state in the vertex attributes,
143 all materials will be equal.
148 int QSGVertexColorMaterial::compare(const QSGMaterial * /* other */) const
157 QSGMaterialType *QSGVertexColorMaterial::type() const
159 return &QSGVertexColorMaterialShader::type;
168 QSGMaterialShader *QSGVertexColorMaterial::createShader() const
170 return new QSGVertexColorMaterialShader;