commit 5e58293a67b480b06aa5cb1b076e9ab47393aed7
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Thu Feb 2 18:10:30 2017 +0000

    Effect-independent normals.

diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index b3caa70..005498f 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -214,7 +214,7 @@ void restrictZ(inout float v)
 //        along the force line is.
 //        0<=C<1 looks completely ridiculous and C<0 destroys the system.
 
-void deform(in int effect, inout vec3 v, inout vec3 n)
+void deform(in int effect, inout vec3 v)
   {
   const vec2 ONE = vec2(1.0,1.0);
 
@@ -247,11 +247,7 @@ void deform(in int effect, inout vec3 v, inout vec3 n)
 
   v.x -= (mvXvert+mvXhorz);
   v.y -= (mvYvert+mvYhorz);
-
   v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                          // thick bubble
-  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;//
-
-  n.xy += n.z*b*ps;
   }
 
 //////////////////////////////////////////////////////////////////////////////////////////////
@@ -309,26 +305,18 @@ void deform(in int effect, inout vec3 v, inout vec3 n)
 // 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 vec3 v, inout vec3 n)
+void distort(in int effect, inout vec3 v)
   {
   vec2 center = vUniforms[effect+1].yz;
   vec2 ps = center-v.xy;
   vec3 force = vUniforms[effect].xyz;
   float d = degree(vUniforms[effect+2],center,ps);
-  float denom = dot(ps+(1.0-d)*force.xy,ps);
-  float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0));          // = denom==0 ? 1000:1/denom;
 
-  //v.z += force.z*d;                                                  // cone
-  //b = -(force.z*(1.0-d))*one_over_denom;                             //
-        
-  //v.z += force.z*d*d*(3.0-2.0*d);                                    // thin bubble
-  //b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom;               //
-        
-  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                            // thick bubble
-  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;  //
-                
+  //v.z += force.z*d;                       // cone
+  //v.z += force.z*d*d*(3.0-2.0*d);         // thin bubble
+  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); // thick bubble
+
   v.xy += d*force.xy;
-  n.xy += n.z*b*ps;
   }
  
 //////////////////////////////////////////////////////////////////////////////////////////////
@@ -460,7 +448,7 @@ void swirl(in int effect, inout vec3 v)
 //
 // Generally speaking I'd keep to amplitude < length, as the opposite case has some other problems as well.
 
-void wave(in int effect, inout vec3 v, inout vec3 n)
+void wave(in int effect, inout vec3 v)
   {
   vec2 center     = vUniforms[effect+1].yz;
   float amplitude = vUniforms[effect  ].x;
@@ -485,41 +473,6 @@ void wave(in int effect, inout vec3 v, inout vec3 n)
     vec3 dir= vec3(sinB*cosA,cosB*cosA,sinA);
 
     v += sin(angle)*deg*dir;
-
-    if( n.z != 0.0 )
-      {
-      float sqrtX = sqrt(dir.y*dir.y + dir.z*dir.z);
-      float sqrtY = sqrt(dir.x*dir.x + dir.z*dir.z);
-
-      float sinX = ( sqrtY==0.0 ? 0.0 : dir.z / sqrtY);
-      float cosX = ( sqrtY==0.0 ? 1.0 : dir.x / sqrtY);
-      float sinY = ( sqrtX==0.0 ? 0.0 : dir.z / sqrtX);
-      float cosY = ( sqrtX==0.0 ? 1.0 : dir.y / sqrtX);
-
-      float abs_z = dir.z <0.0 ? -(sinX*sinY) : (sinX*sinY);
-
-      float tmp = 1.578*cos(angle)*deg/length;
-
-      float fx =-cosB*tmp;
-      float fy = sinB*tmp;
-
-      vec3 sx = vec3 (1.0+cosX*fx,cosY*sinX*fx,abs_z*fx);
-      vec3 sy = vec3 (cosX*sinY*fy,1.0+cosY*fy,abs_z*fy);
-
-      vec3 normal = cross(sx,sy);
-
-      if( normal.z<=0.0 )                   // Why this bizarre shit rather than the straightforward
-        {                                   //
-        normal.x= 0.0;                      // if( normal.z>0.0 )
-        normal.y= 0.0;                      //   {
-        normal.z= 1.0;                      //   n.x = (n.x*normal.z + n.z*normal.x);
-        }                                   //   n.y = (n.y*normal.z + n.z*normal.y);
-                                            //   n.z = (n.z*normal.z);
-                                            //   }
-      n.x = (n.x*normal.z + n.z*normal.x);  //
-      n.y = (n.y*normal.z + n.z*normal.y);  // ? Because if we do the above, my shitty Nexus4 crashes
-      n.z = (n.z*normal.z);                 // during shader compilation!
-      }
     }
   }
 
@@ -533,20 +486,71 @@ void main()
   vec3 n = a_Normal;
 
 #if NUM_VERTEX>0
+
+  vec3 b1;
+  float len = n.x*n.x+n.y*n.y;
+  float l = u_objD.x / 2.0;
+
+  if( len>0.0 )
+    {
+    len = sqrt(len);
+    b1 = vec3( n.y/len,-n.x/len,0.0);
+    }
+  else
+    {
+    b1 = vec3(1.0,0.0,0.0);
+    }
+
+  vec3 b2 = cross(n,b1);
+  vec3 v1 = v+b1;
+  vec3 v2 = v+b2;
+
   int j=0;
 
   for(int i=0; i<vNumEffects; i++)
     {
-         if( vType[i]==DISTORT) distort(j,v,n);
-    else if( vType[i]==DEFORM ) deform (j,v,n);
-    else if( vType[i]==SINK   ) sink   (j,v);
-    else if( vType[i]==PINCH  ) pinch  (j,v);
-    else if( vType[i]==SWIRL  ) swirl  (j,v);
-    else if( vType[i]==WAVE   ) wave   (j,v,n);
+    if( vType[i]==DISTORT)
+      {
+      distort(j,v);
+      distort(j,v1);
+      distort(j,v2);
+      }
+    else if( vType[i]==DEFORM )
+      {
+      deform (j,v);
+      deform (j,v1);
+      deform (j,v2);
+      }
+    else if( vType[i]==SINK   )
+      {
+      sink   (j,v);
+      sink   (j,v1);
+      sink   (j,v2);
+      }
+    else if( vType[i]==PINCH  )
+      {
+      pinch  (j,v);
+      pinch  (j,v1);
+      pinch  (j,v2);
+      }
+    else if( vType[i]==SWIRL  )
+      {
+      swirl  (j,v);
+      swirl  (j,v1);
+      swirl  (j,v2);
+      }
+    else if( vType[i]==WAVE   )
+      {
+      wave   (j,v);
+      wave   (j,v1);
+      wave   (j,v2);
+      }
 
     j+=3;
     }
- 
+
+  n = cross(v1-v,v2-v);
+
   restrictZ(v.z);
 #endif
    
