commit 57297c510d66292c27990fec0adb6ff9d4b3409d
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Sun Oct 16 23:27:18 2016 +0100

    Correct one more issue with computation of WAVE.

diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 0687852..7448638 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -361,15 +361,15 @@ void swirl(in int effect, inout vec4 v)
 //
 // Also, in this case f(x,y) = amplitude*sin(x/length), with those 2 parameters passed in uniforms.
 //
-////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////
 // How to compute any generic effect of type (*)
-////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////
 //
 // By definition, the vertices move by f(x,y)*V.
 //
 // Normals are much more complicated.
-// Let angle X be the angle (0,Vy,Vz)((0,Vy,0)(Vx,Vy,Vz).
-// Let angle Y be the angle (Vx,0,Vz)((Vx,0,0)(Vx,Vy,Vz).
+// Let angle X be the angle (0,Vy,Vz)(0,Vy,0)(Vx,Vy,Vz).
+// Let angle Y be the angle (Vx,0,Vz)(Vx,0,0)(Vx,Vy,Vz).
 //
 // Then it can be shown that the resulting surface, at point to which point (x0,y0,0) got moved to,
 // has 2 tangent vectors given by
@@ -417,7 +417,8 @@ void wave(in int effect, inout vec4 v, inout vec4 n)
     float sinB = vUniforms[effect  ].w;
     float cosB = vUniforms[effect+1].y;
 
-    float angle= 1.578*(-ps.x*cosB-ps.y*sinB) / length;  // -ps.x and -ps.y because the 'ps=center-v.xy' inverts the XY axis!
+    float angle= 1.578*(ps.x*cosB-ps.y*sinB) / length;
+
     vec3 dir   = vec3(sinB*cosA,cosB*cosA,sinA);
 
     v.xyz += sin(angle)*deg*dir;
@@ -432,22 +433,23 @@ void wave(in int effect, inout vec4 v, inout vec4 n)
       float sinY = ( sqrtX==0.0 ? 0.0 : dir.z / sqrtX);
       float cosY = ( sqrtX==0.0 ? 1.0 : dir.y / sqrtX);
 
-      float absSinX = sinX<0.0 ? -sinX:sinX;
-      float absSinY = sinY<0.0 ? -sinY:sinY;
+      float abs_z = dir.z <0.0 ? -(sinX*sinY) : (sinX*sinY);
 
       float tmp = 1.578*cos(angle)*deg/length;
 
-      float fx = cosB*tmp;
+      float fx =-cosB*tmp;
       float fy = sinB*tmp;
 
-      vec3 sx = vec3 (1.0+cosX*fx,cosY*sinX*fx,absSinY*sinX*fx);
-      vec3 sy = vec3 (cosX*sinY*fy,1.0+cosY*fy,absSinX*sinY*fy);
+      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 )
         {
-        n.xy += (n.z/normal.z)*vec2(normal.x,normal.y);
+        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);
         }
       }
     }
