Merge "Control to call Init derived classes before applying style" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / bendy-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/bendy-effect.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string DIRECTION_PROPERTY_NAME( "uDirection" );
31 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
32
33 } // namespace
34
35 BendyEffect::BendyEffect()
36 {
37 }
38
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 BendyEffect::BendyEffect(ShaderEffect handle)
41 :ShaderEffect(handle)
42 {
43 }
44
45 BendyEffect::~BendyEffect()
46 {
47 }
48
49
50 BendyEffect BendyEffect::New()
51 {
52   // append the default version
53   std::string vertextShader(
54               "uniform mediump   vec2  uCenter;\n"
55               "uniform mediump   vec2  uDirection;\n"
56               "uniform mediump   float uRadius;\n"
57               "\n"
58               "varying mediump   float vShade;\n"
59               "\n"
60               "void main()\n"
61               "{\n"
62                   " mediump float lighting = 0.25;\n"
63                   " mediump vec4 position = uModelView * vec4(aPosition,1.0);\n"
64                   "\n"
65                   " mediump vec2 d = position.xy - uCenter;\n"
66                   " mediump float dist = max( 0.0, dot(d,uDirection) );\n"
67                   " mediump float radius = max(0.0, uRadius - dist * 0.01);\n"
68                   "\n"
69                   " mediump float cs = cos(dist / radius / 2.0);\n"
70                   " mediump float sn = sin(dist / radius / 2.0);\n"
71                   "\n"
72                   "position.xy = position.xy - uDirection * dist;\n"
73                   "\n"
74                   "position.xy += uDirection * sn * radius;\n"
75                   "position.z += (1.0 - cs) * radius;\n"
76                   "\n"
77                   "gl_Position = uProjection * position;\n"
78                   "\n"
79                   "vShade = 1.0 - abs(sn) * lighting;\n"
80                   "\n"
81                   "vTexCoord = aTexCoord;\n"
82               "}" );
83
84   std::string fragmentShader(
85               "varying mediump float  vShade;\n"
86               "\n"
87               "void main()\n"
88               "{\n"
89               "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0);\n"
90               "}" );
91
92   // Create the implementation, temporarily owned on stack,
93   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
94       vertextShader,
95       fragmentShader,
96       GeometryType( GEOMETRY_TYPE_IMAGE ),
97       ShaderEffect::GeometryHints( HINT_GRID | HINT_DEPTH_BUFFER ));
98
99   /* Pass ownership to BendyEffect through overloaded constructor, So that it now has access to the
100      Dali::ShaderEffect implementation */
101   Dali::Toolkit::BendyEffect handle( shaderEffectCustom );
102
103   handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_POSITION );
104   handle.SetUniform( DIRECTION_PROPERTY_NAME, Vector2(0.0f, 0.0f), COORDINATE_TYPE_VIEWPORT_DIRECTION );
105   handle.SetUniform( RADIUS_PROPERTY_NAME, 0.0f );
106
107   return handle;
108 }
109
110 void BendyEffect::SetCenter( const Vector2& center )
111 {
112   SetUniform( CENTER_PROPERTY_NAME, center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
113 }
114
115 void BendyEffect::SetDirection( const Vector2& direction )
116 {
117   Vector2 temp(direction);
118   temp.Normalize();
119
120   Vector2 newDirection(temp.x, temp.y);
121
122   SetUniform( DIRECTION_PROPERTY_NAME, newDirection, ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
123 }
124
125 void BendyEffect::SetRadius( float radius )
126 {
127   SetUniform( RADIUS_PROPERTY_NAME, radius );
128 }
129
130 const std::string& BendyEffect::GetCenterPropertyName() const
131 {
132   return CENTER_PROPERTY_NAME;
133 }
134
135 const std::string& BendyEffect::GetDirectionPropertyName() const
136 {
137   return DIRECTION_PROPERTY_NAME;
138 }
139
140 const std::string& BendyEffect::GetRadiusPropertyName() const
141 {
142   return RADIUS_PROPERTY_NAME;
143 }
144
145 } // namespace Toolkit
146
147 } // namespace Dali