commit 54fe333a37844f9e61ac2905f77818922029618c
Author: leszek <leszek@koltunski.pl>
Date:   Mon Feb 27 23:06:02 2017 +0000

    Fixes for z-fighting.

diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 5150b44..6fee3f2 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -32,7 +32,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
   private ArrayList<DistortedNode> mChildren;
   private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
 
-  private float mX, mY, mFOV;
+  private float mNear, mFOV;
   int mWidth,mHeight,mDepth;
   float mDistance;
   float[] mProjectionMatrix;
@@ -53,8 +53,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     mHeight= height;
 
     mFOV = 60.0f;
-    mX   =  0.0f;
-    mY   =  0.0f;
+    mNear=  0.5f;
 
     mDepthCreated= createDepth;
     mFBOH[0]     = fbo;
@@ -71,12 +70,17 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
       {
       if( mFOV>0.0f )  // perspective projection
         {
-        float left   = (-mX-mWidth /2.0f)/mHeight;
-        float right  = (-mX+mWidth /2.0f)/mHeight;
-        float bottom = (-mY-mHeight/2.0f)/mHeight;
-        float top    = (-mY+mHeight/2.0f)/mHeight;
-        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
-        mDistance    = mHeight*near/(top-bottom);
+        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
+        float q = (float)mWidth/mHeight;
+        float c = mHeight*mNear;
+
+        float left   = -q*c/2;
+        float right  = +q*c/2;
+        float bottom =   -c/2;
+        float top    =   +c/2;
+        float near   =    c/a;
+
+        mDistance    = mHeight/a;
         float far    = 2*mDistance-near;
         mDepth       = (int)((far-near)/2);
 
@@ -84,14 +88,14 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
         }
       else             // parallel projection
         {
-        float left   = -mX-mWidth /2.0f;
-        float right  = -mX+mWidth /2.0f;
-        float bottom = -mY-mHeight/2.0f;
-        float top    = -mY+mHeight/2.0f;
-        float near   = (mWidth+mHeight)/2;
-        mDistance    = 2*near;
-        float far    = 3*near;
-        mDepth       = (int)near;
+        float left   = -mWidth /2.0f;
+        float right  = +mWidth /2.0f;
+        float bottom = -mHeight/2.0f;
+        float top    = +mHeight/2.0f;
+        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);
         }
@@ -170,14 +174,15 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
  * Create new Projection matrix.
  *
  * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
- * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
- * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
+ *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
+ * @param near Distance between the screen plane and the near plane.
+ *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
+ *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
  */
-  public void setProjection(float fov, float x, float y)
+  public void setProjection(float fov, float near)
     {
-    mFOV = fov;
-    mX = x;
-    mY = y;
+    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
+    if( near<   1.0f && near> 0.0f ) mNear= near;
 
     createProjection();
     }
