Allow background property to be set as a URL string, get will still return a map
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / quadratic-bezier.h
1 #ifndef __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
2 #define __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29 /**
30  * @brief Creates a new QuadraticBezier shader effect
31  *
32  * QuadraticBezier is a custom shader to render quadratic bezier curves and bounded regions.
33  *
34  * Implementation based on the paper "Resolution Independent Curve Rendering using Programmable Graphics Hardware"
35  * by Charles Loop and Jim Blinn.
36  *
37  * The coordinates of the control points is passed to the shader via uniforms.
38  * aNormal attribute is used to pass the coefficients of each control point (xy coordinates) as well as the vertex ID (z coordinate).
39  * A quadratic curve should have as normal for the first control point (0.0,0.0), (0.5,0.0) for the second and (1.0,1.0) for the third.
40  * Triangles that do not contain curves should have coordinates (0.0,1.0) for each control point in order to be filled properly.
41  *
42  * Animatable/Constrainable uniforms:
43  *  "uPoint"      - Position coordinates for the points in the curve
44  *  "uColor"      - The color of the curve or bounded region
45  *  "uLineWidth"  - The width of the path. Only for not filled curves
46  *
47  * @param[in] pointCount The maximum number of vertices
48  * @param[in] filled Specify whether the the bounded region should be filled or not
49  * @return A handle to a newly allocated ShaderEffect
50  */
51 inline ShaderEffect CreateQuadraticBezier(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 shaderEffect = Dali::ShaderEffect::NewWithPrefix(
112       vertexShaderPrefix.str(),vertexShader,
113       "#extension GL_OES_standard_derivatives:enable\n", fragmentShader );
114
115   //Set default uniform values
116   shaderEffect.SetUniform( "uColor", Vector4(1.0f,1.0f,1.0f,1.0f) );
117   if( !filled )
118   {
119     //Set default line widht to 1 pixel
120     shaderEffect.SetUniform( "uLineWidth", 1.0f );
121   }
122
123   return shaderEffect;
124 }
125
126 } // namespace Toolkit
127
128 } // namespace Dali
129
130 #endif // __DALI_TOOLKIT_IMAGE_REGION_EFFECT_H__