Merge "TET cases for Text." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / quadratic-bezier.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 //CLASS HEADER
19 #include <dali-toolkit/devel-api/shader-effects/quadratic-bezier.h>
20
21 //EXTERNAL HEADERS
22 #include <sstream>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace
31 {
32 const char* POINT_PROPERTY_NAME( "uPoint" );
33 const char* LINEWIDTH_PROPERTY_NAME( "uLineWidth" );
34 const char* COLOR_PROPERTY_NAME( "uColor" );
35 } // namespace
36
37 QuadraticBezier::QuadraticBezier()
38 {
39 }
40
41 //Call the Parent copy constructor to add reference to the implementation for this object
42 QuadraticBezier::QuadraticBezier(ShaderEffect handle)
43 :ShaderEffect(handle)
44 {
45 }
46
47 QuadraticBezier::~QuadraticBezier()
48 {
49 }
50
51 QuadraticBezier QuadraticBezier::New(unsigned int pointCount, bool filled )
52 {
53   std::string vertexShader = DALI_COMPOSE_SHADER
54   (
55       uniform mediump vec3 uPoint[MAX_POINT_COUNT];\n
56       varying highp vec2 vCoefficient;
57       void main()\n
58       {\n
59         int vertexId = int(aNormal.z);\n
60         gl_Position = uMvpMatrix * vec4(uPoint[vertexId], 1.0);\n
61         vCoefficient = aNormal.xy;\n
62       }\n
63   );
64
65   std::string fragmentShader;
66
67   if( filled )
68   {
69     fragmentShader = DALI_COMPOSE_SHADER
70     (
71         varying highp vec2 vCoefficient;\n
72
73         void main()\n
74         {\n
75           highp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y);\n
76           highp float Cdx = dFdx(C);\n
77           highp float Cdy = dFdy(C);\n
78
79           highp float distance = float(C / sqrt(Cdx*Cdx + Cdy*Cdy));\n
80
81           gl_FragColor = uColor;\n
82           highp float alpha = 0.5 - distance;\n
83           if( alpha < 0.0 )\n
84             discard;\n
85
86           gl_FragColor.w = alpha;\n
87         }\n
88     );
89   }
90   else
91   {
92     fragmentShader = DALI_COMPOSE_SHADER
93     (
94         varying highp vec2 vCoefficient;\n
95         uniform lowp float uLineWidth;\n
96
97         void main()\n
98         {\n
99           highp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y);\n
100           highp float Cdx = dFdx(C);\n
101           highp float Cdy = dFdy(C);\n
102           highp float distance = abs(float(C / sqrt(Cdx*Cdx + Cdy*Cdy)));\n
103           gl_FragColor = uColor*(uLineWidth-distance);\n
104         }\n
105     );
106   }
107
108   std::ostringstream vertexShaderPrefix;
109   vertexShaderPrefix << "#define MAX_POINT_COUNT "<< pointCount << "\n";
110
111   Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::NewWithPrefix( vertexShaderPrefix.str(),vertexShader,
112                                                                              "#extension GL_OES_standard_derivatives:enable\n", fragmentShader,
113                                                                              GEOMETRY_TYPE_UNTEXTURED_MESH );
114
115   Dali::Toolkit::QuadraticBezier handle( shaderEffectCustom );
116
117   //Set default uniform values
118   handle.SetUniform( COLOR_PROPERTY_NAME, Vector4(1.0f,1.0f,1.0f,1.0f) );
119   if( !filled )
120   {
121     //Set default line widht to 1 pixel
122     handle.SetUniform( LINEWIDTH_PROPERTY_NAME, 1.0f );
123   }
124
125   return handle;
126 }
127
128 void QuadraticBezier::SetPoint(unsigned int index, const Vector3& position)
129 {
130   SetUniform( GetPointPropertyName(index), position );
131 }
132
133 void QuadraticBezier::SetLineWidth( float width )
134 {
135   SetUniform( LINEWIDTH_PROPERTY_NAME, width );
136 }
137
138 void QuadraticBezier::SetColor( const Vector4& color )
139 {
140   SetUniform( COLOR_PROPERTY_NAME, color );
141 }
142
143 std::string QuadraticBezier::GetPointPropertyName(unsigned int index) const
144 {
145   std::ostringstream propertyName;
146   propertyName<<POINT_PROPERTY_NAME<<"["<<index<<"]";
147   return propertyName.str();
148 }
149
150 std::string QuadraticBezier::GetLineWidthPropertyName( ) const
151 {
152   return LINEWIDTH_PROPERTY_NAME;
153 }
154
155 std::string QuadraticBezier::GetColorPropertyName( ) const
156 {
157   return COLOR_PROPERTY_NAME;
158 }
159
160 } // namespace Toolkit
161
162 } // namespace Dali
163