2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/public-api/shader-effects/quadratic-bezier.h>
32 const char* POINT_PROPERTY_NAME( "uPoint" );
33 const char* LINEWIDTH_PROPERTY_NAME( "uLineWidth" );
34 const char* COLOR_PROPERTY_NAME( "uColor" );
37 QuadraticBezier::QuadraticBezier()
41 //Call the Parent copy constructor to add reference to the implementation for this object
42 QuadraticBezier::QuadraticBezier(ShaderEffect handle)
47 QuadraticBezier::~QuadraticBezier()
51 QuadraticBezier QuadraticBezier::New(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 shaderEffectCustom = Dali::ShaderEffect::NewWithPrefix( vertexShaderPrefix.str(),vertexShader,
112 "#extension GL_OES_standard_derivatives:enable\n", fragmentShader,
113 GEOMETRY_TYPE_UNTEXTURED_MESH );
115 Dali::Toolkit::QuadraticBezier handle( shaderEffectCustom );
117 //Set default uniform values
118 handle.SetUniform( COLOR_PROPERTY_NAME, Vector4(1.0f,1.0f,1.0f,1.0f) );
121 //Set default line widht to 1 pixel
122 handle.SetUniform( LINEWIDTH_PROPERTY_NAME, 1.0f );
128 void QuadraticBezier::SetPoint(unsigned int index, const Vector3& position)
130 SetUniform( GetPointPropertyName(index), position );
133 void QuadraticBezier::SetLineWidth( float width )
135 SetUniform( LINEWIDTH_PROPERTY_NAME, width );
138 void QuadraticBezier::SetColor( const Vector4& color )
140 SetUniform( COLOR_PROPERTY_NAME, color );
143 std::string QuadraticBezier::GetPointPropertyName(unsigned int index) const
145 std::ostringstream propertyName;
146 propertyName<<POINT_PROPERTY_NAME<<"["<<index<<"]";
147 return propertyName.str();
150 std::string QuadraticBezier::GetLineWidthPropertyName( ) const
152 return LINEWIDTH_PROPERTY_NAME;
155 std::string QuadraticBezier::GetColorPropertyName( ) const
157 return COLOR_PROPERTY_NAME;
160 } // namespace Toolkit