eed6958d49cf12c7f9e126e38e193dfdf942147c
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / spot-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/spot-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const float DEFAULT_RADIUS = 0.0f;
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
31
32 } // namespace
33
34 SpotEffect::SpotEffect()
35 {
36 }
37
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 SpotEffect::SpotEffect(ShaderEffect handle)
40 :ShaderEffect(handle)
41 {
42 }
43
44 SpotEffect::~SpotEffect()
45 {
46 }
47
48 SpotEffect SpotEffect::New()
49 {
50   // append the default version
51   std::string vertexShader(
52       "uniform mediump   vec2  uCenter;\n"
53       "uniform mediump   float  uRadius;\n"
54       "varying mediump   float  vRange;\n"
55       "\n"
56       "void main()\n"
57       "{\n"
58       "  vec4 world = vec4(aPosition, 1.0);\n"
59       "  \n"
60       "  vec2 d = vec2(world.xy - uCenter);\n"
61       "  float dist = length(d);\n"
62       "  \n"
63       "  float range = (uRadius - dist) / (uRadius);\n"
64       "  vRange = max(0.1, range);\n"
65       "  \n"
66       "  gl_Position = uMvpMatrix * world;\n"
67       "  vTexCoord = aTexCoord;\n"
68       "}");
69
70   std::string fragmentShader(
71       "varying mediump   float  vRange;\n"
72       "\n"
73       "void main()\n"
74       "{\n"
75       "  gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(vRange, vRange, vRange, 1.0) * uColor;\n"
76       "}" );
77
78   // Create the implementation, temporarily owned on stack
79   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(vertexShader, fragmentShader,
80                                                              Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
81                                                              ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID ));
82
83   /* Pass ownership to SpotEffect through overloaded constructor, So that it now has access to the
84      Dali::ShaderEffect implementation */
85   Dali::Toolkit::SpotEffect handle( shaderEffect );
86
87   handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f) );
88   handle.SetUniform( RADIUS_PROPERTY_NAME, DEFAULT_RADIUS );
89
90   return handle;
91
92 }
93
94 void SpotEffect::SetCenter(const Vector2& center)
95 {
96   SetUniform( CENTER_PROPERTY_NAME, center );
97 }
98
99 void SpotEffect::SetRadius(float radius)
100 {
101   SetUniform( RADIUS_PROPERTY_NAME, radius );
102 }
103
104 const std::string& SpotEffect::GetCenterPropertyName() const
105 {
106   return CENTER_PROPERTY_NAME;
107 }
108
109 const std::string& SpotEffect::GetRadiusPropertyName() const
110 {
111   return RADIUS_PROPERTY_NAME;
112 }
113
114 } // namespace Toolkit
115
116 } // namespace Dali