commit 4aa3864980bd0833c1a88b7102020b48dcd060b3
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sun Dec 30 01:37:34 2018 +0000

    Redefine the Vertex Region from (x,y,r,unused) to (x,y,z,r). This takes into account the 'Z', which makes it possible to warp only one side of a 3D Mesh like Sphere.
    
    Also corresponding changes in applications.

diff --git a/src/main/java/org/distorted/library/effect/VertexEffectDeform.java b/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
index 3a7b019..fa354b0 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
@@ -110,28 +110,28 @@ public class VertexEffectDeform extends VertexEffect
     {
     addEffect( EffectName.DEFORM,
 
-        "const vec2 ONE = vec2(1.0,1.0);  \n"
+        "const vec3 ONE = vec3(1.0,1.0,1.0);  \n"
       + "const float A = 0.5; \n"
       + "const float B = 0.2; \n"
       + "const float C = 5.0; \n"
 
-      + "vec2 center = vUniforms[effect+1].yz; \n"
-      + "vec2 ps     = center-v.xy; \n"
-      + "vec2 aPS    = abs(ps); \n"
-      + "vec2 maxps  = u_objD.xy + abs(center); \n"
+      + "vec3 center = vUniforms[effect+1].yzw; \n"
+      + "vec3 ps     = center-v.xyz; \n"
+      + "vec3 aPS    = abs(ps); \n"
+      + "vec3 maxps  = u_objD + abs(center); \n"
       + "float d     = degree_region(vUniforms[effect+2],ps); \n"
       + "vec3 force  = vUniforms[effect].xyz * d; \n"
-      + "vec2 aForce = abs(force.xy); \n"
-      + "float denom = dot(ps+(1.0-d)*force.xy,ps); \n"
+      + "vec3 aForce = abs(force); \n"
+      + "float denom = dot(ps+(1.0-d)*force,ps); \n"
       + "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n"
-      + "vec2 Aw = A*maxps; \n"
-      + "vec2 quot = ps / maxps; \n"
+      + "vec3 Aw = A*maxps; \n"
+      + "vec3 quot = ps / maxps; \n"
       + "quot = quot*quot; \n"                                                 // ( (x/W)^2 , (y/H)^2 ) where x,y are distances from V to center
 
       + "float denomV = 1.0 / (aForce.y + Aw.x); \n"
       + "float denomH = 1.0 / (aForce.x + Aw.y); \n"
 
-      + "vec2 vertCorr= ONE - aPS / ( aForce+C*aPS + (ONE-sign(aForce)) ); \n" // avoid division by 0 when force and PS both are 0
+      + "vec3 vertCorr= ONE - aPS / ( aForce+C*aPS + (ONE-sign(aForce)) ); \n" // avoid division by 0 when force and PS both are 0
 
       + "float mvXvert = -B * ps.x * aForce.y * (1.0-quot.y) * denomV; \n"     // impact the vertical   component of the force vector has on horizontal movement
       + "float mvYhorz = -B * ps.y * aForce.x * (1.0-quot.x) * denomH; \n"     // impact the horizontal component of the force vector has on vertical   movement
@@ -144,7 +144,7 @@ public class VertexEffectDeform extends VertexEffect
       + "v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); \n"                         // thick bubble
       + "float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom; \n"
 
-      + "n.xy += n.z*b*ps;"
+      + "n.xy += n.z*b*ps.xy;"
       );
     }
 
@@ -178,6 +178,6 @@ public class VertexEffectDeform extends VertexEffect
     super(EffectName.DEFORM);
     mVector = vector;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectDistort.java b/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
index 46a4bb9..66f2e6a 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
@@ -112,11 +112,11 @@ public class VertexEffectDistort extends VertexEffect
     {
     addEffect(EffectName.DISTORT,
 
-        "vec2 center = vUniforms[effect+1].yz; \n"
-      + "vec2 ps = center-v.xy; \n"
+        "vec3 center = vUniforms[effect+1].yzw; \n"
+      + "vec3 ps = center-v.xyz; \n"
       + "vec3 force = vUniforms[effect].xyz; \n"
       + "float d = degree(vUniforms[effect+2],center,ps); \n"
-      + "float denom = dot(ps+(1.0-d)*force.xy,ps); \n"
+      + "float denom = dot(ps+(1.0-d)*force,ps); \n"
       + "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n"          // = denom==0 ? 1000:1/denom;
 
        //v.z += force.z*d;                                                        // cone
@@ -129,7 +129,7 @@ public class VertexEffectDistort extends VertexEffect
       + "float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom; \n"  //
 
       + "v.xy += d*force.xy; \n"
-      + "n.xy += n.z*b*ps;"
+      + "n.xy += n.z*b*ps.xy;"
       );
     }
 
