commit ec231614152da2cfdb65c3450cd47da83ee05a6f
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Mon Apr 24 17:28:01 2017 +0100

    Major refactoring: convert the Matrix Effects to be independent of the resolution of the surface we render to.
    
    Re-write the first 15 apps to work with this.

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 2515a77..a011650 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -638,8 +638,12 @@ public class DistortedEffects
 /**
  * Moves the Object by a (possibly changing in time) vector.
  * 
- * @param vector 3-dimensional Data which at any given time will return a Static3D
- *               representing the current coordinates of the vector we want to move the Object with.
+ * @param vector 3-dimensional Data which at any given time will return a Static3D representing
+ *               the current coordinates of the vector we want to move the Object with.
+ *               Units: to make it independent of the dimensions of the surface we are rendering to,
+ *               the vector's unit is the size of the surface in each direction; i.e. a move(0.3,0.7,0.0)
+ *               will move our object 30% of the width of the surface to the right and 70% of its height
+ *               down.
  * @return       ID of the effect added, or -1 if we failed to add one.
  */
   public long move(Data3D vector)
@@ -653,6 +657,9 @@ public class DistortedEffects
  * 
  * @param scale 3-dimensional Data which at any given time returns a Static3D
  *              representing the current x- , y- and z- scale factors.
+ *              Example: a scale(2,1,-0.5) makes the object 2 times wider, keeps the height
+ *              unchanged, and makes it twice thinner in Z-axis shifting the originally further
+ *              wall in front of the originally closer to the camera one.
  * @return      ID of the effect added, or -1 if we failed to add one.
  */
   public long scale(Data3D scale)
@@ -675,7 +682,7 @@ public class DistortedEffects
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Rotates the Object by 'angle' degrees around the center.
- * Static axis of rotation is given by the last parameter.
+ * Static axis of rotation is given by the 2nd parameter.
  *
  * @param angle  Angle that we want to rotate the Object to. Unit: degrees
  * @param axis   Axis of rotation
@@ -720,7 +727,7 @@ public class DistortedEffects
  *
  * @param shear   The 3-tuple of shear factors. The first controls level
  *                of shearing in the X-axis, second - Y-axis and the third -
- *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
+ *                Z-axis. Each is the tangent of the shear angle, i.e 0 -
  *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
  * @param center  Center of shearing, i.e. the point which stays unmoved.
  * @return        ID of the effect added, or -1 if we failed to add one.
