[dali_1.1.32] Merge branch '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 #define DALI_COMPOSE_SHADER(STR) #STR
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 namespace Internal
30 {
31
32 /**
33  * @brief Creates a new PageTurnBookSpineEffect
34  * This is an assisting effect of PageTurnEffect to display a book spine on _static_ pages, and also to flip the image horizontally when needed.
35  *
36  * When the page is turned over in landscape, call
37  * SetIsBackImageVisible(true), this effect can display the back image
38  * correctly after the page been rotated 180 degrees.  To
39  * display the pages visually consistent with its turning state,
40  * please set the uniforms with the same values as the PageTurnEffect.
41  *
42  * Animatable/Constrainable uniforms:
43  *  "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
44  *                            quarter of this ellipse is used to calculate spine normal for simulating shadow *
45  *  "uTextureWidth" - 1.0 for single sided page,
46  *                    2.0 for double sided image which has left half part as page front side and right half part as page back side.
47  *
48  * @return The newly created Property::Map with the page turn book spine effect
49  **/
50 inline Property::Map CreatePageTurnBookSpineEffect()
51 {
52   const char* vertexSource = DALI_COMPOSE_SHADER(
53       precision mediump float;\n
54       attribute mediump vec2 aPosition;\n
55       uniform mediump mat4 uMvpMatrix;\n
56       uniform vec3 uSize;\n
57       uniform float uTextureWidth;\n
58       varying vec2 vTexCoord;\n
59       void main()\n
60       {\n
61         mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
62         gl_Position = uMvpMatrix * vertexPosition;\n
63         vTexCoord = aPosition + vec2(0.5);\n
64         vTexCoord.x /= uTextureWidth;
65       }\n);
66
67   // the simplified version of the fragment shader of page turn effect
68   const char* fragmentSource = DALI_COMPOSE_SHADER(
69       precision mediump float;\n
70       varying mediump vec2 vTexCoord;\n
71       uniform vec3 uSize;\n
72       uniform vec2 uSpineShadowParameter;\n
73       uniform sampler2D sTexture;\n
74       uniform lowp vec4 uColor;\n
75
76       void main()\n
77       {\n
78         if( gl_FrontFacing )\n // display front side
79         {\n
80           gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
81         }\n
82         else\n // display back side, flip the image horizontally by changing the x component of the texture coordinate
83         {\n
84           gl_FragColor = texture2D( sTexture, vec2( 1.0 - vTexCoord.x, vTexCoord.y ) ) * uColor;\n
85         }\n
86         // display book spine, a stripe of shadowed texture
87         float pixelPos = vTexCoord.x * uSize.x;\n
88         if( pixelPos < uSpineShadowParameter.x )\n
89         {\n
90           float x = pixelPos - uSpineShadowParameter.x;\n
91           float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
92           vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n
93           gl_FragColor.rgb *= spineNormal.y; \n
94         }\n
95       } );
96
97   Property::Map map;
98
99   Property::Map customShader;
100
101   customShader[ "vertexShader" ] = vertexSource;
102   customShader[ "fragmentShader" ] = fragmentSource;
103
104   map[ "shader" ] = customShader;
105   return map;
106 }
107
108 } //namespace Internal
109
110 } // namespace Toolkit
111
112 } // namespace Dali
113
114 #endif /* __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__ */