2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/devel-api/shader-effects/spot-effect.h>
29 const float DEFAULT_RADIUS = 0.0f;
30 const std::string CENTER_PROPERTY_NAME( "uCenter" );
31 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
35 SpotEffect::SpotEffect()
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 SpotEffect::SpotEffect(ShaderEffect handle)
45 SpotEffect::~SpotEffect()
49 SpotEffect SpotEffect::New()
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"
59 " mediump vec4 world = vec4(aPosition, 1.0);\n"
61 " mediump vec2 d = vec2(world.xy - uCenter);\n"
62 " mediump float dist = length(d);\n"
64 " mediump float range = (uRadius - dist) / (uRadius);\n"
65 " vRange = max(0.1, range);\n"
67 " gl_Position = uMvpMatrix * world;\n"
68 " vTexCoord = aTexCoord;\n"
71 std::string fragmentShader(
72 "varying mediump float vRange;\n"
76 " gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(vRange, vRange, vRange, 1.0) * uColor;\n"
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 ));
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 );
88 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f) );
89 handle.SetUniform( RADIUS_PROPERTY_NAME, DEFAULT_RADIUS );
95 void SpotEffect::SetCenter(const Vector2& center)
97 SetUniform( CENTER_PROPERTY_NAME, center );
100 void SpotEffect::SetRadius(float radius)
102 SetUniform( RADIUS_PROPERTY_NAME, radius );
105 const std::string& SpotEffect::GetCenterPropertyName() const
107 return CENTER_PROPERTY_NAME;
110 const std::string& SpotEffect::GetRadiusPropertyName() const
112 return RADIUS_PROPERTY_NAME;
115 } // namespace Toolkit