Project

General

Profile

Download (18 KB) Statistics
| Branch: | Revision:

library / src / main / res / raw / main_vertex_shader.glsl @ 6a06a912

1
uniform vec3 u_bmpD;            // half of object width x half of object height X half the depth; 
2
                                // point (0,0,0) is the center of the object
3

    
4
uniform float u_Depth;          // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes.
5
                                // I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n, .far = f, .diff = f-n so maybe u_Depth is redundant
6
                                // Update: this struct is only available in fragment shaders
7
                                
8
uniform mat4 u_MVPMatrix;		// A constant representing the combined model/view/projection matrix.      		       
9
uniform mat4 u_MVMatrix;		// A constant representing the combined model/view matrix.       		
10
		 
11
attribute vec3 a_Position;		// Per-vertex position information we will pass in.   				
12
attribute vec4 a_Color;			// Per-vertex color information we will pass in. 				
13
attribute vec3 a_Normal;		// Per-vertex normal information we will pass in.      
14
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. 		
15
		  
16
varying vec3 v_Position;		//      		
17
varying vec4 v_Color;			// Those will be passed into the fragment shader.          		
18
varying vec3 v_Normal;			//  
19
varying vec2 v_TexCoordinate;   //  		
20

    
21
uniform int vNumEffects;                  // total number of vertex effects
22

    
23
#if NUM_VERTEX>0
24
uniform int vType[NUM_VERTEX];            // their types.
25
uniform vec3 vUniforms[3*NUM_VERTEX];     // i-th effect is 3 consecutive vec3's: [3*i], [3*i+1], [3*i+2]. first 3 float are the Interpolated values,
26
                                          // next 4 are the Region, next 2 are the Point. 
27
#endif
28

    
29
#if NUM_VERTEX>0
30
//////////////////////////////////////////////////////////////////////////////////////////////
31
// Deform the whole shape of the bitmap by force V 
32
// 
33
// If the point of application (Sx,Sy) is on the edge of the bitmap, then:
34
// a) ignore Vz
35
// b) change shape of the whole bitmap in the following way:
36
//    Suppose the upper-left corner of the bitmap rectangle is point L, upper-right - R, force vector V is applied to point M on the upper edge,
37
//    length of the bitmap = w, height = h, |LM| = Wl, |MR| = Wr, force vector V=(Vx,Vy). Also let H = h/(h+Vy)
38
//
39
//    Let now L' and R' be points such that vec(LL') = Wr/w * vec(V) and vec(RR') = Wl/w * vec(V)
40
//    now let Vl be a point on the line segment L --> M+vec(V) such that Vl(y) = L'(y)
41
//    and let Vr be a point on the line segment R --> M+vec(V) such that Vr(y) = R'(y)
42
//    
43
//    Now define points Fl and Fr, the points L and R will be moved to under force V, with Fl(y)=L'(y) and Fr(y)=R'(y) and |VrFr|/|VrR'| = |VlFl|/|VlL'| = H
44
//    Now notice that |VrR'| = |VlL'| = Wl*Wr / w   ( a little geometric puzzle! )
45
//
46
//    Then points L,R under force V move by vectors vec(Fl), vec(Fr) where
47
//    vec(Fl) = (Wr/w) * [ (Vx+Wl)-Wl*H, Vy ] = (Wr/w) * [ Wl*Vy / (h+Vy) + Vx, Vy ]
48
//    vec(Fr) = (Wl/w) * [ (Vx-Wr)+Wr*H, Vy ] = (Wl/w) * [-Wr*Vy / (h+Vy) + Vx, Vy ]
49
//
50
//    Lets now denote M+vec(v) = M'. The line segment LMR gets distorted to the curve Fl-M'-Fr. Let's now arbitrarilly decide that:
51
//    a) at point Fl the curve has to be parallel to line LM'
52
//    b) at point M' - to line LR
53
//    c) at point Fr - to line M'R
54
//
55
//    Now if Fl=(flx,fly) , M'=(mx,my) , Fr=(frx,fry); direction vector at Fl is (vx,vy) and at M' is (+c,0) where +c is some positive constant, then 
56
//    the parametric equations of the Fl--->M' section of the curve (which has to satisfy (X(0),Y(0)) = Fl, (X(1),Y(1))=M', (X'(0),Y'(0)) = (vx,vy), (X'(1),Y'(1)) = (+c,0)) is
57
//
58
//    X(t) = ( (mx-flx)-vx )t^2 + vx*t + flx                                  (*)
59
//    Y(t) = ( vy - 2(my-fly) )t^3 + ( 3(my-fly) -2vy )t^2 + vy*t + fly
60
//
61
//    Here we have to have X'(1) = 2(mx-flx)-vx which is positive <==> vx<2(mx-flx). We also have to have vy<2(my-fly) so that Y'(t)>0 (this is a must otherwise we have local loops!) 
62
//    Similarly for the Fr--->M' part of the curve we have the same equation except for the fact that this time we have to have X'(1)<0 so now we have to have vx>2(mx-flx).
63
//
64
//    If we are stretching the left or right edge of the bitmap then the only difference is that we have to have (X'(1),Y'(1)) = (0,+-c) with + or - c depending on which part of the curve
65
//    we are tracing. Then the parametric equation is
66
//
67
//    X(t) = ( vx - 2(mx-flx) )t^3 + ( 3(mx-flx) -2vx )t^2 + vx*t + flx
68
//    Y(t) = ( (my-fly)-vy )t^2 + vy*t + fly
69
//
70
//    If we are dragging the top edge:    
71
//
72
//    Now point (x,u_bmpD.x) on the top edge will move by vector (X(t),Y(t)) where those functions are given by (*) and
73
//    t =  x < dSx ? (u_bmpD.x+x)/(u_bmpD.x+dSx) : (u_bmpD.x-x)/(u_bmpD.x-dSx)   
74
//    Any point (x,y) will move by vector (a*X(t),a*Y(t)) where a is (y+u_bmpD.y)/(2*u_bmpD.y)
75
  
