Merge "Added more test cases for ScrollView" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / mirror-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/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 vertexShader(
52       "void main()                                  \n"
53       "{                                            \n"
54       "  mediump vec3 pos = aPosition;              \n"
55       "  pos.y = pos.y * 3.0;                       \n"
56       "  mediump 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  mediump float  uDepth;              \n"
63       "uniform  mediump 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       "   highp float darkness = 3.0 - vTexCoord.y * 3.0;                                                   \n"
78       "   darkness = (1.0 - 1.0 / uDepth + darkness * 1.0/ uDepth) * 0.65;                            \n"
79       "   highp 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       vertexShader,
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