Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / ripple-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/ripple-effect.h>
18
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string AMPLITUDE_PROPERTY_NAME( "uAmplitude" );
30 const std::string CENTER_PROPERTY_NAME( "uCenter" );
31 const std::string TIME_PROPERTY_NAME( "uTime" );
32
33 } // namespace
34
35 RippleEffect::RippleEffect()
36 {
37 }
38
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 RippleEffect::RippleEffect(ShaderEffect handle)
41 :ShaderEffect(handle)
42 {
43 }
44
45 RippleEffect::~RippleEffect()
46 {
47 }
48
49
50 RippleEffect RippleEffect::New()
51 {
52
53   std::string vertexShader(
54        "precision mediump float;\n"
55        "uniform mediump   vec2  uCenter;\n"
56        "uniform mediump   float uTime;\n"
57        "uniform mediump   float uAmplitude;\n"
58        "uniform mediump   float uLighting;\n"
59        "uniform mediump   float uWaveLength;\n"
60        "varying mediump   float vLight;\n"
61        "varying mediump   float vShade;\n"
62        "void main()\n"
63        "{\n"
64           "float lighting = uAmplitude * 0.02;\n"
65           "float waveLength = uAmplitude * 0.0016;\n"
66           "vec4 world = uModelView * vec4(aPosition,1.0);\n"
67           "vec2 d = vec2(world.x - uCenter.x, world.y - uCenter.y);\n"
68           "float dist = length(d);\n"
69           "float amplitude = cos(uTime - dist*waveLength);\n"
70           "float slope     = sin(uTime - dist*waveLength);\n"
71           "world.z += amplitude * uAmplitude;\n"
72           "gl_Position = uProjection * world;\n"
73           "vec2 lightDirection = vec2(-0.707,0.707);\n"
74           "float dot = 0.0;\n"
75           "if(dist > 0.0)\n"
76           "{\n"
77           "  dot = dot(normalize(d),lightDirection) * lighting;\n"
78           "}\n"
79           "vShade = 1.0 - (dot * slope);\n"
80           "vLight = max(0.0, dot * -slope);\n"
81           "vTexCoord = aTexCoord;\n"
82         "}" );
83
84   // append the default version
85   std::string imageFragmentShader(
86         "precision mediump float;\n"
87         "varying mediump float  vLight;\n"
88         "varying mediump float  vShade;\n"
89         "void main()\n"
90         "{\n"
91         "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0) + vec4(vLight, vLight, vLight,0.0);\n"
92         "}" );
93
94   // Create the implementation, temporarily owned on stack
95   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
96       vertexShader, imageFragmentShader, vertexShader, "", GeometryHints(HINT_GRID) );
97
98   /* Pass ownership to RippleEffect through overloaded constructor, So that it now has access to the
99      Dali::ShaderEffect implementation */
100   Dali::Toolkit::RippleEffect handle( shaderEffect );
101
102   handle.SetUniform( AMPLITUDE_PROPERTY_NAME, 0.0f );
103   handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f));
104   handle.SetUniform( TIME_PROPERTY_NAME, 0.0f );
105
106   return handle;
107 }
108
109 void RippleEffect::SetAmplitude(float amplitude)
110 {
111   SetUniform( AMPLITUDE_PROPERTY_NAME, amplitude );
112 }
113
114 void RippleEffect::SetCenter(const Vector2& center)
115 {
116   SetUniform( CENTER_PROPERTY_NAME, center );
117 }
118
119 void RippleEffect::SetTime(float time)
120 {
121   SetUniform( TIME_PROPERTY_NAME, time );
122 }
123
124 const std::string& RippleEffect::GetAmplitudePropertyName() const
125 {
126   return AMPLITUDE_PROPERTY_NAME;
127 }
128
129 const std::string& RippleEffect::GetCenterPropertyName() const
130 {
131   return CENTER_PROPERTY_NAME;
132 }
133
134 const std::string& RippleEffect::GetTimePropertyName() const
135 {
136   return TIME_PROPERTY_NAME;
137 }
138
139 } // namespace Toolkit
140
141 } // namespace Dali