commit 23b733db72d70c485344f21026d6335bf6851ac6
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Mar 3 22:31:39 2020 +0000

    Further corrections.

diff --git a/src/main/java/org/distorted/library/effect/VertexEffectDeform.java b/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
index 5e8bbe0..5ff45d2 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectDeform.java
@@ -113,7 +113,7 @@ public class VertexEffectDeform extends VertexEffect
       + "vec3 center = vUniforms[effect+1].yzw;                              \n"
       + "vec3 ps     = center-v;                                             \n"
       + "vec3 aPS    = abs(ps);                                              \n"
-      + "vec3 maxps  = u_objD + abs(center);                                 \n"
+      + "vec3 maxps  = u_Bounding + abs(center);                             \n"
       + "float d     = degree_region(vUniforms[effect+2],ps);                \n"
       + "vec3 force  = vUniforms[effect].xyz * d;                            \n"
       + "vec3 aForce = abs(force);                                           \n"
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
index 1fc1d2e..a51d305 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
@@ -24,6 +24,7 @@ import org.distorted.library.effect.EffectName;
 import org.distorted.library.effect.EffectType;
 import org.distorted.library.main.DistortedLibrary;
 import org.distorted.library.main.InternalMaster;
+import org.distorted.library.mesh.MeshBase;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -118,10 +119,10 @@ public abstract class EffectQueue implements InternalMaster.Slave
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  public static void send( EffectQueue[] queues, float distance, float mipmap,
-                           float[] projection, float inflate, float halfW, float halfH, float halfZ, int variant )
+  public static void send(EffectQueue[] queues, float distance, float mipmap,
+                          float[] projection, float inflate, MeshBase mesh, int variant )
     {
-    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, halfW, halfH, halfZ, variant);
+    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, mesh, variant);
     ((EffectQueueVertex  )queues[1]).send(inflate, variant);
     ((EffectQueueFragment)queues[2]).send(variant);
     }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java b/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
index 6a91eb6..40c60eb 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
@@ -24,6 +24,7 @@ import android.opengl.Matrix;
 
 import org.distorted.library.effect.EffectType;
 import org.distorted.library.effect.MatrixEffect;
+import org.distorted.library.mesh.MeshBase;
 import org.distorted.library.message.EffectMessageSender;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -36,7 +37,8 @@ class EffectQueueMatrix extends EffectQueue
   private static float[] mMVPMatrix = new float[16];
   private static float[] mViewMatrix= new float[16];
 
-  private static int[] mObjDH      = new int[MAIN_VARIANTS];
+  private static int[] mBoundingH  = new int[MAIN_VARIANTS];
+  private static int[] mStretchH   = new int[MAIN_VARIANTS];
   private static int[] mMVPMatrixH = new int[MAIN_VARIANTS];
   private static int[] mMVMatrixH  = new int[MAIN_VARIANTS];
 
@@ -56,7 +58,8 @@ class EffectQueueMatrix extends EffectQueue
 
   static void uniforms(int mProgramH, int variant)
     {
-    mObjDH[variant]     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
+    mBoundingH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_Bounding");
+    mStretchH[variant]  = GLES31.glGetUniformLocation(mProgramH, "u_Stretch");
     mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
     mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
     }
@@ -79,7 +82,7 @@ class EffectQueueMatrix extends EffectQueue
 // return a float which describes how much larger an object must be so that it appears to be (about)
 // 'marginInPixels' pixels larger in each direction. Used in Postprocessing.
 