@@ -161,7 +161,7 @@ public class VertexEffectDistort extends VertexEffect
     super(EffectName.DISTORT);
     mVector = vector;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
 
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectPinch.java b/src/main/java/org/distorted/library/effect/VertexEffectPinch.java
index e3c6913..4de3a57 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectPinch.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectPinch.java
@@ -69,13 +69,13 @@ public class VertexEffectPinch extends VertexEffect
     {
     addEffect(EffectName.PINCH,
 
-        "vec2 center = vUniforms[effect+1].yz; \n"
-      + "vec2 ps = center-v.xy; \n"
+        "vec3 center = vUniforms[effect+1].yzw; \n"
+      + "vec3 ps = center-v.xyz; \n"
       + "float h = vUniforms[effect].x; \n"
       + "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
       + "float angle = vUniforms[effect].y; \n"
       + "vec2 dir = vec2(sin(angle),-cos(angle)); \n"
-      + "v.xy += t*dot(ps,dir)*dir;"
+      + "v.xy += t*dot(ps.xy,dir)*dir;"
       );
     }
 
@@ -109,6 +109,6 @@ public class VertexEffectPinch extends VertexEffect
     super(EffectName.PINCH);
     mPinch  = pinch;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectSink.java b/src/main/java/org/distorted/library/effect/VertexEffectSink.java
index 29fae8f..49da861 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectSink.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectSink.java
@@ -65,12 +65,12 @@ public class VertexEffectSink extends VertexEffect
     {
     addEffect(EffectName.SINK,
 
-        "vec2 center = vUniforms[effect+1].yz; \n"
-      + "vec2 ps = center-v.xy; \n"
+        "vec3 center = vUniforms[effect+1].yzw; \n"
+      + "vec3 ps = center-v.xyz; \n"
       + "float h = vUniforms[effect].x; \n"
       + "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
 
-      + "v.xy += t*ps;"
+      + "v.xy += t*ps.xy;"
       );
     }
 
@@ -104,6 +104,6 @@ public class VertexEffectSink extends VertexEffect
     super(EffectName.SINK);
     mSink   = sink;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectSwirl.java b/src/main/java/org/distorted/library/effect/VertexEffectSwirl.java
index a5ee386..f817e81 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectSwirl.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectSwirl.java
@@ -66,8 +66,8 @@ public class VertexEffectSwirl extends VertexEffect
     {
     addEffect(EffectName.SWIRL,
 
-        "vec2 center  = vUniforms[effect+1].yz; \n"
-      + "vec2 PS = center-v.xy; \n"
+        "vec3 center  = vUniforms[effect+1].yzw; \n"
+      + "vec3 PS = center-v.xyz; \n"
       + "vec4 SO = vUniforms[effect+2]; \n"
       + "float d1_circle = degree_region(SO,PS); \n"
       + "float d1_bitmap = degree_bitmap(center,PS); \n"
@@ -76,11 +76,11 @@ public class VertexEffectSwirl extends VertexEffect
       + "float sinA = sin(alpha); \n"
       + "float cosA = cos(alpha); \n"
 
-      + "vec2 PS2 = vec2( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA ); \n" // vector PS rotated by A radians clockwise around center.
-      + "vec4 SG = (1.0-d1_circle)*SO; \n"                                // coordinates of the dilated circle P is going to get rotated around
-      + "float d2 = max(0.0,degree(SG,center,PS2)); \n"                   // make it a max(0,deg) because otherwise when center=left edge of the
-                                                                          // bitmap some points end up with d2<0 and they disappear off view.
-      + "v.xy += min(d1_circle,d1_bitmap)*(PS - PS2/(1.0-d2)); \n"        // if d2=1 (i.e P=center) we should have P unchanged. How to do it?
+      + "vec3 PS2 = vec3( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA, PS.z ); \n" // vector PS rotated by A radians clockwise around center.
+      + "vec4 SG = (1.0-d1_circle)*SO; \n"                                      // coordinates of the dilated circle P is going to get rotated around
+      + "float d2 = max(0.0,degree(SG,center,PS2)); \n"                         // make it a max(0,deg) because otherwise when center=left edge of the
+                                                                                // bitmap some points end up with d2<0 and they disappear off view.
+      + "v.xy += min(d1_circle,d1_bitmap)*(PS.xy - PS2.xy/(1.0-d2)); \n"        // if d2=1 (i.e P=center) we should have P unchanged. How to do it?
       );
     }
 
