033de1f73ece0c52508592a73167b72b26082ddf
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / util / qsgvertexcolormaterial.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgvertexcolormaterial_p.h"
43
44 #include <qglshaderprogram.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 provides a convenient way of rendering per-vertex
114     colored geometry in the scene graph.
115
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.
118
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.
125
126     The vertex color material respets both current opacity and current matrix when
127     updating it's rendering state.
128  */
129
130
131 QSGVertexColorMaterial::QSGVertexColorMaterial()
132 {
133     setFlag(Blending, true);
134 }
135
136
137
138 /*!
139     Sets if the renderer should treat colors as opaque.
140
141     Setting this flag can in some cases improve performance.
142  */
143
144 void QSGVertexColorMaterial::setColorsAreOpaque(bool opaqueHint)
145 {
146     setFlag(Blending, !opaqueHint);
147 }
148
149
150
151 /*!
152     \internal
153  */
154
155 QSGMaterialType *QSGVertexColorMaterial::type() const
156 {
157     return &QSGVertexColorMaterialShader::type;
158 }
159
160
161
162 /*!
163     \internal
164  */
165
166 QSGMaterialShader *QSGVertexColorMaterial::createShader() const
167 {
168     return new QSGVertexColorMaterialShader;
169 }
170
171 QT_END_NAMESPACE