commit 341c803d478413dbcfc0c29ac4b6d1135472f643
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Mon Jun 27 13:00:01 2016 +0100

    Progress with Vertex3D

diff --git a/src/main/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index f611bdb..698d741 100644
--- a/src/main/res/raw/main_fragment_shader.glsl
+++ b/src/main/res/raw/main_fragment_shader.glsl
@@ -36,7 +36,7 @@ uniform vec4 fUniforms[2*NUM_FRAGMENT]; // i-th effect is 2 consecutive vec4's:
 const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
  
 //////////////////////////////////////////////////////////////////////////////////////////////
-// macroblocks
+// MACROBLOCK EFFECT
 
 void macroblock(float degree, int effect, inout vec2 tex)
   {
@@ -46,7 +46,7 @@ void macroblock(float degree, int effect, inout vec2 tex)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// change the RGB values
+// CHROMA EFFECT
 
 void chroma(float degree, int effect, inout vec4 color)
   {
@@ -54,7 +54,7 @@ void chroma(float degree, int effect, inout vec4 color)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// change the transparency level
+// ALPHA EFFECT (change transparency level)
 
 void alpha(float degree, int effect, inout vec4 color)
   {
@@ -62,7 +62,7 @@ void alpha(float degree, int effect, inout vec4 color)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// change the brightness level
+// BRIGHTNESS EFFECT
 
 void brightness(float degree, int effect, inout vec4 color)
   {
@@ -70,7 +70,7 @@ void brightness(float degree, int effect, inout vec4 color)
   }
   
 //////////////////////////////////////////////////////////////////////////////////////////////
-// change the contrast level
+// CONTRAST EFFECT
 
 void contrast(float degree, int effect, inout vec4 color)
   {
@@ -78,7 +78,7 @@ void contrast(float degree, int effect, inout vec4 color)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// change the saturation level
+// SATURATION EFFECT
 
 void saturation(float degree, int effect, inout vec4 color)
   {
@@ -102,22 +102,7 @@ void main()
     {
     diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
     pointDegree = max(0.0,1.0-dot(diff,diff));
-  
-    //switch(fType[i])  // future version of GLSL
-    //  {
-    //  case MACROBLOCK        : macroblock(sign(pointDegree),2*i,tex); break;
-    //  case CHROMA            : chroma    (sign(pointDegree),2*i,col); break;
-    //  case SMOOTH_CHROMA     : chroma    (     pointDegree ,2*i,col); break;
-    //  case ALPHA             : alpha     (sign(pointDegree),2*i,col); break;
-    //  case SMOOTH_ALPHA      : alpha     (     pointDegree ,2*i,col); break;
-    //  case BRIGHTNESS        : brightness(sign(pointDegree),2*i,col); break;
-    //  case SMOOTH_BRIGHTNESS : brightness(     pointDegree ,2*i,col); break;
-    //  case CONTRAST          : contrast  (sign(pointDegree),2*i,col); break;
-    //  case SMOOTH_CONTRAST   : contrast  (     pointDegree ,2*i,col); break;
-    //  case SATURATION        : saturation(sign(pointDegree),2*i,col); break;
-    //  case SMOOTH_SATURATION : saturation(     pointDegree ,2*i,col); break;
-    //  }
-    
+
          if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i,tex);
     else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,col);
     else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,col);
@@ -133,4 +118,4 @@ void main()
 #endif
  
   gl_FragColor = (col * 0.5 * (v_Normal.z+1.0) * texture2D(u_Texture, tex));
-  }
+  }
\ No newline at end of file
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 4c5b18d..6c8654f 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -48,8 +48,97 @@ uniform vec3 vUniforms[3*NUM_VERTEX];     // i-th effect is 3 consecutive vec3's
 #endif
 
 #if NUM_VERTEX>0
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+// HELPER FUNCTIONS
+//////////////////////////////////////////////////////////////////////////////////////////////
+// Let (v.x,v.y) be point P (the current vertex).
+// Let vPoint[effect].xy be point S (the center of effect)
+// Let vPoint[effect].xy + vRegion[effect].xy be point O (the center of the Region circle)
+// 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.
+//
+// If P is inside the Region, this function returns |PX|/||SX|, aka the 'degree' of point P. Otherwise, it returns 0.
+//
+// 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
+// the triangle with vertices at O, P and the point of intersection with the circle we are looking for X.
+// We know the lengths |PO|, |OX| and the angle OPX , because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
+// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so
+// 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
+// |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.
+//
+// the trick below is the if-less version of the
+//
+// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
+// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
+// d = min(t,h);
+//
+// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
+//
+// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
+// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
+//
+//////////////////////////////////////////////////////////////////////////////////////////////
+// return degree of the point as defined by the bitmap rectangle
+
+float degree_bitmap(in vec2 S, in vec2 PS)
+  {
+  vec2 A = sign(PS)*u_objD.xy + S;
+  float B = sign(A.x*A.y);
+
+  return B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
+  }
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+// return degree of the point as defined by the Region
+// Currently only supports circles; .xy = vector from center of effect to the center of the circle, .z = radius
+
+float degree_region(in vec3 region, in vec2 PS)
+  {
+  vec2 PO  = PS + region.xy;
+  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  float ps_sq = dot(PS,PS);
+  float DOT  = dot(PS,PO)/ps_sq;
+
+  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.
+  }
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
+
+float degree(in vec3 region, in vec2 S, in vec2 PS)
+  {
+  vec2 PO  = PS + region.xy;
+  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  vec2 A = sign(PS)*u_objD.xy + S;
+  float B = sign(A.x*A.y);
+  float E = B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
+  float ps_sq = dot(PS,PS);
+  float DOT  = dot(PS,PO)/ps_sq;
+
+  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.
+  }
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+// Clamp v.z to (-u_Depth,u_Depth) with the following function:
+// define h to be, say, 0.7; let H=u_Depth
+//      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)
+// 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)
+// else v.z = v.z
+
+void restrict(inout float v)
+  {
+  const float h = 0.7;
+  float signV = 2.0*max(0.0,sign(v))-1.0;
+  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
+  float b = max(0.0,sign(abs(v)-h*u_Depth));
+
+  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
+  }
+
 //////////////////////////////////////////////////////////////////////////////////////////////
-// Deform the whole shape of the bitmap by force V 
+// DEFORM EFFECT
+//
+// Deform the whole shape of the bitmap by force V
 // 
 // If the point of application (Sx,Sy) is on the edge of the bitmap, then:
 // a) ignore Vz
@@ -117,74 +206,7 @@ void deform(in int effect, inout vec4 v)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// Let (v.x,v.y) be point P (the current vertex). 
-// Let vPoint[effect].xy be point S (the center of effect)
-// Let vPoint[effect].xy + vRegion[effect].xy be point O (the center of the Region circle)
-// 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. 
-//
-// If P is inside the Region, this function returns |PX|/||SX|, aka the 'degree' of point P. Otherwise, it returns 0. 
-//
-// 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 
-// the triangle with vertices at O, P and the point of intersection with the circle we are looking for X.
-// We know the lengths |PO|, |OX| and the angle OPX , because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
-// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so 
-// 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
-// |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.
-//
-// the trick below is the if-less version of the
-// 
-// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
-// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
-// d = min(t,h);      
-//
-// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
-//
-// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
-// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
-//
-//////////////////////////////////////////////////////////////////////////////////////////////
-// return degree of the point as defined by the bitmap rectangle
-   
-float degree_bitmap(in vec2 S, in vec2 PS)
-  {
-  vec2 A = sign(PS)*u_objD.xy + S;
-  float B = sign(A.x*A.y);
-
-  return B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
-  }
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-// return degree of the point as defined by the Region
-// Currently only supports circles; .xy = vector from center of effect to the center of the circle, .z = radius
-      
-float degree_region(in vec3 region, in vec2 PS)
-  {
-  vec2 PO  = PS + region.xy;
-  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
-  float ps_sq = dot(PS,PS);
-  float DOT  = dot(PS,PO)/ps_sq;
-  
-  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.
-  }
-   
-//////////////////////////////////////////////////////////////////////////////////////////////
-// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
-    
-float degree(in vec3 region, in vec2 S, in vec2 PS)
-  {
-  vec2 PO  = PS + region.xy;
-  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
-  vec2 A = sign(PS)*u_objD.xy + S;
-  float B = sign(A.x*A.y);
-  float E = B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
-  float ps_sq = dot(PS,PS);
-  float DOT  = dot(PS,PO)/ps_sq;
-  
-  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.
-  }
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-// Distort effect
+// DISTORT EFFECT
 //
 // Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where 
 // 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) 
@@ -260,7 +282,8 @@ void distort(in int effect, inout vec4 v, inout vec4 n)
   }
  
 //////////////////////////////////////////////////////////////////////////////////////////////
