Stop using ImageActor in PageTurnView
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-effect.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 //EXTERNAL INCLUDES
19 #include <string.h>
20 #include <dali/public-api/animation/constraint.h>
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/object/property-map.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-effect.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 namespace
31 {
32 #define DALI_COMPOSE_SHADER(STR) #STR
33 const char * const PROPERTY_COMMON_PARAMETERS( "uCommonParameters" );
34 const char * const PROPERTY_ORIGINAL_CENTER( "originalCenter" );
35 const char * const PROPERTY_CURRENT_CENTER( "currentCenter" );
36 }
37
38 /**
39  * This constraint updates the common parameter values used by every vertex.
40  * By using constraint, they are calculate once in CPU then pass into the vertex shader as uniforms
41  */
42 struct CommonParametersConstraint
43 {
44   CommonParametersConstraint( float pageHeight )
45   : mPageHeight( pageHeight )
46   {}
47
48   void operator()( Dali::Matrix& current, const PropertyInputContainer& inputs )
49   {
50     const Vector2& originalCenter = inputs[0]->GetVector2();
51     Vector2 currentCenter = inputs[1]->GetVector2();
52
53     // calculate the curve direction and the vanishing point
54     // here, the vanishing point is the intersection of spine with the line passing through original center and vertical to curve direction
55     Vector2 curveDirection( currentCenter - originalCenter );
56     curveDirection.Normalize();
57     if( fabs(curveDirection.y) < 0.01f) // eliminate the possibility of division by zero in the next step
58     {
59       curveDirection.y = 0.01f;
60     }
61     float vanishingPointY = originalCenter.y + curveDirection.x * originalCenter.x / curveDirection.y;
62
63     float curveEndY, cosTheta ,sinTheta ,translateX, translateY;
64     // when the vanishing point is very far away, make it infinitely, in this case, the page bent horizontally
65     const float THRESHOLD(20.0);
66     if( fabs(vanishingPointY-mPageHeight*0.5f) >= mPageHeight*THRESHOLD )
67     {
68       curveDirection = Vector2(-1.f,0.f);
69       currentCenter.y = originalCenter.y;
70
71       curveEndY = originalCenter.y;
72       cosTheta = 1.f;
73       sinTheta = 0.f;
74       translateX = currentCenter.x - originalCenter.x;
75       translateY = vanishingPointY;
76     }
77     else
78     {
79       curveEndY = currentCenter.y - curveDirection.y * (currentCenter.x/curveDirection.x) ;
80       Vector2 v1( currentCenter.x, currentCenter.y - vanishingPointY );
81       v1.Normalize();
82       Vector2 v2( originalCenter.x, originalCenter.y - vanishingPointY );
83       v2.Normalize();
84       cosTheta = v1.x*v2.x + v1.y*v2.y;
85       sinTheta = ( vanishingPointY > mPageHeight*0.5f ) ? sqrt(1.0-cosTheta*cosTheta) : -sqrt(1.0-cosTheta*cosTheta);
86       translateX = currentCenter.x - cosTheta*originalCenter.x - sinTheta*( originalCenter.y-vanishingPointY );
87       translateY = currentCenter.y + sinTheta*originalCenter.x - cosTheta*( originalCenter.y-vanishingPointY );
88     }
89
90     float originalLength = fabs(originalCenter.x/curveDirection.x);
91     float currentLength = fabs(currentCenter.x/curveDirection.x);
92     float curveHeight = 0.45f*sqrt(originalLength*originalLength - currentLength*currentLength);
93
94     float* parameterArray = current.AsFloat();
95     parameterArray[0] = cosTheta;
96     parameterArray[1] = -sinTheta;
97     parameterArray[2] = originalCenter.x;
98     parameterArray[3] = originalCenter.y;
99     parameterArray[4] = sinTheta;
100     parameterArray[5] = cosTheta;
101     parameterArray[6] = currentCenter.x;
102     parameterArray[7] = currentCenter.y;
103     parameterArray[8] = translateX;
104     parameterArray[9] = translateY;
105     parameterArray[10] = vanishingPointY;
106     parameterArray[11] = curveEndY;
107     parameterArray[12] = curveDirection.x;
108     parameterArray[13] = curveDirection.y;
109     parameterArray[14] = curveHeight;
110     parameterArray[15] = currentLength;
111   }
112
113   float mPageHeight;
114 };
115
116 void Dali::Toolkit::Internal::PageTurnApplyInternalConstraint( Actor& actor, float pageHeight )
117 {
118   Constraint constraint = Constraint::New<Dali::Matrix>( actor, actor.GetPropertyIndex( PROPERTY_COMMON_PARAMETERS ) , CommonParametersConstraint( pageHeight ) );
119   constraint.AddSource( LocalSource( actor.GetPropertyIndex( PROPERTY_ORIGINAL_CENTER ) ) );
120   constraint.AddSource( LocalSource( actor.GetPropertyIndex( PROPERTY_CURRENT_CENTER ) ) );
121   constraint.Apply();
122 }
123
124 Property::Map Dali::Toolkit::Internal::CreatePageTurnEffect()
125 {
126   const char* vertexShader = DALI_COMPOSE_SHADER(
127       /*
128        * The common parameters for all the vertices, calculate in CPU then pass into the shader as uniforms
129        *
130        *  first part of the page, (outside the the line passing through original center and vertical to curve direction)
131        * no Z change, only 2D rotation and translation
132        * ([0][0],[0][1],[1][0],[1][1]) mat2 rotateMatrix
133        * ([2][0],[2][1]) vec2 translationVector
134        *
135        * ([0][2],[0][3]) vec2 originalCenter: Typically the press down position of the Pan Gesture
136        * ([1][2],[1][3]) vec2 currentCenter: Typically the current position of the Pan Gesture
137        * ([3][0],[3][1]) vec2 curveDirection: The normalized vector pointing from original center to current center
138        * ([2][2]) float vanishingPointY: The Y coordinate of the intersection of the spine
139        *                                 and the line which goes through the original center and is vertical to the curveDirection
140        * ([2][3]) float curveEndY: The Y coordinate of intersection of the spine and the line through both original and current center
141        * ([3][2]) float curveHeight: The height of the interpolated hermite curve.
142        * ([3][3]) float currentLength: The length from the current center to the curveEnd.
143        */
144       precision mediump float;\n
145       \n
146       attribute mediump vec2 aPosition;\n
147       \n
148       uniform mediump mat4 uMvpMatrix;\n
149       uniform mediump mat3 uNormalMatrix;\n
150       uniform mediump mat4 uModelView;\n
151       \n
152       uniform mat4 uCommonParameters;\n
153       \n
154       uniform vec3 uSize;\n
155       uniform float uIsTurningBack;\n
156       uniform float uTextureWidth;\n
157       varying vec3 vNormal;\n
158       varying vec4 vPosition;\n
159       varying mediump vec2 vTexCoord;\n
160       \n
161       void main()\n
162       {\n
163         vec4 position = vec4( aPosition*uSize.xy, 0.0, 1.0);\n
164         vec2 currentCenter = vec2( uCommonParameters[1][2], uCommonParameters[1][3]);\n
165         vec2 originalCenter = vec2( uCommonParameters[0][2], uCommonParameters[0][3]);\n
166         vec3 normal = vec3(0.0,0.0,1.0);\n
167         \n
168         if(currentCenter.x < originalCenter.x)\n
169         {\n
170           // change the coordinate origin from the center of the page to its top-left
171           position.xy += uSize.xy * 0.5;\n
172           vec2 curveDirection = vec2( uCommonParameters[3]);\n
173           vec3 vanishingPoint = vec3(0.0, uCommonParameters[2][2], 0.0);\n
174           // first part of the page, (outside the the line passing through original center and vertical to curve direction)
175           //no Z change, only 2D rotation and translation
176           if( dot(curveDirection, position.xy - originalCenter) < 0.0 )
177           {\n
178             position.y -= vanishingPoint.y;\n
179             position.xy = mat2(uCommonParameters)*position.xy + vec2( uCommonParameters[2]);\n
180           }\n
181           // second part of the page, bent as a ruled surface
182           else\n
183           {\n
184             // calculate on the flat plane, between
185             // the first line passing through current vertex and vanishing point
186             // the second line passing through original center and current center
187             vec2 curveEnd = vec2( 0.0, uCommonParameters[2][3] );\n
188             vec2 curFlatDirection = vec2(0.0,1.0);\n
189             float lengthFromCurve = position.y - originalCenter.y;\n
190             float lengthOnCurve = position.x;\n
191             if(currentCenter.y != originalCenter.y)\n
192             {\n
193               curFlatDirection = normalize(position.xy - vanishingPoint.xy);\n
194               lengthFromCurve = (curveEnd.x*curveDirection.y-curveEnd.y*curveDirection.x-position.x*curveDirection.y+position.y*curveDirection.x)
195               / (curFlatDirection.x*curveDirection.y-curFlatDirection.y*curveDirection.x);\n
196               lengthOnCurve = length(position.xy+lengthFromCurve*curFlatDirection-curveEnd);\n
197             }\n
198             \n
199             // define the control points of hermite curve, composed with two segments
200             // calculation is carried out on the 2D plane which is passing through both current and original center and vertical to the image plane
201             float currentLength = uCommonParameters[3][3];\n
202             float originalLength =  abs(originalCenter.x/curveDirection.x);\n
203             float height = uCommonParameters[3][2];\n
204             float percentage = currentLength/originalLength;\n
205             //vec2 SegmentOneControlPoint0 = vec2(0.0, 0.0);
206             vec2 SegmentOneControlPoint1 = vec2((0.65*percentage - 0.15)*originalLength, (0.8 + 0.2 * percentage)*height); \n
207             vec2 SegmentTwoControlPoint0 = SegmentOneControlPoint1;\n
208             vec2 SegmentTwoControlPoint1 = vec2(currentLength, 0.0); \n
209             vec2 SegmentOneTangentVector0 = SegmentOneControlPoint1;\n
210             vec2 SegmentOneTangentVector1 = vec2(0.5*originalLength,0.0);\n
211             vec2 SegmentTwoTangentVector0 = SegmentOneTangentVector1;\n
212             vec2 SegmentTwoTangentVector1 = SegmentOneTangentVector1;\n
213             \n
214             // calculate the corresponding curve point position and its tangent vector
215             // it is a linear mapping onto nonlinear curves, might cause some unwanted deformation
216             // but as there are no analytical method to calculate the curve length on arbitrary segment
217             // no efficient way to solve this nonlinear mapping, Numerical approximation would cost too much computation in shader
218             vec2 curvePoint2D;\n
219             vec2 tangent;\n
220             float t0 = lengthOnCurve / originalLength;\n
221             if(t0<=0.5)\n
222             {\n
223               float t = 2.0*t0;\n
224               float t_2 = t*t;\n
225               float t_3 = t*t_2;\n
226               curvePoint2D = (-2.0*t_3+3.0*t_2)*SegmentOneControlPoint1
227               + (t_3-2.0*t_2+t)*SegmentOneTangentVector0 + (t_3-t_2)*SegmentOneTangentVector1;\n
228               tangent = (-6.0*t_2+6.0*t)*SegmentOneControlPoint1
229               + (3.0*t_2-4.0*t+1.0)*SegmentOneTangentVector0 + (3.0*t_2-2.0*t)*SegmentOneTangentVector1;\n
230             }\n
231             else\n
232             {\n
233               float t = 2.0*t0-1.0;\n
234               float t_2 = t*t;\n
235               float t_3 = t*t_2;\n
236               curvePoint2D = (2.0*t_3-3.0*t_2+1.0)*SegmentTwoControlPoint0 + (-2.0*t_3+3.0*t_2)*SegmentTwoControlPoint1
237               + (t_3-2.0*t_2+t)*SegmentTwoTangentVector0 + (t_3-t_2)*SegmentTwoTangentVector1;\n
238               tangent = (6.0*t_2-6.0*t)*SegmentTwoControlPoint0 + (-6.0*t_2+6.0*t)*SegmentTwoControlPoint1
239               + (3.0*t_2-4.0*t+1.0)*SegmentTwoTangentVector0 + (3.0*t_2-2.0*t)*SegmentTwoTangentVector1;\n
240               // a trick to eliminate some optical illusion caused by the gradient matter of normal in per-fragment shading
241               // which is caused by linear interpolation of normal vs. nonlinear lighting
242               // will notice some artifact in the areas with dramatically normal changes, so compress the normal differences here
243               tangent.y *=  min(1.0, length(position.xyz - vanishingPoint) / uSize.y ); \n
244             }\n
245             vec3 curvePoint = vec3(curveEnd - curvePoint2D.x*curveDirection,max(0.0,curvePoint2D.y));\n
246             vec3 tangentVector = vec3(-tangent.x*curveDirection,tangent.y);\n
247             \n
248             // locate the new vertex position on the line passing through both vanishing point and the calculated curve point position
249             vec3 curLiftDirection = vec3(0.0,-1.0,0.0);\n
250             if(currentCenter.y != originalCenter.y)\n
251             {\n
252               curLiftDirection = normalize(curvePoint - vanishingPoint);\n
253               tangentVector *= (curveDirection.y > 0.0) ? -1.0 : 1.0;\n
254               // an heuristic adjustment here, to compensate the linear parameter mapping onto the nonlinear curve
255               float Y0 = position.y - curveDirection.y * (position.x/curveDirection.x); \n
256               float proportion;
257               float refLength;\n
258               if(abs(Y0-vanishingPoint.y) > abs(curveEnd.y-vanishingPoint.y)) \n
259               {\n
260                 proportion = abs(curveEnd.y - Y0) / (abs(curveEnd.y-Y0)+abs(curveEnd.y - vanishingPoint.y)); \n
261                 refLength = proportion*length(originalCenter-vanishingPoint.xy) / (proportion-1.0); \n
262               }\n
263               else\n
264               {\n
265                 proportion = abs(curveEnd.y - Y0) / abs(curveEnd.y - vanishingPoint.y);\n
266                 refLength = proportion*length(originalCenter-vanishingPoint.xy); \n
267               }\n
268               float Y1 = currentCenter.y - (normalize(currentCenter-vanishingPoint.xy)).y * refLength; \n
269               position.y = mix(Y0, Y1, t0); \n
270             }\n
271             position.xz = curvePoint.xz - lengthFromCurve*curLiftDirection.xz;\n
272             // calculate the normal vector, will be used for lighting
273             normal = cross(curLiftDirection, normalize(tangentVector));\n
274             // the signature of Z is decided by the page turning direction:
275             // from left to right(negative); from right to left (positive)
276             position.z *= -uIsTurningBack;\n
277             normal.xy *= -uIsTurningBack;\n
278           }\n
279           // change the coordinate origin from the top-left of the page to its center
280           position.xy -= uSize.xy * 0.5; \n
281         }\n
282         vNormal =  uNormalMatrix * normal;\n
283         gl_Position = uMvpMatrix * position;
284         // varying parameters for fragment shader
285         vTexCoord = aPosition + vec2(0.5);\n
286         vTexCoord.x /= uTextureWidth;
287         vPosition = uModelView * position;\n
288       }\n
289   );
290
291   const char* fragmentShader = DALI_COMPOSE_SHADER(
292       precision mediump float;\n
293       \n
294       varying mediump vec2 vTexCoord;\n
295       \n
296       uniform sampler2D sTexture;\n
297       uniform lowp vec4 uColor;\n
298       uniform vec3 uSize;\n
299       uniform vec2 uSpineShadowParameter;\n
300       varying vec3 vNormal;\n
301       varying vec4 vPosition;\n
302       \n
303       void main()\n
304       {\n
305         // need to re-normalize the interpolated normal
306         vec3 normal = normalize( vNormal );\n
307         // display page content
308         vec4 texel;
309         // display back image of the page, flip the texture
310         if(  dot(vPosition.xyz, normal) > 0.0 ) texel = texture2D( sTexture, vec2( 1.0 - vTexCoord.x, vTexCoord.y ) );\n
311         // display front image of the page
312         else texel = texture2D( sTexture, vTexCoord );\n
313
314         // display book spine, a stripe of shadowed texture
315         float pixelPos = vTexCoord.x * uSize.x; \n
316         float spineShadowCoef = 1.0; \n
317         if( pixelPos < uSpineShadowParameter.x ) \n
318         {\n
319           float x = pixelPos - uSpineShadowParameter.x;\n
320           float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
321           spineShadowCoef = normalize( vec2( uSpineShadowParameter.y*x/uSpineShadowParameter.x, y ) ).y;\n
322         }\n
323         // calculate the lighting
324         // set the ambient color as vec3(0.4);
325         float lightColor = abs( normal.z ) * 0.6 + 0.4;\n
326         gl_FragColor = vec4( ( spineShadowCoef * lightColor ) * texel.rgb , texel.a ) * uColor;\n
327       }
328   );
329
330   Property::Map map;
331
332   Property::Map customShader;
333
334   customShader[ "vertexShader" ] = vertexShader;
335   customShader[ "fragmentShader" ] = fragmentShader;
336   customShader[ "subdivideGridX" ] = 20;
337   customShader[ "subdivideGridY" ] = 20;
338
339   map[ "shader" ] = customShader;
340   return map;
341
342 }
343