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