Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / bendy-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 #include <dali-toolkit/public-api/shader-effects/bendy-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string CENTER_PROPERTY_NAME( "uCenter" );
29 const std::string DIRECTION_PROPERTY_NAME( "uDirection" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
31
32 } // namespace
33
34 BendyEffect::BendyEffect()
35 {
36 }
37
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 BendyEffect::BendyEffect(ShaderEffect handle)
40 :ShaderEffect(handle)
41 {
42 }
43
44 BendyEffect::~BendyEffect()
45 {
46 }
47
48
49 BendyEffect BendyEffect::New()
50 {
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"
56               "\n"
57               "varying mediump   float vShade;\n"
58               "\n"
59               "void main()\n"
60               "{\n"
61                   " mediump float lighting = 0.25;\n"
62                   " mediump vec4 position = uModelView * vec4(aPosition,1.0);\n"
63                   "\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"
67                   "\n"
68                   " mediump float cs = cos(dist / radius / 2.0);\n"
69                   " mediump float sn = sin(dist / radius / 2.0);\n"
70                   "\n"
71                   "position.xy = position.xy - uDirection * dist;\n"
72                   "\n"
73                   "position.xy += uDirection * sn * radius;\n"
74                   "position.z += (1.0 - cs) * radius;\n"
75                   "\n"
76                   "gl_Position = uProjection * position;\n"
77                   "\n"
78                   "vShade = 1.0 - abs(sn) * lighting;\n"
79                   "\n"
80                   "vTexCoord = aTexCoord;\n"
81               "}" );
82
83   std::string fragmentShader(
84               "varying mediump float  vShade;\n"
85               "\n"
86               "void main()\n"
87               "{\n"
88               "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0);\n"
89               "}" );
90
91   // Create the implementation, temporarily owned on stack,
92   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
93       vertextShader,
94       fragmentShader,
95       GeometryType( GEOMETRY_TYPE_IMAGE ),
96       ShaderEffect::GeometryHints( HINT_GRID | HINT_DEPTH_BUFFER ));
97
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 );
101
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 );
105
106   return handle;
107 }
108
109 void BendyEffect::SetCenter( const Vector2& center )
110 {
111   SetUniform( CENTER_PROPERTY_NAME, center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
112 }
113
114 void BendyEffect::SetDirection( const Vector2& direction )
115 {
116   Vector2 temp(direction);
117   temp.Normalize();
118
119   Vector2 newDirection(temp.x, temp.y);
120
121   SetUniform( DIRECTION_PROPERTY_NAME, newDirection, ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
122 }
123
124 void BendyEffect::SetRadius( float radius )
125 {
126   SetUniform( RADIUS_PROPERTY_NAME, radius );
127 }
128
129 const std::string& BendyEffect::GetCenterPropertyName() const
130 {
131   return CENTER_PROPERTY_NAME;
132 }
133
134 const std::string& BendyEffect::GetDirectionPropertyName() const
135 {
136   return DIRECTION_PROPERTY_NAME;
137 }
138
139 const std::string& BendyEffect::GetRadiusPropertyName() const
140 {
141   return RADIUS_PROPERTY_NAME;
142 }
143
144 } // namespace Toolkit
145
146 } // namespace Dali