-// sink effect
+// SINK EFFECT
+//
 // Pull P=(v.x,v.y) towards S=vPoint[effect] with P' = P + (1-h)d(S-P)
 // when h>1 we are pushing points away from S: P' = P + (1/h-1)d(S-P)
  
@@ -275,7 +298,7 @@ void sink(in int effect,inout vec4 v)
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
-// Swirl 
+// SWIRL EFFECT
 //
 // Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
 // This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated 
@@ -297,22 +320,6 @@ void swirl(in int effect, inout vec4 P)
   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?
   }
 
-//////////////////////////////////////////////////////////////////////////////////////////////
-// Clamp v.z to (-u_Depth,u_Depth) with the following function:
-// define h to be, say, 0.7; let H=u_Depth
-//      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)
-// 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)
-// else v.z = v.z  
-	
-void restrict(inout float v)
-  {
-  const float h = 0.7;
-  float signV = 2.0*max(0.0,sign(v))-1.0;
-  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
-  float b = max(0.0,sign(abs(v)-h*u_Depth));
-  
-  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
-  }                
 #endif
 
 //////////////////////////////////////////////////////////////////////////////////////////////
@@ -325,18 +332,10 @@ void main()
 #if NUM_VERTEX>0
   for(int i=0; i<vNumEffects; i++)
     {
-    //switch(vType[i])
-    //  {
-    //  case DISTORT: distort(3*i,v,n); break;
-    //  case DEFORM : deform(3*i,v)   ; break;
-    //  case SINK   : sink(3*i,v)     ; break;
-    //  case SWIRL  : swirl(3*i,v)    ; break;
-    //  }
-        
          if( vType[i]==DISTORT) distort(3*i,v,n);
-    else if( vType[i]==DEFORM ) deform(3*i,v);
-    else if( vType[i]==SINK   ) sink(3*i,v);
-    else if( vType[i]==SWIRL  ) swirl(3*i,v);
+    else if( vType[i]==DEFORM ) deform (3*i,v);
+    else if( vType[i]==SINK   ) sink   (3*i,v);
+    else if( vType[i]==SWIRL  ) swirl  (3*i,v);
     }
  
   restrict(v.z);  
