[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / ripple2d-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/ripple2d-effect.h>
18
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string TEXTURE_SIZE_PROPERTY_NAME( "uTextureSize" );
30 const std::string AMPLITUDE_PROPERTY_NAME( "uAmplitude" );
31 const std::string TIME_PROPERTY_NAME( "uTime" );
32
33 } // namespace
34
35 Ripple2DEffect::Ripple2DEffect()
36 {
37 }
38
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 Ripple2DEffect::Ripple2DEffect(ShaderEffect handle)
41 :ShaderEffect(handle)
42 {
43 }
44
45 Ripple2DEffect::~Ripple2DEffect()
46 {
47 }
48
49
50 Ripple2DEffect Ripple2DEffect::New()
51 {
52     // append the default version
53     std::string fragmentShader(
54         "uniform float uAmplitude;\n"
55         "uniform float uTime;\n"
56         "void main()\n"
57         "{\n"
58         "  highp vec2 textureSize = sTextureRect.zw - sTextureRect.xy;\n"
59         "  highp vec2 pos = -1.0 + 2.0 * vTexCoord.st/textureSize;\n"
60         "  highp float len = length(pos);\n"
61         "  highp vec2 texCoord = vTexCoord.st/textureSize + pos/len * sin( len * 12.0 - uTime * 4.0 ) * uAmplitude;\n"
62         "  gl_FragColor = texture2D(sTexture, texCoord) * uColor;\n"
63         "}" );
64
65   // Create the implementation, temporarily owned on stack
66   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
67       "",
68       fragmentShader,
69       Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
70       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
71
72   /* Pass ownership to Ripple2DEffect through overloaded constructor, So that it now has access to the
73      Dali::ShaderEffect implementation */
74   Dali::Toolkit::Ripple2DEffect handle( shaderEffect );
75
76   handle.SetUniform( TEXTURE_SIZE_PROPERTY_NAME, Vector2(0.0f, 0.0f) );
77   handle.SetUniform( AMPLITUDE_PROPERTY_NAME, 0.0f );
78   handle.SetUniform( TIME_PROPERTY_NAME, 0.0f );
79
80   return handle;
81
82 }
83
84 void Ripple2DEffect::SetAmplitude(float amplitude)
85 {
86   SetUniform( AMPLITUDE_PROPERTY_NAME, amplitude );
87 }
88
89 void Ripple2DEffect::SetTime(float time)
90 {
91   SetUniform( TIME_PROPERTY_NAME, time );
92 }
93
94 const std::string& Ripple2DEffect::GetAmplitudePropertyName() const
95 {
96   return AMPLITUDE_PROPERTY_NAME;
97 }
98
99 const std::string& Ripple2DEffect::GetTimePropertyName() const
100 {
101   return TIME_PROPERTY_NAME;
102 }
103
104 } // namespace Toolkit
105
106 } // namespace Dali