commit 9ea4f88fac604c01ec342a2371813e1979d12d38
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Wed Aug 31 23:39:22 2016 +0100

    Better normals in DISTORT

diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 3c68636..5402707 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -301,7 +301,31 @@ void distort(in int effect, inout vec4 v, inout vec4 n)
   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;
+
+  float l= dot(ps,ps);
+  float psx,psy;
+
+  if( l==0.0 )
+    {
+    psx = 0.0;
+    psy = 0.0;
+    }
+  else
+    {
+    float len = sqrt(l);
+    psx = ps.x / len;
+    psy = ps.y / len;
+    }
+
+  float e = b*b*l;
+  float cosA = sqrt(1.0/(1.0+e));
+  float sinA = sign(uz)*sqrt(e)*cosA;
+  float t1 = sinA*n.z;
+  float t2 = (1.0-cosA)*(psy*n.x-psx*n.y);
+
+  n.x = cosA*n.x - t1*psx + t2*psy;
+  n.y = cosA*n.y - t1*psy - t2*psx;
+  n.z = cosA*n.z + sinA*(psy*n.y + psx*n.x);
   }
  
 //////////////////////////////////////////////////////////////////////////////////////////////
@@ -348,7 +372,7 @@ void swirl(in int effect, inout vec4 v)
 //
 // Directional sinusoidal wave effect.
 
-void wave(in int effect, inout vec4 v)
+void wave(in int effect, inout vec4 v, inout vec4 n)
   {
   vec2 center     = vUniforms[effect+1].zw;
   float sinA      = vUniforms[effect  ].y;
@@ -359,13 +383,13 @@ void wave(in int effect, inout vec4 v)
   float length    = vUniforms[effect  ].z;
 
   vec2 ps = center-v.xy;
-  float deg = degree_region(vUniforms[effect+2],ps);
-  float d = -ps.x*cosB-ps.y*sinB;
-  float num = length==0.0 ? 0.0 : d / length;
-
-  vec3 dv = sin(1.578*num)*amplitude*deg*vec3(sinB*cosA,cosB*cosA,sinA);
+  float deg = amplitude*degree_region(vUniforms[effect+2],ps);
 
-  v.xyz += dv;
+  if( deg != 0.0 )
+    {
+    float angle = length==0.0 ? 0.0 : 1.578*(ps.x*cosB+ps.y*sinB) / length;
+    v.xyz += sin(angle)*deg*vec3(sinB*cosA,cosB*cosA,sinA);
+    }
   }
 
 #endif
@@ -384,7 +408,7 @@ void main()
     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]==WAVE   ) wave   (3*i,v);
+    else if( vType[i]==WAVE   ) wave   (3*i,v,n);
     }
  
   restrict(v.z);  
