2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
17 #include <dali-toolkit/public-api/shader-effects/bendy-effect.h>
28 const std::string CENTER_PROPERTY_NAME( "uCenter" );
29 const std::string DIRECTION_PROPERTY_NAME( "uDirection" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
34 BendyEffect::BendyEffect()
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 BendyEffect::BendyEffect(ShaderEffect handle)
44 BendyEffect::~BendyEffect()
49 BendyEffect BendyEffect::New()
51 // append the default version
52 std::string vertextShader(
53 "uniform mediump vec2 uCenter;\n"
54 "uniform mediump vec2 uDirection;\n"
55 "uniform mediump float uRadius;\n"
57 "varying mediump float vShade;\n"
61 " mediump float lighting = 0.25;\n"
62 " mediump vec4 position = uModelView * vec4(aPosition,1.0);\n"
64 " mediump vec2 d = position.xy - uCenter;\n"
65 " mediump float dist = max( 0.0, dot(d,uDirection) );\n"
66 " mediump float radius = max(0.0, uRadius - dist * 0.01);\n"
68 " mediump float cs = cos(dist / radius / 2.0);\n"
69 " mediump float sn = sin(dist / radius / 2.0);\n"
71 "position.xy = position.xy - uDirection * dist;\n"
73 "position.xy += uDirection * sn * radius;\n"
74 "position.z += (1.0 - cs) * radius;\n"
76 "gl_Position = uProjection * position;\n"
78 "vShade = 1.0 - abs(sn) * lighting;\n"
80 "vTexCoord = aTexCoord;\n"
83 std::string fragmentShader(
84 "varying mediump float vShade;\n"
88 " gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0);\n"
91 // Create the implementation, temporarily owned on stack,
92 Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New(
95 GeometryType( GEOMETRY_TYPE_IMAGE ),
96 ShaderEffect::GeometryHints( HINT_GRID | HINT_DEPTH_BUFFER ));
98 /* Pass ownership to BendyEffect through overloaded constructor, So that it now has access to the
99 Dali::ShaderEffect implementation */
100 Dali::Toolkit::BendyEffect handle( shaderEffectCustom );
102 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_POSITION );
103 handle.SetUniform( DIRECTION_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_DIRECTION );
104 handle.SetUniform( RADIUS_PROPERTY_NAME, 0.0f );
109 void BendyEffect::SetCenter( const Vector2& center )
111 SetUniform( CENTER_PROPERTY_NAME, center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
114 void BendyEffect::SetDirection( const Vector2& direction )
116 Vector2 temp(direction);
119 Vector2 newDirection(temp.x, temp.y);
121 SetUniform( DIRECTION_PROPERTY_NAME, newDirection, ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
124 void BendyEffect::SetRadius( float radius )
126 SetUniform( RADIUS_PROPERTY_NAME, radius );
129 const std::string& BendyEffect::GetCenterPropertyName() const
131 return CENTER_PROPERTY_NAME;
134 const std::string& BendyEffect::GetDirectionPropertyName() const
136 return DIRECTION_PROPERTY_NAME;
139 const std::string& BendyEffect::GetRadiusPropertyName() const
141 return RADIUS_PROPERTY_NAME;
144 } // namespace Toolkit