[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / iris-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/iris-effect.h>
18 namespace Dali
19 {
20
21 namespace Toolkit
22 {
23
24 namespace
25 {
26 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
27 const std::string CENTER_PROPERTY_NAME( "uCenter" );
28 const std::string BLEND_FACTOR_PROPERTY_NAME( "uBlendFactor" );
29 } // namespace
30
31 IrisEffect::IrisEffect()
32 {
33 }
34
35 //Call the Parent copy constructor to add reference to the implementation for this object
36 IrisEffect::IrisEffect(ShaderEffect handle)
37 :ShaderEffect(handle)
38 {
39 }
40
41 IrisEffect::~IrisEffect()
42 {
43 }
44
45 IrisEffect IrisEffect::New()
46 {
47   // append the default version
48   std::string vertexShader(
49               "uniform mediump vec2 uCenter;\n"
50               "varying vec2 vRelativePosition;\n"
51               "\n"
52               "void main()\n"
53               "{\n"
54               "    vec4 world = uModelView * vec4(aPosition,1.0);\n"
55               "    gl_Position = uProjection * world;\n"
56               "    \n"
57               "    vTexCoord = aTexCoord;\n"
58               "    vRelativePosition = aTexCoord - uCenter;\n"
59               "}\n");
60
61   std::string fragmentShader(
62               "uniform float uRadius;                                                           \n"
63               "uniform float uBlendFactor;                                                      \n"
64               "varying vec2 vRelativePosition;                                                  \n"
65               "void main()                                                                      \n"
66               "{                                                                                \n"
67               "   float delta = (length(vRelativePosition) - uRadius);                          \n"
68               "   delta = clamp(0.0 - delta * uBlendFactor, 0.0, 1.0);                          \n"
69               "   gl_FragColor = texture2D(sTexture, vTexCoord) * uColor;                       \n"
70               "   gl_FragColor.a *= delta;                                                      \n"
71               "}                                                                                \n"
72               );
73
74   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
75       vertexShader,
76       fragmentShader,
77       GeometryType( GEOMETRY_TYPE_IMAGE ),
78       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ));
79
80   Dali::Toolkit::IrisEffect handle( shaderEffectCustom );
81   handle.SetUniform( RADIUS_PROPERTY_NAME, 0.0f );
82   handle.SetUniform( BLEND_FACTOR_PROPERTY_NAME, 100.0f );
83   handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.5f, 0.5f) );
84   return handle;
85 }
86
87 void IrisEffect::SetRadius( float radius )
88 {
89   SetUniform( RADIUS_PROPERTY_NAME, radius );
90 }
91
92 void IrisEffect::SetBlendFactor(float value )
93 {
94   SetUniform( BLEND_FACTOR_PROPERTY_NAME, value );
95 }
96
97 void IrisEffect::SetCenter( const Vector2& center )
98 {
99   SetUniform( CENTER_PROPERTY_NAME, center );
100 }
101
102 const std::string& IrisEffect::GetRadiusPropertyName() const
103 {
104   return RADIUS_PROPERTY_NAME;
105 }
106
107 const std::string& IrisEffect::GetBlendFactorPropertyName() const
108 {
109   return BLEND_FACTOR_PROPERTY_NAME;
110 }
111
112 const std::string& IrisEffect::GetCenterPropertyName() const
113 {
114   return CENTER_PROPERTY_NAME;
115 }
116
117
118 }
119 }
120