2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/public-api/shader-effects/page-turn-book-spine-effect.h>
29 const std::string SHADOW_WIDTH_PROPERTY_NAME("uShadowWidth");
30 const std::string SPINE_SHADOW_PARAMETER_PROPERTY_NAME("uSpineShadowParameter");
31 const std::string IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME( "uIsBackImageVisible" );
32 const std::string PAGE_WIDTH_PROPERTY_NAME( "uPageWidth" );
34 // fake shadow is used to enhance the effect, with its default maximum width to be pageSize * 0.15
35 const float DEFAULT_SHADOW_WIDTH(0.15f);
37 // the major&minor radius (in pixels) to form an ellipse shape
38 // the top-left quarter of this ellipse is used to calculate spine normal for simulating shadow
39 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
43 PageTurnBookSpineEffect::PageTurnBookSpineEffect()
47 PageTurnBookSpineEffect::PageTurnBookSpineEffect( ShaderEffect handle )
48 : ShaderEffect( handle )
52 PageTurnBookSpineEffect::~PageTurnBookSpineEffect()
56 PageTurnBookSpineEffect PageTurnBookSpineEffect::New()
58 std::string vertexSource(
59 "uniform float uShadowWidth;\n"
62 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
63 " vTexCoord.x = (aTexCoord.x-sTextureRect.s) /( 1.0 - uShadowWidth ) + sTextureRect.s;\n"
64 " vTexCoord.y = ( aTexCoord.y-sTextureRect.t-0.5*uShadowWidth*(sTextureRect.q-sTextureRect.t) )/( 1.0 - uShadowWidth ) + sTextureRect.t;\n"
67 // the simplified version of the fragment shader of page turn effect
68 std::string fragmentSource(
69 "uniform float uIsBackImageVisible;\n"
70 "uniform float uPageWidth;\n"
71 "uniform vec2 uSpineShadowParameter;\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"
77 " gl_FragColor = vec4( 0.0 );\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"
85 // display book spine, a stripe of shadowed texture
86 " float pixelPos = (vTexCoord.x-sTextureRect.s)*uPageWidth; \n"
87 " if(pixelPos < uSpineShadowParameter.x) \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"
98 shader = ShaderEffect::New( vertexSource,fragmentSource );
99 PageTurnBookSpineEffect handle( shader );
100 handle.SetUniform( IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME, -1.f );
101 handle.SetUniform( SHADOW_WIDTH_PROPERTY_NAME, DEFAULT_SHADOW_WIDTH );
102 handle.SetUniform( SPINE_SHADOW_PARAMETER_PROPERTY_NAME, DEFAULT_SPINE_SHADOW_PARAMETER );
103 float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
104 handle.SetUniform( PAGE_WIDTH_PROPERTY_NAME, defaultPageWidth/(1.f-DEFAULT_SHADOW_WIDTH) );
108 void PageTurnBookSpineEffect::SetIsBackImageVisible( bool isBackVisible )
110 float direction = isBackVisible ? 1.0f : -1.0f;
111 SetUniform( IS_BACK_IMAGE_VISIBLE_PROPERTY_NAME, direction );
114 void PageTurnBookSpineEffect::SetPageWidth( float pageWidth )
116 SetUniform( PAGE_WIDTH_PROPERTY_NAME, pageWidth );
119 void PageTurnBookSpineEffect::SetShadowWidth( float shadowWidth )
121 SetUniform( SHADOW_WIDTH_PROPERTY_NAME, shadowWidth );
124 void PageTurnBookSpineEffect::SetSpineShadowParameter( const Vector2& spineShadowParameter )
126 SetUniform( SPINE_SHADOW_PARAMETER_PROPERTY_NAME, spineShadowParameter);
129 } // namespace Toolkit