commit 309255000265069b2c667f7a0845c697dc6d3333
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Sun Jul 17 22:27:41 2016 +0100

    Fix for Bug #18: DISTORT effect: disappearing triangles

diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 4e32e11..ec6ef9d 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -236,7 +236,8 @@ void deform(in int effect, inout vec4 v)
 // Solution: 
 // 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
 // 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
-// 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)   
+// 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)
 // 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
 //
 // |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq)  (-Vy because y is inverted)
@@ -258,8 +259,8 @@ void deform(in int effect, inout vec4 v)
 // so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
 //                  
 // Case 3:
-// 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
-// so this produces a fuller, thicker bubble!
+// 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,
+// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble!
 // then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!) 
 // so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)  
 //
@@ -269,8 +270,8 @@ void deform(in int effect, inout vec4 v)
 // 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
 // add up the first and second components.
 //
-// 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. 
-// (a is the horizontal part)
+// 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. (a is the horizontal part)
         
 void distort(in int effect, inout vec4 v, inout vec4 n)
   {
@@ -278,17 +279,18 @@ void distort(in int effect, inout vec4 v, inout vec4 n)
   vec2 ps = point-v.xy;
   float d = degree(vUniforms[effect+1],point,ps);
   vec2 w = vec2(vUniforms[effect].x, -vUniforms[effect].y);
-  float dt = dot(ps,ps);
-  float uz = vUniforms[effect].z; // height of the bubble
-     
-  //v.z += uz*d;                                                                                // cone
-  //b = -(uz*(1.0-d)) / (dt + (1.0-d)*dot(w,ps) + (sign(dt)-1.0) );                             //
+  float uz = vUniforms[effect].z;                                         // height of the bubble
+  float denominator = dot(ps+(1.0-d)*w,ps);
+  float one_over_denom = 1.0/(denominator+0.001*(1.0-sign(denominator))); // = denominator==0 ? 1000:1/denominator;
+
+  //v.z += uz*d;                                                          // cone
+  //b = -(uz*(1.0-d))*one_over_denom;                                     //
         
-  //v.z += uz*d*d*(3.0-2.0*d);                                                                  // thin bubble
-  //b = -(6.0*uz*d*(1.0-d)*(1.0-d)) / (dt + (1.0-d)*dot(w,ps) + (sign(dt)-1.0) );               //
+  //v.z += uz*d*d*(3.0-2.0*d);                                            // thin bubble
+  //b = -(6.0*uz*d*(1.0-d)*(1.0-d))*one_over_denom;                       //
         
-  v.z += uz*d*d*(3.0*d*d -8.0*d +6.0);                                                          // thick bubble
-  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)
+  v.z += uz*d*d*(3.0*d*d -8.0*d +6.0);                                    // thick bubble
+  float b = -(12.0*uz*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;          //
                 
   v.xy += d*w;
   n.xy += b*ps;