-  float magnify(float[] projection, int width, int height, float mipmap, float halfX, float halfY, float halfZ, float marginInPixels)
+  float magnify(float[] projection, int width, int height, float mipmap, MeshBase mesh, float marginInPixels)
     {
     mMinX = Integer.MAX_VALUE;
     mMaxX = Integer.MIN_VALUE;
@@ -90,6 +93,10 @@ class EffectQueueMatrix extends EffectQueue
 
     Matrix.multiplyMM(mTmpMatrix, 0, projection, 0, mViewMatrix, 0);
 
+    float halfX = mesh.getBoundingX();
+    float halfY = mesh.getBoundingY();
+    float halfZ = mesh.getBoundingZ();
+
     mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
     mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
     mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
@@ -143,7 +150,7 @@ class EffectQueueMatrix extends EffectQueue
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void send(float distance, float mipmap, float[] projection, float halfX, float halfY, float halfZ, int variant)
+  void send(float distance, float mipmap, float[] projection, MeshBase mesh, int variant)
     {
     Matrix.setIdentityM(mViewMatrix, 0);
     Matrix.translateM(mViewMatrix, 0, 0,0, -distance);
@@ -153,7 +160,8 @@ class EffectQueueMatrix extends EffectQueue
     for(int i=mNumEffects-1; i>=0; i--) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
     Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mViewMatrix, 0);
 
-    GLES31.glUniform3f( mObjDH[variant] , halfX, halfY, halfZ);
+    GLES31.glUniform3f( mBoundingH[variant] , mesh.getBoundingX(), mesh.getBoundingY(), mesh.getBoundingZ());
+    GLES31.glUniform3f( mStretchH[variant]  , mesh.getStretchX() , mesh.getStretchY() , mesh.getStretchZ() );
     GLES31.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mViewMatrix, 0);
     GLES31.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
     }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
index 3e7d397..d01d9ac 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
@@ -161,10 +161,6 @@ public class EffectQueuePostprocess extends EffectQueue
     MeshBase mesh = node.getMesh();
     DistortedEffects effects = node.getEffects();
 
-    float halfW = mesh.getBoundingX();
-    float halfH = mesh.getBoundingY();
-    float halfZ = mesh.getBoundingZ();
-
     int width   = buffer.getWidth();
     int height  = buffer.getHeight();
 
@@ -183,11 +179,11 @@ public class EffectQueuePostprocess extends EffectQueue
 
     float inflate=0.0f;
 
-    matrix.send(distance, mipmap, projection, halfW, halfH, halfZ, 2);
+    matrix.send(distance, mipmap, projection, mesh, 2);
 
     if( mHalo!=0.0f )
       {
-      inflate = matrix.magnify(projection, width, height, mipmap, halfW, halfH, halfZ, mHalo);
+      inflate = matrix.magnify(projection, width, height, mipmap, mesh, mHalo);
       }
 
     vertex.send(inflate,2);
diff --git a/src/main/java/org/distorted/library/main/DistortedLibrary.java b/src/main/java/org/distorted/library/main/DistortedLibrary.java
index ab0af3e..629ee7c 100644
--- a/src/main/java/org/distorted/library/main/DistortedLibrary.java
+++ b/src/main/java/org/distorted/library/main/DistortedLibrary.java
@@ -441,21 +441,18 @@ public class DistortedLibrary
     GLES31.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
     mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
 
-    float halfX       = mesh.getBoundingX();
-    float halfY       = mesh.getBoundingY();
-    float halfZ       = mesh.getBoundingZ();
     float inflate     = mesh.getInflate();
     float distance    = surface.mDistance;
     float mipmap      = surface.mMipmap;
     float[] projection= surface.mProjectionMatrix;
 
-    EffectQueue.send(queues, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 1 );
+    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 1 );
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
 
     if( mesh.getShowNormals() )
       {
       DistortedLibrary.mMainProgram.useProgram();
-      EffectQueue.send(queues, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 0 );
+      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
       displayNormals(queues,mesh);
       }
     }
@@ -473,15 +470,12 @@ public class DistortedLibrary
     GLES31.glUniform1i(DistortedLibrary.mMainTextureH, 0);
     mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
 
-    float halfX       = mesh.getBoundingX();
-    float halfY       = mesh.getBoundingY();
-    float halfZ       = mesh.getBoundingZ();
     float inflate     = mesh.getInflate();
     float distance    = surface.mDistance;
     float mipmap      = surface.mMipmap;
     float[] projection= surface.mProjectionMatrix;
 
-    EffectQueue.send(queues, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 0 );
+    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
 
     if( mesh.getShowNormals() ) displayNormals(queues,mesh);