76
void deform(in int effect, inout vec4 v)
77
  {
78
  vec2 p = vUniforms[effect+2].yz;  
79
  vec2 w = vUniforms[effect].xy;    // w = vec(MM')
80
  vec2 vert_vec, horz_vec; 
81
  vec2 signXY = sign(p-v.xy);  
82
  vec2 time = (u_bmpD.xy+signXY*v.xy)/(u_bmpD.xy+signXY*p);
83
  vec2 factorV = vec2(0.5,0.5) + sign(p)*v.xy/(4.0*u_bmpD.xy);
84
  vec2 factorD = (u_bmpD.xy-signXY*p)/(2.0*u_bmpD.xy);
85
  vec2 vert_d = factorD.x*w;
86
  vec2 horz_d = factorD.y*w;
87
  vec2 corr = 0.33 / ( 1.0 + (4.0*u_bmpD.x*u_bmpD.x)/dot(w,w) ) * (p+w+signXY*u_bmpD.xy); // .x = the vector tangent to X(t) at Fl = 0.3*vec(LM')  (or vec(RM') if signXY.x=-1). 
88
                                                                                       // .y = the vector tangent to X(t) at Fb = 0.3*vec(BM')  (or vec(TM') if signXY.y=-1)
89
                                                                                       // the scalar: make the length of the speed vectors at Fl and Fr be 0 when force vector 'w' is zero 
90
  vert_vec.x = ( w.x-vert_d.x-corr.x )*time.x*time.x + corr.x*time.x + vert_d.x;
91
  horz_vec.y = (-w.y+horz_d.y+corr.y )*time.y*time.y - corr.y*time.y - horz_d.y;
92
  vert_vec.y = (-3.0*vert_d.y+2.0*w.y )*time.x*time.x*time.x + (-3.0*w.y+5.0*vert_d.y )*time.x*time.x - vert_d.y*time.x - vert_d.y;
93
  horz_vec.x = ( 3.0*horz_d.x-2.0*w.x )*time.y*time.y*time.y + ( 3.0*w.x-5.0*horz_d.x )*time.y*time.y + horz_d.x*time.y + horz_d.x;  
94
  
95
  v.xy += (factorV.y*vert_vec + factorV.x*horz_vec);  
96
  }
