commit 6e7c8721ae3bccc50aa577d10f930e968746bf29
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed Apr 26 11:08:30 2017 +0100

    Simplifications.

diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index a2e2317..a00605f 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -45,7 +45,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
       GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
       GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
       GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
-      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
+      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
 
       GLES30.glGenFramebuffers(1, mFBOH, 0);
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
@@ -61,7 +61,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
       GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
       GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
       GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
-      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
+      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
 
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
       GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 47ff599..d60d618 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -55,7 +55,6 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
   private long mTime;
   private float mFOV;
-  int mWidth,mHeight,mDepth;
   float mDistance, mNear;
   float[] mProjectionMatrix;
 
@@ -118,7 +117,6 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
         mDistance    = mHeight/a;
         float far    = 2*mDistance-near;
-        mDepth       = (int)((far-near)/2);
 
         Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
@@ -131,7 +129,6 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
         float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
         mDistance    = mWidth+mHeight;
         float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
-        mDepth       = (int)((far-near)/2);
 
         Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
@@ -365,8 +362,6 @@ if( !sNew.equals(sOld) )
       {
       mWidth = width;
       mHeight= height;
-      mSizeX = width;
-      mSizeY = height;
 
       createProjection();
 
diff --git a/src/main/java/org/distorted/library/DistortedSurface.java b/src/main/java/org/distorted/library/DistortedSurface.java
index 5185c1a..b6952e1 100644
--- a/src/main/java/org/distorted/library/DistortedSurface.java
+++ b/src/main/java/org/distorted/library/DistortedSurface.java
@@ -20,9 +20,7 @@
 package org.distorted.library;
 
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedList;
-import java.util.Map;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
@@ -68,7 +66,7 @@ abstract class DistortedSurface
   private int mType;
   int mColorCreated;
   int[] mColorH = new int[1];
-  int mSizeX, mSizeY;  // in screen space
+  int mWidth, mHeight;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -193,8 +191,8 @@ abstract class DistortedSurface
 
   DistortedSurface(int width, int height, int create, int type)
     {
-    mSizeX        = width ;
-    mSizeY        = height;
+    mWidth        = width ;
+    mHeight       = height;
     mColorCreated = create;
     mColorH[0]    = 0;
     mID           = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
@@ -247,7 +245,7 @@ abstract class DistortedSurface
  */
   public int getWidth()
     {
-    return mSizeX;
+    return mWidth;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -258,7 +256,7 @@ abstract class DistortedSurface
  */
   public int getHeight()
     {
-    return mSizeY;
+    return mHeight;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -273,6 +271,6 @@ abstract class DistortedSurface
  */
   public int getDepth(MeshObject mesh)
     {
-    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
+    return mesh==null ? 0 : (int)(mWidth*mesh.zFactor);
     }
   }
diff --git a/src/main/java/org/distorted/library/EffectQueueMatrix.java b/src/main/java/org/distorted/library/EffectQueueMatrix.java
index 1cd9df4..5723de9 100644
--- a/src/main/java/org/distorted/library/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/EffectQueueMatrix.java
@@ -47,7 +47,6 @@ class EffectQueueMatrix extends EffectQueue
   private static float[] mViewMatrix= new float[16];
 
   private static int mObjDH;      // This is a handle to half a Object dimensions
-  private static int mDepthH;     // Handle to the max Depth, i.e (farplane-nearplane)/2
   private static int mMVPMatrixH; // pass in the transformation matrix
   private static int mMVMatrixH;  // pass in the modelview matrix.
   
@@ -192,7 +191,6 @@ class EffectQueueMatrix extends EffectQueue
   static void getUniforms(int mProgramH)
     {
     mObjDH     = GLES30.glGetUniformLocation(mProgramH, "u_objD");
-    mDepthH    = GLES30.glGetUniformLocation(mProgramH, "u_Depth");
     mMVPMatrixH= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
     mMVMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix"); 
     }
@@ -256,7 +254,6 @@ class EffectQueueMatrix extends EffectQueue
     constructMatrices(projection,halfX,halfY);
 
     GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
-    GLES30.glUniform1f( mDepthH, projection.mDepth);
     GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
     GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
     }
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index b5bc402..1b53e0b 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -38,11 +38,6 @@ varying 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 float u_Depth;               // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes.
-                                     // I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n,
-                                     // .far = f, .diff = f-n so maybe u_Depth is redundant
-                                     // Update: this struct is only available in fragment shaders
-
 uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
 uniform mat4 u_MVMatrix;             // the combined model/view matrix.
 
@@ -147,23 +142,6 @@ float degree(in vec4 region, in vec2 S, in vec2 PS)
   return min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT)),E);
   }
 
-//////////////////////////////////////////////////////////////////////////////////////////////
-// Clamp v.z to (-u_Depth,u_Depth) with the following function:
-// define h to be, say, 0.7; let H=u_Depth
-//      if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H   (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H)
-// else if v.z >  hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H   (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H)
-// else v.z = v.z
-
-void restrictZ(inout float v)
-  {
-  const float h = 0.7;
-  float signV = 2.0*max(0.0,sign(v))-1.0;
-  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
-  float b = max(0.0,sign(abs(v)-h*u_Depth));
-
-  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
-  }
-
 //////////////////////////////////////////////////////////////////////////////////////////////
 // DEFORM EFFECT
 //
@@ -575,8 +553,6 @@ void main()
 
     j+=3;
     }
-
-  restrictZ(v.z);
 #endif
    
   v_Position      = v;
