[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / swirl-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/swirl-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string ANGLE_PROPERTY_NAME( "uAngle" );
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
31
32 } // namespace
33
34 SwirlEffect::SwirlEffect()
35 {
36 }
37
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 SwirlEffect::SwirlEffect(ShaderEffect handle)
40 :ShaderEffect(handle)
41 {
42 }
43
44 SwirlEffect::~SwirlEffect()
45 {
46 }
47
48
49 SwirlEffect SwirlEffect::New(bool warp)
50 {
51   // append the default version
52   std::string fragmentShader(
53       "uniform vec2  uTextureSize;\n"
54       "uniform float uRadius;\n"
55       "uniform float uAngle;\n"
56       "uniform vec2  uCenter;\n"
57       "void main()\n"
58       "{\n"
59       "  vec2 textureCenter = (sTextureRect.xy + sTextureRect.zw) * 0.5;\n"
60       "  textureCenter = vTexCoord.st - textureCenter;\n"
61       "  float distance = length(textureCenter);\n"
62       "  if (distance >= uRadius)\n"
63       "     discard;\n"
64       "  float percent = (uRadius - distance) / uRadius;\n"
65       "  float theta = percent * percent * uAngle * 4.0;\n"
66       "  float sinTheta = sin(theta);\n"
67       "  float cosTheta = cos(theta);\n" );
68       // if warp, loose the sign from sin
69   if( warp )
70   {
71     fragmentShader.append(
72       "  textureCenter = vec2( dot( textureCenter, vec2(cosTheta, sinTheta) ), "
73       "                        dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
74   }
75   else
76   {
77     fragmentShader.append(
78       "  textureCenter = vec2( dot( textureCenter, vec2(cosTheta, -sinTheta) ), "
79       "                        dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
80   }
81   fragmentShader.append(
82     "  textureCenter += uCenter;\n"
83     "  gl_FragColor = texture2D( sTexture, textureCenter ) * uColor;\n"
84     "}" );
85
86   // Create the implementation, temporarily owned on stack,
87   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
88       "",
89       fragmentShader,
90       Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
91       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
92
93   /* Pass ownership to SwirlEffect through overloaded constructor, So that it now has access to the
94      Dali::ShaderEffect implementation */
95   Dali::Toolkit::SwirlEffect handle( shaderEffectCustom );
96
97   handle.SetUniform( ANGLE_PROPERTY_NAME, 0.0f );
98   handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.5f, 0.5f) );
99   handle.SetUniform( RADIUS_PROPERTY_NAME, 1.0f );
100
101   return handle;
102 }
103
104 void SwirlEffect::SetAngle(float angle)
105 {
106   SetUniform( ANGLE_PROPERTY_NAME, angle );
107 }
108
109 void SwirlEffect::SetCenter(const Vector2& center)
110 {
111   SetUniform( CENTER_PROPERTY_NAME, center );
112 }
113
114 void SwirlEffect::SetRadius(float radius)
115 {
116   SetUniform( RADIUS_PROPERTY_NAME, radius );
117 }
118
119 const std::string& SwirlEffect::GetAnglePropertyName() const
120 {
121   return ANGLE_PROPERTY_NAME;
122 }
123
124 const std::string& SwirlEffect::GetCenterPropertyName() const
125 {
126   return CENTER_PROPERTY_NAME;
127 }
128
129 const std::string& SwirlEffect::GetRadiusPropertyName() const
130 {
131   return RADIUS_PROPERTY_NAME;
132 }
133
134 } // namespace Toolkit
135
136 } // namespace Dali