1 #ifndef __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
2 #define __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/shader-effects/shader-effect.h>
30 * @brief Creates a new QuadraticBezier shader effect
32 * QuadraticBezier is a custom shader to render quadratic bezier curves and bounded regions.
34 * Implementation based on the paper "Resolution Independent Curve Rendering using Programmable Graphics Hardware"
35 * by Charles Loop and Jim Blinn.
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.
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
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
51 inline ShaderEffect CreateQuadraticBezier(unsigned int pointCount, bool filled)
53 std::string vertexShader = DALI_COMPOSE_SHADER
55 uniform mediump vec3 uPoint[MAX_POINT_COUNT];\n
56 varying highp vec2 vCoefficient;
59 int vertexId = int(aNormal.z);\n
60 gl_Position = uMvpMatrix * vec4(uPoint[vertexId], 1.0);\n
61 vCoefficient = aNormal.xy;\n
65 std::string fragmentShader;
69 fragmentShader = DALI_COMPOSE_SHADER
71 varying highp vec2 vCoefficient;\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
79 highp float distance = float(C / sqrt(Cdx*Cdx + Cdy*Cdy));\n
81 gl_FragColor = uColor;\n
82 highp float alpha = 0.5 - distance;\n
86 gl_FragColor.w = alpha;\n
92 fragmentShader = DALI_COMPOSE_SHADER
94 varying highp vec2 vCoefficient;\n
95 uniform lowp float uLineWidth;\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
108 std::ostringstream vertexShaderPrefix;
109 vertexShaderPrefix << "#define MAX_POINT_COUNT "<< pointCount << "\n";
111 Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::NewWithPrefix(
112 vertexShaderPrefix.str(),vertexShader,
113 "#extension GL_OES_standard_derivatives:enable\n", fragmentShader );
115 //Set default uniform values
116 shaderEffect.SetUniform( "uColor", Vector4(1.0f,1.0f,1.0f,1.0f) );
119 //Set default line widht to 1 pixel
120 shaderEffect.SetUniform( "uLineWidth", 1.0f );
126 } // namespace Toolkit
130 #endif // __DALI_TOOLKIT_IMAGE_REGION_EFFECT_H__