2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
17 #include <dali-toolkit/public-api/shader-effects/spot-effect.h>
28 const float DEFAULT_RADIUS = 0.0f;
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
34 SpotEffect::SpotEffect()
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 SpotEffect::SpotEffect(ShaderEffect handle)
44 SpotEffect::~SpotEffect()
48 SpotEffect SpotEffect::New()
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"
58 " vec4 world = vec4(aPosition, 1.0);\n"
60 " vec2 d = vec2(world.xy - uCenter);\n"
61 " float dist = length(d);\n"
63 " float range = (uRadius - dist) / (uRadius);\n"
64 " vRange = max(0.1, range);\n"
66 " gl_Position = uMvpMatrix * world;\n"
67 " vTexCoord = aTexCoord;\n"
70 std::string fragmentShader(
71 "varying mediump float vRange;\n"
75 " gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(vRange, vRange, vRange, 1.0) * uColor;\n"
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 ));
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 );
87 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f) );
88 handle.SetUniform( RADIUS_PROPERTY_NAME, DEFAULT_RADIUS );
94 void SpotEffect::SetCenter(const Vector2& center)
96 SetUniform( CENTER_PROPERTY_NAME, center );
99 void SpotEffect::SetRadius(float radius)
101 SetUniform( RADIUS_PROPERTY_NAME, radius );
104 const std::string& SpotEffect::GetCenterPropertyName() const
106 return CENTER_PROPERTY_NAME;
109 const std::string& SpotEffect::GetRadiusPropertyName() const
111 return RADIUS_PROPERTY_NAME;
114 } // namespace Toolkit