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