@@ -112,7 +112,7 @@ public class VertexEffectSwirl extends VertexEffect
     super(EffectName.SWIRL);
     mSwirl  = swirl;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
 
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectWave.java b/src/main/java/org/distorted/library/effect/VertexEffectWave.java
index 40ffb6c..103c9cb 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectWave.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectWave.java
@@ -127,11 +127,11 @@ public class VertexEffectWave extends VertexEffect
     {
     addEffect(EffectName.WAVE,
 
-        "vec2 center     = vUniforms[effect+1].yz; \n"
+        "vec3 center     = vUniforms[effect+1].yzw; \n"
       + "float amplitude = vUniforms[effect  ].x; \n"
       + "float length    = vUniforms[effect  ].y; \n"
 
-      + "vec2 ps = center - v.xy; \n"
+      + "vec3 ps = center - v.xyz; \n"
       + "float deg = amplitude*degree_region(vUniforms[effect+2],ps); \n"
 
       + "if( deg != 0.0 && length != 0.0 ) \n"
@@ -170,7 +170,7 @@ public class VertexEffectWave extends VertexEffect
 
       +     "vec3 normal = cross(sx,sy); \n"
 
-      +     "if( normal.z<=0.0 ) \n"                   // Why this bizarre shit rather than the straightforward
+      +     "if( normal.z<=0.0 ) \n"                   // Why this bizarre thing rather than the straightforward
       +       "{ \n"                                   //
       +       "normal.x= 0.0; \n"                      // if( normal.z>0.0 )
       +       "normal.y= 0.0; \n"                      //   {
@@ -179,7 +179,7 @@ public class VertexEffectWave extends VertexEffect
                                                        //   n.z = (n.z*normal.z);
                                                        //   }
       +     "n.x = (n.x*normal.z + n.z*normal.x); \n"  //
-      +     "n.y = (n.y*normal.z + n.z*normal.y); \n"  // ? Because if we do the above, my shitty Nexus4 crashes
+      +     "n.y = (n.y*normal.z + n.z*normal.y); \n"  // ? Because if we do the above, my Nexus4 crashes
       +     "n.z = (n.z*normal.z); \n"                 // during shader compilation!
       +     "} \n"
       +   "}"
@@ -235,6 +235,6 @@ public class VertexEffectWave extends VertexEffect
     super(EffectName.WAVE);
     mWave   = wave;
     mCenter = center;
-    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
+    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
     }
   }
diff --git a/src/main/java/org/distorted/library/main/DistortedScreen.java b/src/main/java/org/distorted/library/main/DistortedScreen.java
index 423580e..e10729a 100644
--- a/src/main/java/org/distorted/library/main/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/main/DistortedScreen.java
@@ -38,9 +38,11 @@ import org.distorted.library.type.Static3D;
 public class DistortedScreen extends DistortedFramebuffer
   {
   ///// DEBUGGING ONLY /////////////////////////
-  private boolean mShowFPS;
-
   private static final int NUM_FRAMES  = 100;
+  private static final int FPS_W       = 120;
+  private static final int FPS_H       =  70;
+
+  private boolean mShowFPS;
 
   private MeshQuad fpsMesh;
   private DistortedTexture fpsTexture;
@@ -48,7 +50,6 @@ public class DistortedScreen extends DistortedFramebuffer
   private Canvas fpsCanvas;
   private Bitmap fpsBitmap;
   private Paint mPaint;
-  private int fpsH, fpsW;
   private String fpsString;
   private long lastTime=0;
   private long[] durations;
@@ -100,9 +101,9 @@ public class DistortedScreen extends DistortedFramebuffer
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
 
       mPaint.setColor(0xffffffff);
-      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
+      fpsCanvas.drawRect(0, 0, FPS_W, FPS_H, mPaint);
       mPaint.setColor(0xff000000);
-      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
+      fpsCanvas.drawText(fpsString, FPS_W/2, 0.75f*FPS_H, mPaint);
       fpsTexture.setTexture(fpsBitmap);
 
       lastTime = time;
@@ -129,7 +130,7 @@ public class DistortedScreen extends DistortedFramebuffer
 
     if( mShowFPS && fpsTexture.setAsInput())
       {
-      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time);
+      fpsEffects.drawPriv(FPS_W / 2.0f, FPS_H / 2.0f, fpsMesh, this, time);
       }
 
     if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
