commit c6ea36802eff194300ec42ad6f300d71208ea8a7
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Wed Oct 12 22:50:39 2016 +0100

    Correct one issue with incorrect shadows in WAVE when V.z <0 .

diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 8d29a22..0687852 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -374,8 +374,8 @@ void swirl(in int effect, inout vec4 v)
 // 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
 //
-// SX = (1.0+cosX*fx , cosY*sinX*fx , sinY*sinX*fx);  (**)
-// SY = (cosX*sinY*fy , 1.0+cosY*fy , sinX*sinY*fy);  (***)
+// SX = (1.0+cosX*fx , cosY*sinX*fx , |sinY|*sinX*fx);  (**)
+// SY = (cosX*sinY*fy , 1.0+cosY*fy , |sinX|*sinY*fy);  (***)
 //
 // and then obviously the normal N is given by N= SX x SY .
 //
@@ -391,7 +391,13 @@ void swirl(in int effect, inout vec4 v)
 //    - crosses plane XY along line parallel to X axis
 // c) apply the 2D analogon and notice that the tangent vector to the curve that is the common part of P
 //    and our surface (I am talking about the tangent vector which belongs to P) is given by
-//    (1+cosX*fx,0,sinX*fx) rotated by angle Y (where angles X,Y are defined above) along vector (1,0,0).
+//    (1+cosX*fx,0,sinX*fx) rotated by angle (90-|Y|) (where angles X,Y are defined above) along vector (1,0,0).
+//
+//    Matrix of rotation:
+//
+//    |sinY|  cosY
+//    -cosY  |sinY|
+//
 // d) compute the above and see that this is equal precisely to SX from (**).
 // e) repeat points b,c,d in direction Y and come up with (***).
 
@@ -426,13 +432,16 @@ 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 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,sinY*sinX*fx);
-      vec3 sy = vec3 (cosX*sinY*fy,1.0+cosY*fy,sinX*sinY*fy);
+      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 normal = cross(sx,sy);
 