diff --git a/src/main/java/org/distorted/library/EffectQueueMatrix.java b/src/main/java/org/distorted/library/EffectQueueMatrix.java
index 1cd9df4..2335752 100644
--- a/src/main/java/org/distorted/library/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/EffectQueueMatrix.java
@@ -110,7 +110,7 @@ class EffectQueueMatrix extends EffectQueue
   private void constructMatrices(DistortedOutputSurface projection, float halfX, float halfY)
     {
     Matrix.setIdentityM(mViewMatrix, 0);
-    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
+    Matrix.translateM(mViewMatrix, 0, 0, 0, -projection.mDistance);
 
     float x,y,z, sx,sy,sz;
 
@@ -118,9 +118,9 @@ class EffectQueueMatrix extends EffectQueue
       {
       if (mName[i] == EffectNames.ROTATE.ordinal() )
         {
-        x = mUniforms[NUM_UNIFORMS*i+4];
-        y = mUniforms[NUM_UNIFORMS*i+5];
-        z = mUniforms[NUM_UNIFORMS*i+6];
+        x = mUniforms[NUM_UNIFORMS*i+4]*projection.mWidth;
+        y = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
+        z = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
         Matrix.rotateM( mViewMatrix, 0, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
@@ -128,9 +128,9 @@ class EffectQueueMatrix extends EffectQueue
         }
       else if(mName[i] == EffectNames.QUATERNION.ordinal() )
         {
-        x = mUniforms[NUM_UNIFORMS*i+4];
-        y = mUniforms[NUM_UNIFORMS*i+5];
-        z = mUniforms[NUM_UNIFORMS*i+6];
+        x = mUniforms[NUM_UNIFORMS*i+4]*projection.mWidth;
+        y = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
+        z = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
         multiplyByQuat(mViewMatrix, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
@@ -138,9 +138,9 @@ class EffectQueueMatrix extends EffectQueue
         }
       else if(mName[i] == EffectNames.MOVE.ordinal() )
         {
-        sx = mUniforms[NUM_UNIFORMS*i  ];
-        sy = mUniforms[NUM_UNIFORMS*i+1];
-        sz = mUniforms[NUM_UNIFORMS*i+2];
+        sx = mUniforms[NUM_UNIFORMS*i  ]*projection.mWidth;
+        sy = mUniforms[NUM_UNIFORMS*i+1]*projection.mHeight;
+        sz = mUniforms[NUM_UNIFORMS*i+2]*projection.mDistance;
 
         Matrix.translateM(mViewMatrix, 0, sx,-sy, sz);
         }
@@ -158,9 +158,9 @@ class EffectQueueMatrix extends EffectQueue
         sy = mUniforms[NUM_UNIFORMS*i+1];
         sz = mUniforms[NUM_UNIFORMS*i+2];
 
-        x  = mUniforms[NUM_UNIFORMS*i+4];
-        y  = mUniforms[NUM_UNIFORMS*i+5];
-        z  = mUniforms[NUM_UNIFORMS*i+6];
+        x  = mUniforms[NUM_UNIFORMS*i+4]*projection.mWidth;
+        y  = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
+        z  = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
 
@@ -183,7 +183,7 @@ class EffectQueueMatrix extends EffectQueue
         }
       }
 
-    Matrix.translateM(mViewMatrix, 0, halfX,-halfY, 0);
+    Matrix.scaleM(mViewMatrix, 0, projection.mWidth/(2*halfX), projection.mHeight/(2*halfY), 1);
     Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
     }
 
@@ -311,7 +311,7 @@ class EffectQueueMatrix extends EffectQueue
       mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
       mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
 
-      if( center instanceof Dynamic3D)
+      if( center instanceof Dynamic3D )
         {
         mInter[1][mNumEffects] = (Dynamic3D)center;
         }
@@ -337,15 +337,15 @@ class EffectQueueMatrix extends EffectQueue
     {
     if( mMax[INDEX]>mNumEffects )
       {
-      if( data instanceof Dynamic4D  )
+      if( data instanceof Dynamic4D )
         {
         mInter[0][mNumEffects] = (Dynamic4D)data;
         }
-      else if( data instanceof DynamicQuat)
+      else if( data instanceof DynamicQuat )
         {
         mInter[0][mNumEffects] = (DynamicQuat)data;
         }
-      else if( data instanceof Static4D   )
+      else if( data instanceof Static4D )
         {
         mInter[0][mNumEffects] = null;
         mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)data).getX();
@@ -355,7 +355,7 @@ class EffectQueueMatrix extends EffectQueue
         }
       else return -1;
 
-      if( center instanceof Dynamic3D)
+      if( center instanceof Dynamic3D )
         {
         mInter[1][mNumEffects] = (Dynamic3D)center;
         }
@@ -381,7 +381,7 @@ class EffectQueueMatrix extends EffectQueue
     {
     if( mMax[INDEX]>mNumEffects )
       {
-      if( shear instanceof Dynamic3D)
+      if( shear instanceof Dynamic3D )
         {
         mInter[0][mNumEffects] = (Dynamic3D)shear;
         }
@@ -394,7 +394,7 @@ class EffectQueueMatrix extends EffectQueue
         }
       else return -1;
 
-      if( center instanceof Dynamic3D)
+      if( center instanceof Dynamic3D )
         {
         mInter[1][mNumEffects] = (Dynamic3D)center;
         }
