1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qsgvertexcolormaterial_p.h"
44 #include <qglshaderprogram.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 provides a convenient way of rendering per-vertex
114 colored geometry in the scene graph.
116 The vertex color material will give each vertex in a geometry a color. Pixels between
117 vertices will be linearly interpolated. The colors can contain transparency.
119 The geometry to be rendered with vertex color must have the following layout. Attribute
120 position 0 must contain vertices. Attribute position 1 must contain colors, a tuple of
121 4 values with RGBA layout. Both floats in the range of 0 to 1 and unsigned bytes in
122 the range 0 to 255 are valid for the color values. The
123 QSGGeometry::defaultAttributes_ColoredPoint2D() constructs an attribute set
124 compatible with this material.
126 The vertex color material respets both current opacity and current matrix when
127 updating it's rendering state.
131 QSGVertexColorMaterial::QSGVertexColorMaterial()
133 setFlag(Blending, true);
139 Sets if the renderer should treat colors as opaque.
141 Setting this flag can in some cases improve performance.
144 void QSGVertexColorMaterial::setColorsAreOpaque(bool opaqueHint)
146 setFlag(Blending, !opaqueHint);
155 QSGMaterialType *QSGVertexColorMaterial::type() const
157 return &QSGVertexColorMaterialShader::type;
166 QSGMaterialShader *QSGVertexColorMaterial::createShader() const
168 return new QSGVertexColorMaterialShader;