Merge branch 'devel/master' into devel/new_mesh
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-book-spine-effect.h
1 #ifndef __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__
2 #define __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 /**
34  * @brief Creates a new PageTurnBookSpineEffect
35  * This is an assisting effect of PageTurnEffect to display a book spine on _static_ pages, and also to flip the image horizontally when needed.
36  *
37  * When the page is turned over in landscape, call
38  * SetIsBackImageVisible(true), this effect can display the back image
39  * correctly after the imageActor been rotated 180 degrees.  To
40  * display the pages visually consistent with its turning state,
41  * please set the uniforms with the same values as the PageTurnEffect.
42  *
43  * Animatable/Constrainable uniforms:
44  *  "uShadowWidth"          - The width of shadow to be pageSize * shadowWidth. This shadow appears at the edges of the actor
45  *                            which is not visible on static pages
46  *  "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
47  *                            quarter of this ellipse is used to calculate spine normal for simulating shadow
48  *  "uIsBackImageVisible"   - Set whether the current page is with its backside visible. Need to pass the parameter as true for
49  *                            the page which is turned over but still visible in Landscape
50  *  "uPageWidth"            - The page width of the PageTurnBookSpineEffect
51  *
52  * @return A handle to a newly allocated ShaderEffect
53  **/
54 inline ShaderEffect CreatePageTurnBookSpineEffect()
55 {
56   std::string vertexSource(
57       "precision mediump float;\n"
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       "precision mediump float;\n"
69       "uniform float uIsBackImageVisible;\n"
70       "uniform float uPageWidth;\n"
71       "uniform vec2 uSpineShadowParameter;\n"
72       "  void main()\n"
73       "  {\n"
74       // leave the border for display shadow, not visible( out of the screen ) when the page is static
75       "    if( vTexCoord.y > sTextureRect.q || vTexCoord.y < sTextureRect.t || vTexCoord.x > sTextureRect.p  )\n"
76       "    {\n"
77       "      gl_FragColor = vec4( 0.0 );\n"
78       "    }\n"
79       "    else \n"
80       "    { \n"
81       // flip the image horizontally by changing the x component of the texture coordinate
82       "      if( uIsBackImageVisible == 1.0 )  gl_FragColor = texture2D( sTexture, vec2( sTextureRect.p+sTextureRect.s-vTexCoord.x, vTexCoord.y ) ) * uColor; \n"
83       "      else gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
84       "      \n"
85       // display book spine, a stripe of shadowed texture
86       "      float pixelPos = (vTexCoord.x-sTextureRect.s)*uPageWidth; \n"
87       "      if(pixelPos < uSpineShadowParameter.x) \n"
88       "      {\n"
89       "        float x = pixelPos - uSpineShadowParameter.x;\n"
90       "        float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n"
91       "        vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n"
92       "        gl_FragColor.rgb *= spineNormal.y; \n"
93       "      }"
94       "    }\n"
95       "  }" );
96
97   const float DEFAULT_SHADOW_WIDTH(0.15f);
98   const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
99
100   ShaderEffect shaderEffect = ShaderEffect::New( vertexSource, fragmentSource );
101
102   shaderEffect.SetUniform( "uIsBackImageVisible", -1.f );
103   shaderEffect.SetUniform( "uShadowWidth", DEFAULT_SHADOW_WIDTH );
104   shaderEffect.SetUniform( "uSpineShadowParameter", DEFAULT_SPINE_SHADOW_PARAMETER );
105
106   float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
107   shaderEffect.SetUniform( "uPageWidth", defaultPageWidth/(1.f-DEFAULT_SHADOW_WIDTH) );
108
109   return shaderEffect;
110 }
111
112 } //namespace Internal
113
114 } // namespace Toolkit
115
116 } // namespace Dali
117
118 #endif /* __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__ */