@@ -155,14 +156,10 @@ public class DistortedScreen extends DistortedFramebuffer
     if( !mShowFPS )
       {
       mShowFPS = true;
-
-      fpsW = 120;
-      fpsH = 70;
-
       fpsString = "";
-      fpsBitmap = Bitmap.createBitmap(fpsW, fpsH, Bitmap.Config.ARGB_8888);
+      fpsBitmap = Bitmap.createBitmap(FPS_W, FPS_H, Bitmap.Config.ARGB_8888);
       fpsMesh = new MeshQuad();
-      fpsTexture = new DistortedTexture(fpsW, fpsH);
+      fpsTexture = new DistortedTexture(FPS_W, FPS_H);
       fpsTexture.setTexture(fpsBitmap);
       fpsCanvas = new Canvas(fpsBitmap);
       fpsEffects = new DistortedEffects();
@@ -171,7 +168,7 @@ public class DistortedScreen extends DistortedFramebuffer
       mPaint = new Paint();
       mPaint.setAntiAlias(true);
       mPaint.setTextAlign(Paint.Align.CENTER);
-      mPaint.setTextSize(0.7f * fpsH);
+      mPaint.setTextSize(0.7f * FPS_H);
 
       durations = new long[NUM_FRAMES + 1];
       currDuration = 0;
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index fe6a09b..7720a97 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -43,7 +43,7 @@ uniform int vNumEffects;             // total number of vertex effects
 uniform int vName[NUM_VERTEX];       // their names.
 uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
                                      // The first vec4 is the Interpolated values,
-                                     // next is half cache half Center, the third -  the Region.
+                                     // second vec4: first float - cache, next 3: Center, the third -  the Region.
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 // HELPER FUNCTIONS
@@ -62,13 +62,13 @@ uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*
 //////////////////////////////////////////////////////////////////////////////////////////////
 // return degree of the point as defined by the bitmap rectangle
 
-float degree_bitmap(in vec2 S, in vec2 PS)
+float degree_bitmap(in vec3 S, in vec3 PS)
   {
-  vec2 A = sign(PS)*u_objD.xy + S;
+  vec3 A = sign(PS)*u_objD + S;
 
-  vec2 signA = sign(A);                           //
-  vec2 signA_SQ = signA*signA;                    // div = PS/A if A!=0, 0 otherwise.
-  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));//
+  vec3 signA = sign(A);                                    //
+  vec3 signA_SQ = signA*signA;                             // div = PS/A if A!=0, 0 otherwise.
+  vec3 div = signA_SQ*PS/(A-(vec3(1.0,1.0,1.0)-signA_SQ)); //
 
   return 1.0-max(div.x,div.y);
   }
@@ -94,10 +94,10 @@ float degree_bitmap(in vec2 S, in vec2 PS)
 // 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.
 
-float degree_region(in vec4 region, in vec2 PS)
+float degree_region(in vec4 region, in vec3 PS)
   {
-  vec2 PO  = PS + region.xy;
-  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  vec3 PO  = PS + region.xyz;
+  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
 
   if( D<=0.0 ) return 0.0;
 
@@ -119,17 +119,17 @@ float degree_region(in vec4 region, in vec2 PS)
 //////////////////////////////////////////////////////////////////////////////////////////////
 // return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
 
-float degree(in vec4 region, in vec2 S, in vec2 PS)
+float degree(in vec4 region, in vec3 S, in vec3 PS)
   {
-  vec2 PO  = PS + region.xy;
-  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  vec3 PO  = PS + region.xyz;
+  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
 
   if( D<=0.0 ) return 0.0;
 
-  vec2 A = sign(PS)*u_objD.xy + S;
-  vec2 signA = sign(A);
-  vec2 signA_SQ = signA*signA;
-  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));
+  vec3 A = sign(PS)*u_objD.xyz + S;
+  vec3 signA = sign(A);
+  vec3 signA_SQ = signA*signA;
+  vec3 div = signA_SQ*PS/(A-(vec3(1.0,1.0,1.0)-signA_SQ));
   float E = 1.0-max(div.x,div.y);
 
   float ps_sq = dot(PS,PS);