97

    
98
//////////////////////////////////////////////////////////////////////////////////////////////
99
// Let (v.x,v.y) be point P (the current vertex). 
100
// Let vPoint[effect].xy be point S (the center of effect)
101
// Let vPoint[effect].xy + vRegion[effect].xy be point O (the center of the Region circle)
102
// Let X be the point where the halfline SP meets a) if region is non-null, the region circle b) otherwise, the edge of the bitmap. 
103
//
104
// If P is inside the Region, this function returns |PX|/||SX|, aka the 'degree' of point P. Otherwise, it returns 0. 
105
//
106
// We compute the point where half-line from S to P intersects the edge of the bitmap. If that's inside the circle, end. If not, we solve the 
107
// the triangle with vertices at O, P and the point of intersection with the circle we are looking for X.
108
// We know the lengths |PO|, |OX| and the angle OPX , because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
109
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so 
110
// PX = -a + sqrt(a^2 + OX^2 - PO^2) where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
111
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
112
//
113
// the trick below is the if-less version of the
114
// 
115
// t = dx<0.0 ? (u_bmpD.x-v.x) / (u_bmpD.x-ux) : (u_bmpD.x+v.x) / (u_bmpD.x+ux);
116
// h = dy<0.0 ? (u_bmpD.y-v.y) / (u_bmpD.y-uy) : (u_bmpD.y+v.y) / (u_bmpD.y+uy);
117
// d = min(t,h);      
118
//
119
// float d = min(-ps.x/(sign(ps.x)*u_bmpD.x+p.x),-ps.y/(sign(ps.y)*u_bmpD.y+p.y))+1.0;    
120

    
121
//////////////////////////////////////////////////////////////////////////////////////////////
122
// return degree of the point as defined by the bitmap rectangle
123
   
124
float degree_bitmap(in vec2 S, in vec2 PS)
125
  {
126
  return min(-PS.x/(sign(PS.x)*u_bmpD.x+S.x),-PS.y/(sign(PS.y)*u_bmpD.y+S.y))+1.0;    
127
  }
128

    
129
//////////////////////////////////////////////////////////////////////////////////////////////
130
// return degree of the point as defined by the Region
131
// Currently only supports circles; .xy = vector from center of effect to the center of the circle, .z = radius
132
      
133
float degree_region(in vec3 region, in vec2 PS)
134
  {
135
  vec2 PO  = PS + region.xy;
136
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
137
  float ps_sq = dot(PS,PS);
138
  float DOT  = dot(PS,PO)/ps_sq;
139
  
140
  return max(sign(D),0.0) / (1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT));  // if D<=0 (i.e p is outside the Region) return 0.
141
  }
142
   
143
//////////////////////////////////////////////////////////////////////////////////////////////
144
// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
145
    
146
float degree(in vec3 region, in vec2 S, in vec2 PS)
147
  {
148
  vec2 PO  = PS + region.xy;
149
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
150
  float E = min(-PS.x/(sign(PS.x)*u_bmpD.x+S.x),-PS.y/(sign(PS.y)*u_bmpD.y+S.y))+1.0;    
151
  float ps_sq = dot(PS,PS);
152
  float DOT  = dot(PS,PO)/ps_sq;
153
  
154
  return max(sign(D),0.0) * min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT)),E);  // if D<=0 (i.e p is outside the Region) return 0.
155
  }
