Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / page-turn-book-spine-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/page-turn-book-spine-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string SHADOW_WIDTH_PROPERTY_NAME("uShadowWidth");
29 const std::string SPINE_SHADOW_PARAMETER_PROPERTY_NAME("uSpineShadowParameter");
30 const std::string IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME( "uIsBackImageVisible" );
31 const std::string PAGE_WIDTH_PROPERTY_NAME( "uPageWidth" );
32
33 // fake shadow is used to enhance the effect, with its default maximum width to be pageSize * 0.15
34 const float DEFAULT_SHADOW_WIDTH(0.15f);
35
36 // the major&minor radius (in pixels) to form an ellipse shape
37 // the top-left quarter of this ellipse is used to calculate spine normal for simulating shadow
38 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
39
40 }
41
42 PageTurnBookSpineEffect::PageTurnBookSpineEffect()
43 {
44 }
45
46 PageTurnBookSpineEffect::PageTurnBookSpineEffect( ShaderEffect handle )
47 : ShaderEffect( handle )
48 {
49 }
50
51 PageTurnBookSpineEffect::~PageTurnBookSpineEffect()
52 {
53 }
54
55 PageTurnBookSpineEffect PageTurnBookSpineEffect::New()
56 {
57   std::string vertexSource(
58   "uniform float uShadowWidth;\n"
59   "  void main()\n"
60   "  {\n"
61   "    gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
62   "    vTexCoord.x = (aTexCoord.x-sTextureRect.s) /( 1.0 - uShadowWidth ) + sTextureRect.s;\n"
63   "    vTexCoord.y = ( aTexCoord.y-sTextureRect.t-0.5*uShadowWidth*(sTextureRect.q-sTextureRect.t) )/( 1.0 - uShadowWidth ) + sTextureRect.t;\n"
64   "  }");
65
66   // the simplified version of the fragment shader of page turn effect
67   std::string fragmentSource(
68   "uniform float uIsBackImageVisible;\n"
69   "uniform float uPageWidth;\n"
70   "uniform vec2 uSpineShadowParameter;\n"
71   "  void main()\n"
72   "  {\n"
73       // leave the border for display shadow, not visible( out of the screen ) when the page is static
74   "    if( vTexCoord.y > sTextureRect.q || vTexCoord.y < sTextureRect.t || vTexCoord.x > sTextureRect.p  )\n"
75   "    {\n"
76   "      gl_FragColor = vec4( 0.0 );\n"
77   "    }\n"
78   "    else \n"
79   "    { \n"
80          // flip the image horizontally by changing the x component of the texture coordinate
81   "      if( uIsBackImageVisible == 1.0 )  gl_FragColor = texture2D( sTexture, vec2( sTextureRect.p+sTextureRect.s-vTexCoord.x, vTexCoord.y ) ) * uColor; \n"
82   "      else gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
83   "      \n"
84          // display book spine, a stripe of shadowed texture
85   "      float pixelPos = (vTexCoord.x-sTextureRect.s)*uPageWidth; \n"
86   "      if(pixelPos < uSpineShadowParameter.x) \n"
87   "      {\n"
88   "        float x = pixelPos - uSpineShadowParameter.x;\n"
89   "        float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n"
90   "        vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n"
91   "        gl_FragColor.rgb *= spineNormal.y; \n"
92   "      }"
93   "    }\n"
94   "  }" );
95
96   ShaderEffect shader;
97   shader = ShaderEffect::New( vertexSource,fragmentSource );
98   PageTurnBookSpineEffect handle( shader );
99   handle.SetUniform( IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME, -1.f );
100   handle.SetUniform( SHADOW_WIDTH_PROPERTY_NAME, DEFAULT_SHADOW_WIDTH );
101   handle.SetUniform( SPINE_SHADOW_PARAMETER_PROPERTY_NAME, DEFAULT_SPINE_SHADOW_PARAMETER );
102   float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
103   handle.SetUniform( PAGE_WIDTH_PROPERTY_NAME, defaultPageWidth/(1.f-DEFAULT_SHADOW_WIDTH) );
104   return handle;
105 }
106
107 void PageTurnBookSpineEffect::SetIsBackImageVisible( bool isBackVisible )
108 {
109   float direction = isBackVisible ? 1.0f : -1.0f;
110   SetUniform( IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME, direction );
111 }
112
113 void PageTurnBookSpineEffect::SetPageWidth( float pageWidth )
114 {
115   SetUniform( PAGE_WIDTH_PROPERTY_NAME, pageWidth );
116 }
117
118 void PageTurnBookSpineEffect::SetShadowWidth( float shadowWidth )
119 {
120   SetUniform( SHADOW_WIDTH_PROPERTY_NAME, shadowWidth );
121 }
122
123 void PageTurnBookSpineEffect::SetSpineShadowParameter( const Vector2& spineShadowParameter )
124 {
125   SetUniform( SPINE_SHADOW_PARAMETER_PROPERTY_NAME, spineShadowParameter);
126 }
127
128 } // namespace Toolkit
129
130 } // namespace Dali