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.
18 #include <dali-toolkit/public-api/shader-effects/bendy-effect.h>
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string DIRECTION_PROPERTY_NAME( "uDirection" );
31 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
35 BendyEffect::BendyEffect()
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 BendyEffect::BendyEffect(ShaderEffect handle)
45 BendyEffect::~BendyEffect()
50 BendyEffect BendyEffect::New()
52 // append the default version
53 std::string vertextShader(
54 "uniform mediump vec2 uCenter;\n"
55 "uniform mediump vec2 uDirection;\n"
56 "uniform mediump float uRadius;\n"
58 "varying mediump float vShade;\n"
62 " mediump float lighting = 0.25;\n"
63 " mediump vec4 position = uModelView * vec4(aPosition,1.0);\n"
65 " mediump vec2 d = position.xy - uCenter;\n"
66 " mediump float dist = max( 0.0, dot(d,uDirection) );\n"
67 " mediump float radius = max(0.0, uRadius - dist * 0.01);\n"
69 " mediump float cs = cos(dist / radius / 2.0);\n"
70 " mediump float sn = sin(dist / radius / 2.0);\n"
72 "position.xy = position.xy - uDirection * dist;\n"
74 "position.xy += uDirection * sn * radius;\n"
75 "position.z += (1.0 - cs) * radius;\n"
77 "gl_Position = uProjection * position;\n"
79 "vShade = 1.0 - abs(sn) * lighting;\n"
81 "vTexCoord = aTexCoord;\n"
84 std::string fragmentShader(
85 "varying mediump float vShade;\n"
89 " gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0);\n"
92 // Create the implementation, temporarily owned on stack,
93 Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New(
96 GeometryType( GEOMETRY_TYPE_IMAGE ),
97 ShaderEffect::GeometryHints( HINT_GRID | HINT_DEPTH_BUFFER ));
99 /* Pass ownership to BendyEffect through overloaded constructor, So that it now has access to the
100 Dali::ShaderEffect implementation */
101 Dali::Toolkit::BendyEffect handle( shaderEffectCustom );
103 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_POSITION );
104 handle.SetUniform( DIRECTION_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_DIRECTION );
105 handle.SetUniform( RADIUS_PROPERTY_NAME, 0.0f );
110 void BendyEffect::SetCenter( const Vector2& center )
112 SetUniform( CENTER_PROPERTY_NAME, center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
115 void BendyEffect::SetDirection( const Vector2& direction )
117 Vector2 temp(direction);
120 Vector2 newDirection(temp.x, temp.y);
122 SetUniform( DIRECTION_PROPERTY_NAME, newDirection, ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
125 void BendyEffect::SetRadius( float radius )
127 SetUniform( RADIUS_PROPERTY_NAME, radius );
130 const std::string& BendyEffect::GetCenterPropertyName() const
132 return CENTER_PROPERTY_NAME;
135 const std::string& BendyEffect::GetDirectionPropertyName() const
137 return DIRECTION_PROPERTY_NAME;
140 const std::string& BendyEffect::GetRadiusPropertyName() const
142 return RADIUS_PROPERTY_NAME;
145 } // namespace Toolkit