156

    
157
//////////////////////////////////////////////////////////////////////////////////////////////
158
// Distort effect
159
//
160
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where 
161
// a=Py/Sy (N --> when (Px,Py) is above (Sx,Sy)) or a=Px/Sx (W) or a=(w-Px)/(w-Sx) (E) or a=(h-Py)/(h-Sy) (S) 
162
// It remains to be computed which of the N,W,E or S case we have: answer: a = min[ Px/Sx , Py/Sy , (w-Px)/(w-Sx) , (h-Py)/(h-Sy) ]
163
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2)
164
//  
165
// the vertical part
166
// Let |(v.x,v.y),(ux,uy)| = |PS|, ux-v.x=dx,uy-v.y=dy, f(x) (0<=x<=|SX|) be the shape of the side of the bubble.
167
// H(v.x,v.y) = |PS|>|SX| ? 0 : f(|PX|)
168
// N(v.x,v.y) = |PS|>|SX| ? (0,0,1) : ( -(dx/|PS|)sin(beta), -(dy/|PS|)sin(beta), cos(beta) ) where tan(beta) is f'(|PX|) 
169
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|))         
170
//
171
// Now we also have to take into account the effect horizontal move by V=(u_dVx[i],u_dVy[i]) will have on the normal vector.
172
// Solution: 
173
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
174
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
175
// 3. The parallel component changes the length of |SX| by the factor of a=(|SX|-|Vpar|)/|SX| (where the length can be negative depending on the direction)   
176
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
177
//
178
// |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq)  (-Vy because y is inverted)
179
// a =  (|SX| - |Vpar|)/|SX| = 1 - |Vpar|/((sqrt(ps_sq)/(1-d)) = 1 - (1-d)*|Vpar|/sqrt(ps_sq) = 1-(1-d)*(Vx*dx-Vy*dy)/ps_sq 
180
//
181
// Side of the bubble
182
// 
183
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble          
184
// Case 1: 
185
// f(t) = t, i.e. f(x) = uz * x/|SX|   (a cone)
186
// -|PS|/f'(|PX|) = -|PS|*|SX|/uz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
187
// so finally -|PS|/f'(|PX|) = -ps_sq/(uz*(1-d))
188
//                    
189
// Case 2: 
190
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve)
191
// here we have t = x/|SX| which makes f'(|PX|) = 6*uz*|PS|*|PX|/|SX|^3.
192
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6uz|PX|) =  (-|SX|^2) / (6*uz*d) but
193
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2
194
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
195
//                  
196
// Case 3:
197
// f(t) = 3t^4-8t^3+6t^2 would be better as this safisfies f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 and f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0
198
// so this produces a fuller, thicker bubble!
199
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!) 
200
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)  
201
//
202
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces.
203
// If a surface is given by z = f(x,y), then the normal vector at (x0,y0) is given by (df/dx (x0,y0), df/dy (x0,y0), 1 ).
204
// so if we have two surfaces defined by f1(x,y) and f2(x,y) with their normals expressed as (f1x,f1y,1) and (f2x,f2y,1) 
205
// then the normal to g = f1+f2 is simply given by (f1x+f2x,f1y+f2y,1), i.e. if the third component is 1, then we can simply
206
// add up the first and second components.
207
//
208
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding the first two components. 
209
// (a is the horizontal part)
210
        
211
void distort(in int effect, inout vec4 v, inout vec4 n)
212
  {
213
  vec2 point = vUniforms[effect+2].yz;
214
  vec2 ps = point-v.xy;
215
  float d = degree(vUniforms[effect+1],point,ps);
216
  vec2 w = vec2(vUniforms[effect].x, -vUniforms[effect].y);
217
  float dt = dot(ps,ps);
218
  float uz = vUniforms[effect].z; // height of the bubble
219
     
220
  //v.z += uz*d;                                                                                // cone
221
  //b = -(uz*(1.0-d)) / (dt + (1.0-d)*dot(w,ps) + (sign(dt)-1.0) );                             //
222
        
223
  //v.z += uz*d*d*(3.0-2.0*d);                                                                  // thin bubble
224
  //b = -(6.0*uz*d*(1.0-d)*(1.0-d)) / (dt + (1.0-d)*dot(w,ps) + (sign(dt)-1.0) );               //
225
        
226
  v.z += uz*d*d*(3.0*d*d -8.0*d +6.0);                                                          // thick bubble
227
  float b = -(12.0*uz*d*(1.0-d)*(1.0-d)*(1.0-d)) / (dt + (1.0-d)*dot(w,ps) + (sign(dt)-1.0) );  // the last part - (sign-1) is to avoid b being a NaN when ps=(0,0)
228
                
229
  v.xy += d*w;  
230
  n.xy += b*ps;
231
  }
232
 
233
//////////////////////////////////////////////////////////////////////////////////////////////
234
// sink effect
235
// Pull P=(v.x,v.y) towards S=vPoint[effect] with P' = P + (1-h)d(S-P)
236
// when h>1 we are pushing points away from S: P' = P + (1/h-1)d(S-P)
237
 
