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