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