Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / mirror-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/mirror-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string DEPTH_PROPERTY_NAME( "uDepth" );
29 const std::string ALPHA_PROPERTY_NAME( "uAlpha" );
30
31 } // namespace
32
33 MirrorEffect::MirrorEffect()
34 {
35 }
36
37 //Call the Parent copy constructor to add reference to the implementation for this object
38 MirrorEffect::MirrorEffect(ShaderEffect handle)
39 :ShaderEffect(handle)
40 {
41 }
42
43 MirrorEffect::~MirrorEffect()
44 {
45 }
46
47 MirrorEffect MirrorEffect::New()
48 {
49
50   std::string vertextShader(
51       "precision mediump float;                     \n"
52       "void main()                                  \n"
53       "{                                            \n"
54       "  vec3 pos = aPosition;                      \n"
55       "  pos.y = pos.y * 3.0;                       \n"
56       "  vec4 world = uModelView * vec4(pos,1.0);   \n"
57       "  gl_Position = uProjection * world;         \n"
58       "  vTexCoord = aTexCoord;                     \n"
59       "}                                            \n" );
60
61   std::string fragmentShader(
62       "uniform  float  uDepth;                      \n"
63       "uniform  float  uAlpha;                      \n"
64       "void main()                                  \n"
65       "{                                            \n"
66       " if(vTexCoord.y < 1.0 / 3.0)                 \n"
67       " {                                           \n"
68       "   gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);  \n"
69       " }                                           \n"
70       " else if(vTexCoord.y < 2.0 / 3.0)            \n"
71       " {                                           \n"
72       "   gl_FragColor = texture2D(sTexture, vec2(vTexCoord.x, vTexCoord.y * 3.0 - 1.0)) * uColor;    \n"
73       "   gl_FragColor.a *= uAlpha;                 \n"
74       " }                                           \n"
75       " else                                        \n"
76       " {                                           \n"
77       "   float darkness = 3.0 - vTexCoord.y * 3.0;                                                   \n"
78       "   darkness = (1.0 - 1.0 / uDepth + darkness * 1.0/ uDepth) * 0.65;                            \n"
79       "   vec4 color = texture2D(sTexture, vec2(vTexCoord.x, -vTexCoord.y *3.0 + 3.0)) * uColor;      \n"
80       "   color.a *= uAlpha;                                                                          \n"
81       "   gl_FragColor = color * vec4(darkness, darkness, darkness, darkness);                        \n"
82       " }                                           \n"
83       "}                                            \n" );
84
85   // Create the implementation, temporarily owned on stack,
86   Dali::ShaderEffect shaderEffectCustom =  Dali::ShaderEffect::New(
87       vertextShader,
88       fragmentShader,
89       GeometryType( GEOMETRY_TYPE_IMAGE ),
90       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ));
91
92   /* Pass ownership to MirrorEffect through overloaded constructor, So that it now has access to the
93      Dali::ShaderEffect implementation */
94   Dali::Toolkit::MirrorEffect handle( shaderEffectCustom );
95   handle.SetUniform(ALPHA_PROPERTY_NAME, 1.0f);
96   handle.SetUniform(DEPTH_PROPERTY_NAME, 0.5f);
97   return handle;
98 }
99
100 void MirrorEffect::SetDepth( float depth )
101 {
102   SetUniform( DEPTH_PROPERTY_NAME, depth );
103 }
104
105 void MirrorEffect::SetAlpha( float alpha )
106 {
107   SetUniform( ALPHA_PROPERTY_NAME, alpha );
108 }
109
110 const std::string& MirrorEffect::GetDepthPropertyName() const
111 {
112   return DEPTH_PROPERTY_NAME;
113 }
114
115 const std::string& MirrorEffect::GetAlphaPropertyName() const
116 {
117   return ALPHA_PROPERTY_NAME;
118 }
119
120 } // namespace Toolkit
121
122 } // namespace Dali