diff --git a/src/main/java/org/distorted/library/mesh/MeshBase.java b/src/main/java/org/distorted/library/mesh/MeshBase.java
index 652a679..80838f9 100644
--- a/src/main/java/org/distorted/library/mesh/MeshBase.java
+++ b/src/main/java/org/distorted/library/mesh/MeshBase.java
@@ -69,6 +69,7 @@ public abstract class MeshBase
    private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
    private float mInflate;
    private float mBoundingX, mBoundingY, mBoundingZ;
+   private float mStretchX, mStretchY, mStretchZ;
 
    private class Component
      {
@@ -106,6 +107,10 @@ public abstract class MeshBase
      mBoundingY = by/2;
      mBoundingZ = bz/2;
 
+     mStretchX = 1.0f;
+     mStretchY = 1.0f;
+     mStretchZ = 1.0f;
+
      mShowNormals = false;
      mInflate     = 0.0f;
      mComponent = new ArrayList<>();
@@ -124,6 +129,10 @@ public abstract class MeshBase
      mBoundingY = original.mBoundingY;
      mBoundingZ = original.mBoundingZ;
 
+     mStretchX = original.mStretchX;
+     mStretchY = original.mStretchY;
+     mStretchZ = original.mStretchZ;
+
      mShowNormals = original.mShowNormals;
      mInflate     = original.mInflate;
 
@@ -191,7 +200,7 @@ public abstract class MeshBase
  */
    public float getBoundingX()
     {
-    return mBoundingX;
+    return mBoundingX*mStretchX;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -200,7 +209,7 @@ public abstract class MeshBase
  */
    public float getBoundingY()
     {
-    return mBoundingY;
+    return mBoundingY*mStretchY;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -209,9 +218,62 @@ public abstract class MeshBase
  */
    public float getBoundingZ()
     {
-    return mBoundingZ;
+    return mBoundingZ*mStretchZ;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
+ * different factors in x and y (or z) directions. If we also wanted do display some vertex effects
+ * done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
+ * (i.e the same in x and y directions) then doing so without this method would be impossible.
+ *
+ * This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
+ * by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
+ * Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
+ * want to display the Mesh on, and this way we can achieve a round Distort bulge!
+ *
+ * This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
+ * this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
+ *
+ * @param sx stretch factor in x.
+ * @param sy stretch factor in y.
+ * @param sz stretch factor in z.
+ */
+   public void setStretch(float sx, float sy, float sz)
+     {
+     mStretchX = sx;
+     mStretchY = sy;
+     mStretchZ = sz;
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Returns the x-factor set by setStretch().
+ */
+   public float getStretchX()
+     {
+     return mStretchX;
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Returns the y-factor set by setStretch().
+ */
+   public float getStretchY()
+     {
+     return mStretchY;
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Returns the z-factor set by setStretch().
+ */
+   public float getStretchZ()
+     {
+     return mStretchZ;
+     }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Not part of public API, do not document (public only because has to be used from the main package)
@@ -474,34 +536,6 @@ public abstract class MeshBase
 
        }
      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// all 4 APIs depreciated - being removed from the apps. When done, we will also remove the
-// premultiply of the Object vertices by m_objD in the vertex shader.
-
-   @Deprecated
-   public void setStretch(int sx, int sy, int sz)
-     {
-     mBoundingX = sx/2.0f;
-     mBoundingY = sy/2.0f;
-     mBoundingZ = sz/2.0f;
-     }
-
-   @Deprecated
-   public float getStretchX()
-    {
-    return mBoundingX*2;
-    }
-   @Deprecated
-   public float getStretchY()
-    {
-    return mBoundingY*2;
-    }
-   @Deprecated
-   public float getStretchZ()
-    {
-    return mBoundingZ*2;
-    }
    }
 
 
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 24c0e49..592fa6e 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -31,9 +31,8 @@ out vec3 v_endPosition;              // for Transform Feedback only
 out vec3 v_Normal;                   //
 out vec2 v_TexCoordinate;            //
 
-uniform vec3 u_objD;                 // half of object width x half of object height X half the depth;
-                                     // point (0,0,0) is the center of the object
-
+uniform vec3 u_Bounding;             // MeshBase.mBounding{X,Y,Z}
+uniform vec3 u_Stretch;              // MeshBase.mStretch{X,Y,Z}
 uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
 uniform mat4 u_MVMatrix;             // the combined model/view matrix.
 uniform float u_Inflate;             // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
@@ -50,29 +49,30 @@ uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*
 //////////////////////////////////////////////////////////////////////////////////////////////
 // The trick below is the if-less version of the
 //
-// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
-// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
+// t = dx<0.0 ? (u_Bounding.x-v.x) / (u_Bounding.x-ux) : (u_Bounding.x+v.x) / (u_Bounding.x+ux);
+// h = dy<0.0 ? (u_Bounding.y-v.y) / (u_Bounding.y-uy) : (u_Bounding.y+v.y) / (u_Bounding.y+uy);
 // d = min(t,h);
 //
-// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
+// float d = min(-ps.x/(sign(ps.x)*u_Bounding.x+p.x),-ps.y/(sign(ps.y)*u_Bounding.y+p.y))+1.0;
 //
-// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
+// We still have to avoid division by 0 when p.x = +- u_Bounding.x or p.y = +- u_Bounding.y (i.e
+// on the edge of the Object).
 // We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
 //
 // 2019-01-09: make this 3D. The trick: we want only the EDGES of the cuboid to stay constant.
-// the interiors of the Faces move! Thus, we want the MIDDLE of the PS/(sign(PS)*u_objD+S) !
+// the interiors of the Faces move! Thus, we want the MIDDLE of the PS/(sign(PS)*u_Bounding+S) !
 //////////////////////////////////////////////////////////////////////////////////////////////
-// return degree of the point as defined by the object cuboid (u_objD.x X u_objD.y X u_objD.z)
+// return degree of the point as defined by the cuboid (u_Bounding.x X u_Bounding.y X u_Bounding.z)
 
 float degree_object(in vec3 S, in vec3 PS)
   {
   vec3 ONE = vec3(1.0,1.0,1.0);
-  vec3 A = sign(PS)*u_objD + S;
+  vec3 A = sign(PS)*u_Bounding + S;
 
   vec3 signA = sign(A);                      //
   vec3 signA_SQ = signA*signA;               // div = PS/A if A!=0, 0 otherwise.
   vec3 div = signA_SQ*PS/(A-(ONE-signA_SQ)); //
-  vec3 ret = sign(u_objD)-div;
+  vec3 ret = sign(u_Bounding)-div;
 
   float d1= ret.x-ret.y;
   float d2= ret.y-ret.z;
@@ -131,15 +131,15 @@ float degree_region(in vec4 region, in vec3 PS)
 float degree(in vec4 region, in vec3 S, in vec3 PS)
   {
   vec3 PO  = PS + region.xyz;
-  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  float D = region.w*region.w-dot(PO,PO);     // D = |OX|^2 - |PO|^2
 
   if( D<=0.0 ) return 0.0;
 
-  vec3 A = sign(PS)*u_objD + S;
+  vec3 A = sign(PS)*u_Bounding + S;
   vec3 signA = sign(A);
   vec3 signA_SQ = signA*signA;
   vec3 div = signA_SQ*PS/(A-(vec3(1.0,1.0,1.0)-signA_SQ));
-  vec3 ret = sign(u_objD)-div;                // if object is flat, make ret.z 0
+  vec3 ret = sign(u_Bounding)-div;            // if object is flat, make ret.z 0
 
   float d1= ret.x-ret.y;
   float d2= ret.y-ret.z;
@@ -164,10 +164,10 @@ float degree(in vec4 region, in vec3 S, in vec3 PS)
 
 void main()
   {
-  vec3 v = 2.0*u_objD*a_Position;
+  vec3 v = u_Stretch*a_Position;
   vec3 n = a_Normal;
 
-  v += (u_objD.x+u_objD.y)*u_Inflate*a_Inflate;
+  v += u_Inflate*u_Stretch*a_Inflate;
 
 #if NUM_VERTEX>0
   int effect=0;
@@ -181,7 +181,7 @@ void main()
 #endif
    
   v_Position      = v;
-  v_endPosition   = v + (0.3*u_objD.x)*n;
+  v_endPosition   = v + 0.3*u_Stretch*n;
   v_TexCoordinate = a_TexCoordinate;
   v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
   gl_Position     = u_MVPMatrix*vec4(v,1.0);
