ef1fecae90f459621718d4c6f6575590b27b2cc6
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / util / qsgvertexcolormaterial.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgvertexcolormaterial.h"
43
44 #include <qopenglshaderprogram.h>
45
46 QT_BEGIN_NAMESPACE
47
48 class QSGVertexColorMaterialShader : 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_opacity_id;
63 };
64
65 QSGMaterialType QSGVertexColorMaterialShader::type;
66
67 void QSGVertexColorMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *)
68 {
69     if (!(newEffect->flags() & QSGMaterial::Blending) || state.isOpacityDirty())
70         program()->setUniformValue(m_opacity_id, state.opacity());
71
72     if (state.isMatrixDirty())
73         program()->setUniformValue(m_matrix_id, state.combinedMatrix());
74 }
75
76 char const *const *QSGVertexColorMaterialShader::attributeNames() const
77 {
78     static const char *const attr[] = { "vertexCoord", "vertexColor", 0 };
79     return attr;
80 }
81
82 void QSGVertexColorMaterialShader::initialize()
83 {
84     m_matrix_id = program()->uniformLocation("matrix");
85     m_opacity_id = program()->uniformLocation("opacity");
86 }
87
88 const char *QSGVertexColorMaterialShader::vertexShader() const {
89     return
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"
95         "void main() {                                  \n"
96         "    gl_Position = matrix * vertexCoord;        \n"
97         "    color = vertexColor * opacity;             \n"
98         "}";
99 }
100
101 const char *QSGVertexColorMaterialShader::fragmentShader() const {
102     return
103         "varying lowp vec4 color;                       \n"
104         "void main() {                                  \n"
105         "    gl_FragColor = color;                      \n"
106         "}";
107 }
108
109
110
111 /*!
112     \class QSGVertexColorMaterial
113     \brief The QSGVertexColorMaterial class provides a convenient way of rendering per-vertex
114     colored geometry in the scene graph.
115
116     \inmodule QtQuick
117
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.
120
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.
127
128     The vertex color material respects both current opacity and current matrix when
129     updating it's rendering state.
130  */
131
132
133 QSGVertexColorMaterial::QSGVertexColorMaterial()
134 {
135     setFlag(Blending, true);
136 }
137
138
139 /*!
140     int QSGVertexColorMaterial::compare() const
141
142     As the vertex color material has all its state in the vertex attributes,
143     all materials will be equal.
144
145     \internal
146  */
147
148 int QSGVertexColorMaterial::compare(const QSGMaterial * /* other */) const
149 {
150     return 0;
151 }
152
153 /*!
154     \internal
155  */
156
157 QSGMaterialType *QSGVertexColorMaterial::type() const
158 {
159     return &QSGVertexColorMaterialShader::type;
160 }
161
162
163
164 /*!
165     \internal
166  */
167
168 QSGMaterialShader *QSGVertexColorMaterial::createShader() const
169 {
170     return new QSGVertexColorMaterialShader;
171 }
172
173 QT_END_NAMESPACE