Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / dissolve-local-effect.cpp
1 /*
2  * Copyright (c) 2014 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 <sstream>
19
20 #include <dali-toolkit/public-api/shader-effects/dissolve-local-effect.h>
21
22 #include <dali-toolkit/public-api/shader-effects/dissolve-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace
31 {
32   const std::string DISTORTION_PROPERTY_NAME( "uPercentage" );
33   const std::string CENTER_PROPERTY_NAME( "uCenter" );
34   const std::string RADIUS_PROPERTY_NAME( "uRadius" );
35   const std::string TRANSPARENCY_PROPERTY_NAME( "uTransparency" );
36 }
37
38 DissolveLocalEffect::DissolveLocalEffect()
39 {
40 }
41
42 //Call the Parent copy constructor to add reference to the implementation for this object
43 DissolveLocalEffect::DissolveLocalEffect( ShaderEffect handle )
44 : ShaderEffect( handle )
45 {
46 }
47
48 DissolveLocalEffect::~DissolveLocalEffect()
49 {
50 }
51
52 DissolveLocalEffect DissolveLocalEffect::New( unsigned int numberOfDimples )
53 {
54   std::ostringstream vertexShaderStringStream;
55   vertexShaderStringStream << "#define NUMBER_OF_DIMPLE "<< numberOfDimples << "\n";
56   std::string vertexShader(
57     "uniform vec2 uCenter[ NUMBER_OF_DIMPLE ];\n"
58     "uniform float uRadius[ NUMBER_OF_DIMPLE ]; \n"
59     "uniform float uPercentage[ NUMBER_OF_DIMPLE ]; \n"
60     "varying float vPercentage;\n"
61     "void main()\n"
62     "{\n"
63     "  vec4 position = uModelView * vec4( aPosition, 1.0 );\n"
64     "  float percentage = 0.0;\n"
65     "  for( int i=0; i<NUMBER_OF_DIMPLE; ++i )\n"
66     "  {\n"
67     "    float distance = distance(uCenter[i], position.xy);\n"
68     "    percentage = max(percentage, uPercentage[i] * cos(clamp( distance/uRadius[i], 0.0, 1.0 )*1.57) );"
69     "  }\n"
70     "  vPercentage = clamp( percentage, 0.0, 1.0 );\n"
71     "  gl_Position = uProjection * position;\n"
72     "  vTexCoord = aTexCoord;\n"
73     "}\n");
74   vertexShaderStringStream << vertexShader;
75
76   std::string fragmentShader(
77     "precision highp float;\n"
78     "uniform float uTransparency;\n"
79     "varying float vPercentage;\n"
80     "float rand(vec2 co) \n"
81     "{\n"
82     "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
83     "}\n"
84     "void main()\n"
85     "{\n"
86       //Calculate the randomness
87     "  float offsetS = rand( vTexCoord * vPercentage ); \n"
88     "  float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ); \n"
89     "  vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
90     "  gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
91     "  gl_FragColor.a *= 1.0 - uTransparency*vPercentage; \n"
92     "}\n");
93
94   ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
95                                                  GeometryType( GEOMETRY_TYPE_IMAGE),
96                                                  ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
97
98   DissolveLocalEffect handle( shaderEffect );
99   handle.mNumberOfDimples = numberOfDimples;
100
101   //Register uniform properties
102   for( unsigned int i = 0; i < numberOfDimples; i++ )
103   {
104     handle.SetCenter(i, Vector2(0.f,0.f));
105     handle.SetRadius(i, 0.f);
106     handle.SetDistortion( i, 0.f );
107   }
108   handle.SetProperty( ShaderEffect::GRID_DENSITY, Property::Value(5.f) );
109   handle.SetTransparency( 0.5f );
110
111   return handle;
112 }
113
114 unsigned int DissolveLocalEffect::GetNumberOfDimples() const
115 {
116   return mNumberOfDimples;
117 }
118
119 void DissolveLocalEffect::SetTransparency( float transparency)
120 {
121   SetUniform( TRANSPARENCY_PROPERTY_NAME, transparency );
122 }
123
124 void DissolveLocalEffect::SetCenter( unsigned int index, const Vector2& center )
125 {
126   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
127   SetUniform( GetCenterPropertyName( index ), center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
128 }
129
130 void DissolveLocalEffect::SetRadius( unsigned int index, float radius )
131 {
132   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
133   SetUniform( GetRadiusPropertyName( index ), radius );
134 }
135
136 void DissolveLocalEffect::SetDistortion( unsigned int index, float distortion )
137 {
138   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
139   SetUniform( GetDistortionPropertyName( index ), distortion );
140 }
141
142 std::string DissolveLocalEffect::GetCenterPropertyName( unsigned int index ) const
143 {
144   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
145   std::ostringstream oss;
146   oss<< CENTER_PROPERTY_NAME << "[" << index << "]";
147   return oss.str();
148 }
149
150 std::string DissolveLocalEffect::GetRadiusPropertyName( unsigned int index ) const
151 {
152   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
153   std::ostringstream oss;
154   oss<< RADIUS_PROPERTY_NAME << "[" << index << "]";
155   return oss.str();
156 }
157
158 std::string DissolveLocalEffect::GetDistortionPropertyName( unsigned int index ) const
159 {
160   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
161   std::ostringstream oss;
162   oss<< DISTORTION_PROPERTY_NAME << "["<< index << "]";
163   return oss.str();
164 }
165
166 } // namespace Toolkit
167
168 } // namespace Dali