238
void sink(in int effect,inout vec4 v)
239
  {
240
  vec2 point = vUniforms[effect+2].yz;
241
  vec2 ps = point-v.xy;
242
  float h = vUniforms[effect].x;
243
  float t = degree(vUniforms[effect+1],point,ps) * (1.0-h)/max(1.0,h);                                                                        
244
  
245
  v.xy += t*ps;           
246
  }
247

    
248
//////////////////////////////////////////////////////////////////////////////////////////////
249
// Swirl 
250
//
251
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
252
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated 
253
// by (1-d) around the center of the effect S.
254

    
255
void swirl(in int effect, inout vec4 P)
256
  {
257
  vec2 S  = vUniforms[effect+2].yz;
258
  vec2 PS = S-P.xy;
259
  vec3 SO = vUniforms[effect+1];
260
  float d1_circle = degree_region(SO,PS);
261
  float d1_bitmap = degree_bitmap(S,PS);
262
  float sinA = vUniforms[effect].y;                            // sin(A) precomputed in EffectListVertex.postprocess                                         
263
  float cosA = vUniforms[effect].z;                            // cos(A) precomputed in EffectListVertex.postprocess  
264
  vec2 PS2 = vec2( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA ); // vector PS rotated by A radians clockwise around S.                               
265
  vec3 SG = (1.0-d1_circle)*SO;                                // coordinates of the dilated circle P is going to get rotated around
266
  float d2 = max(0.0,degree(SG,S,PS2));                        // make it a max(0,deg) because when S=left edge of the bitmap, otherwise
267
                                                               // some points end up with d2<0 and they disappear off view.
268
  P.xy += min(d1_circle,d1_bitmap)*(PS - PS2/(1.0-d2));        // if d2=1 (i.e P=S) we should have P unchanged. How to do it?
269
  }
270

    
271
//////////////////////////////////////////////////////////////////////////////////////////////
272
// Wave
273
//
274

    
275
void wave(in int effect, inout vec4 P)
276
  {
277

    
278
  }
279

    
280
//////////////////////////////////////////////////////////////////////////////////////////////
281
// Clamp v.z to (-u_Depth,u_Depth) with the following function:
282
// define h to be, say, 0.7; let H=u_Depth
283
//      if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H   (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H)
284
// else if v.z >  hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H   (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H)
285
// else v.z = v.z  
286
	
287
void restrict(inout float v)
288
  {
289
  const float h = 0.7;
290
  float signV = 2.0*max(0.0,sign(v))-1.0;
291
  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
292
  float b = max(0.0,sign(abs(v)-h*u_Depth));
293
  
294
  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
295
  }                
296
#endif
297

    
298
//////////////////////////////////////////////////////////////////////////////////////////////
299
  		  
300
void main()                                                 	
301
  {              
302
  vec4 v = vec4( 2.0*u_bmpD*a_Position,1.0 );
303
  vec4 n = vec4(a_Normal,0.0);
304

    
305
#if NUM_VERTEX>0
306
  for(int i=0; i<vNumEffects; i++)
307
    {
308
    //switch(vType[i])
309
    //  {
310
    //  case DISTORT: distort(3*i,v,n); break;
311
    //  case DEFORM : deform(3*i,v)   ; break;
312
    //  case SINK   : sink(3*i,v)     ; break;
313
    //  case SWIRL  : swirl(3*i,v)    ; break;
314
    //  case WAVE   : wave(3*i,v)     ; break;
315
    //  }
316
        
317
         if( vType[i]==DISTORT) distort(3*i,v,n);
318
    else if( vType[i]==DEFORM ) deform(3*i,v);
319
    else if( vType[i]==SINK   ) sink(3*i,v);
320
    else if( vType[i]==SWIRL  ) swirl(3*i,v);
321
    else if( vType[i]==WAVE   ) wave(3*i,v);   
322
    }
323
 
324
  restrict(v.z);  
325
#endif
326
   
327
  v_Position      = vec3(u_MVMatrix*v);           
328
  v_Color         = a_Color;              
329
  v_TexCoordinate = a_TexCoordinate;                                         
330
  v_Normal        = normalize(vec3(u_MVMatrix*n));
331
  gl_Position     = u_MVPMatrix*v;      
332
  }                               
(2-2/2)