Merge "Update the custom shader effects to resize the geometry" into devel/master
[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  *  "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
45  *                            quarter of this ellipse is used to calculate spine normal for simulating shadow
46  *  "uIsBackImageVisible"   - Set whether the current page is with its backside visible. Need to pass the parameter as true for
47  *                            the page which is turned over but still visible in Landscape
48  *  "uPageWidth"            - The page width of the PageTurnBookSpineEffect
49  *
50  * @return A handle to a newly allocated ShaderEffect
51  **/
52 inline ShaderEffect CreatePageTurnBookSpineEffect()
53 {
54   // the simplified version of the fragment shader of page turn effect
55   std::string fragmentSource = DALI_COMPOSE_SHADER(
56       precision mediump float;\n
57       uniform float uIsBackImageVisible;\n
58       uniform float uPageWidth;\n
59       uniform vec2 uSpineShadowParameter;\n
60       void main()\n
61       {\n
62       // flip the image horizontally by changing the x component of the texture coordinate
63         if( uIsBackImageVisible == 1.0 )\n
64           gl_FragColor = texture2D( sTexture, vec2( uTextureRect.p+uTextureRect.s-vTexCoord.x, vTexCoord.y ) ) * uColor; \n
65         else\n
66         gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
67       // display book spine, a stripe of shadowed texture
68         float pixelPos = (vTexCoord.x-uTextureRect.s)*uPageWidth; \n
69         if(pixelPos < uSpineShadowParameter.x) \n
70         {\n
71           float x = pixelPos - uSpineShadowParameter.x;\n
72           float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
73           vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n
74           gl_FragColor.rgb *= spineNormal.y; \n
75         }
76       } );
77
78   const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
79
80   ShaderEffect shaderEffect = ShaderEffect::New( "", fragmentSource );
81
82   shaderEffect.SetUniform( "uIsBackImageVisible", -1.f );
83   shaderEffect.SetUniform( "uSpineShadowParameter", DEFAULT_SPINE_SHADOW_PARAMETER );
84
85   float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
86   shaderEffect.SetUniform( "uPageWidth", defaultPageWidth );
87
88   return shaderEffect;
89 }
90
91 } //namespace Internal
92
93 } // namespace Toolkit
94
95 } // namespace Dali
96
97 #endif /* __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__ */