commit 16d8b8f346b2263876114e71f86386ab13083154
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Tue Dec 6 12:11:40 2016 +0000

    DistortedFramebuffer should we working now, including deletions.

diff --git a/src/main/java/org/distorted/library/DistortedBitmapGrid.java b/src/main/java/org/distorted/library/DistortedBitmapGrid.java
index b2de13c..9934c24 100644
--- a/src/main/java/org/distorted/library/DistortedBitmapGrid.java
+++ b/src/main/java/org/distorted/library/DistortedBitmapGrid.java
@@ -175,7 +175,8 @@ class DistortedBitmapGrid extends DistortedObjectGrid
       //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
       //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
 
-      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
+      if( remainingVert!=0 )
+        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
 
       mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
       mGridPositions.put(positionData).position(0);
diff --git a/src/main/java/org/distorted/library/DistortedCubesGrid.java b/src/main/java/org/distorted/library/DistortedCubesGrid.java
index e7a06df..6df2e6f 100644
--- a/src/main/java/org/distorted/library/DistortedCubesGrid.java
+++ b/src/main/java/org/distorted/library/DistortedCubesGrid.java
@@ -776,7 +776,9 @@ class DistortedCubesGrid extends DistortedObjectGrid
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
      */
-     android.util.Log.d("CUBES", "remainingVert " +remainingVert );
+
+     if( remainingVert!=0 )
+       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
 
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
      mGridPositions.put(positionData).position(0);
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 07bfb11..67365ad 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -55,7 +55,13 @@ public class DistortedFramebuffer
       mDistance = (int)(mHeight*near/(top-bottom));
       far = 2*mDistance-near;
 
-      Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
+      if( far<=0 )
+        {
+        android.util.Log.e("FBO", "error: far<=0. width="+mWidth+" height="+mHeight+
+                           " mFOV="+mFOV+" mDistance="+mDistance+" far="+far+" near="+near);
+        }
+      else
+        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
       }
     else                      // parallel projection
       {
@@ -104,6 +110,8 @@ public class DistortedFramebuffer
 
     mFramebufferID = mFBORenderToTexture[0];
 
+    android.util.Log.e("FBO", "creating ("+mWidth+","+mHeight+") "+mFramebufferID);
+
     return true;
     }
 
@@ -112,6 +120,8 @@ public class DistortedFramebuffer
 
   private void deleteFBO()
     {
+    android.util.Log.e("FBO", "deleting ("+mWidth+","+mHeight+") "+mFramebufferID);
+
     int[] textureIds = new int[1];
     int[] mFBORenderToTexture = new int[1];
 
@@ -126,7 +136,7 @@ public class DistortedFramebuffer
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// must be called form a thread holding OpenGL Context
+// must be called from a thread holding OpenGL Context
 
   void release()
     {
@@ -152,7 +162,7 @@ public class DistortedFramebuffer
 
     mHeight        = height;
     mWidth         = width;
-    mFramebufferID = 0;
+    mFramebufferID = -1;
     mTextureID     = TEXTURE_NOT_CREATED_YET;
     mFOV           = 60.0f;
     mX             = 0.0f;
@@ -161,6 +171,8 @@ public class DistortedFramebuffer
     mMarked = false;
 
     createProjection();
+
+    DistortedFramebufferList.add(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -182,6 +194,8 @@ public class DistortedFramebuffer
 
   public void markForDeletion()
     {
+    android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+mFramebufferID);
+
     DistortedFramebufferList.markForDeletion();
     mMarked = true;
     }
diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index 687f4e4..ca11e73 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -114,7 +114,7 @@ public class DistortedNode
         }
 
       df.setOutput();
-      mData.mDF.setInput();   // this is safe because we must have called createFBO() above before.
+      mData.mDF.setInput();
       }
     
     mObject.drawPriv(currTime, df);
@@ -173,8 +173,23 @@ public class DistortedNode
       if( otherNodesPoint )  mData = new NodeData(++mNextNodeID);
       else                   mData.ID = ++mNextNodeID;  // numPointingNodes must be 1 already
 
-      if( newList.size()>1 && mData.mDF==null )
-        mData.mDF = new DistortedFramebuffer(mObject.getWidth(), mObject.getHeight());
+      if( newList.size()>1 )
+        {
+        if( mData.mDF==null )
+          mData.mDF = new DistortedFramebuffer(mObject.getWidth(), mObject.getHeight());
+        }
+      else
+        {
+        if( mData.mDF!=null )
+          {
+          mData.mDF.markForDeletion();
+          mData.mDF = null;
+          }
+        else
+          {
+          android.util.Log.e("DistortedNode", "adjustNodeData: impossible situation??");
+          }
+        }
 
       mMapNodeID.put(newList, mData);
       }
diff --git a/src/main/java/org/distorted/library/DistortedObject.java b/src/main/java/org/distorted/library/DistortedObject.java
index 71a12fc..1e7c7ed 100644
--- a/src/main/java/org/distorted/library/DistortedObject.java
+++ b/src/main/java/org/distorted/library/DistortedObject.java
@@ -161,6 +161,8 @@ public abstract class DistortedObject
    
   void drawPriv(long currTime, DistortedFramebuffer df)
     {
+    DistortedFramebufferList.deleteAllMarked();
+
     GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
       
     mM.compute(currTime);
@@ -275,8 +277,6 @@ public abstract class DistortedObject
     GLES20.glUniform1i(Distorted.mTextureUniformH, 0);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
 
-    DistortedFramebufferList.deleteAllMarked();
-
     drawPriv(currTime, Distorted.mFramebuffer);
     }
  
