commit 575786360a73bbe7c95d4ff3bc4425a120f9769f
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Thu Dec 8 14:13:02 2016 +0000

    Improvements for DFramebuffer.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 927939d..3d9a753 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -85,11 +85,12 @@ public class Distorted
   private static final String TAG = Distorted.class.getSimpleName();
   private static boolean mInitialized = false;
   
-  static int mPositionH;        // pass in model position information
-  static int mTextureUniformH;  // pass in the texture.
-  static int mNormalH;          // pass in model normal information.
-  static int mTextureCoordH;    // pass in model texture coordinate information.
-  static int mProgramH;         // This is a handle to our shading program.
+  private static int mProgramH;         // handle to our shading program.
+  private static int mTextureUniformH;  // the texture.
+
+  static int mPositionH;                // model position information
+  static int mNormalH;                  // model normal information.
+  static int mTextureCoordH;            // model texture coordinate information.
 
   static DistortedFramebuffer mFramebuffer = new DistortedFramebuffer(0); // output to the screen
 
@@ -325,8 +326,16 @@ public class Distorted
     final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);     
       
     mProgramH = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position",  "a_Color", "a_Normal", "a_TexCoordinate"});                                                            
-      
+
+    mFramebuffer.setProjection(60.0f,0.0f,0.0f);
+
     GLES20.glUseProgram(mProgramH);
+
+    mTextureUniformH = GLES20.glGetUniformLocation(mProgramH, "u_Texture");
+    mPositionH       = GLES20.glGetAttribLocation( mProgramH, "a_Position");
+    mNormalH         = GLES20.glGetAttribLocation( mProgramH, "a_Normal");
+    mTextureCoordH   = GLES20.glGetAttribLocation( mProgramH, "a_TexCoordinate");
+
     GLES20.glEnable (GLES20.GL_DEPTH_TEST);
     GLES20.glDepthFunc(GLES20.GL_LEQUAL);
     GLES20.glEnable(GLES20.GL_BLEND);
@@ -334,13 +343,9 @@ public class Distorted
     GLES20.glEnable(GLES20.GL_CULL_FACE);
     GLES20.glCullFace(GLES20.GL_BACK);
     GLES20.glFrontFace(GLES20.GL_CW);
+    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
+    GLES20.glUniform1i(mTextureUniformH, 0);
 
-    mTextureUniformH = GLES20.glGetUniformLocation(mProgramH, "u_Texture");
-    
-    mPositionH       = GLES20.glGetAttribLocation( mProgramH, "a_Position");
-    mNormalH         = GLES20.glGetAttribLocation( mProgramH, "a_Normal");
-    mTextureCoordH   = GLES20.glGetAttribLocation( mProgramH, "a_TexCoordinate");
-    
     EffectQueueFragment.getUniforms(mProgramH);
     EffectQueueVertex.getUniforms(mProgramH);
     EffectQueueMatrix.getUniforms(mProgramH);
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index ce6901f..19f6ad2 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -67,27 +67,27 @@ public class DistortedFramebuffer
       {
       if( mFOV>0.0f )  // perspective projection
         {
-        float left   =(-mX-mWidth/2 )/mHeight;
-        float right  =(-mX+mWidth/2 )/mHeight;
-        float bottom =(-mY-mHeight/2)/mHeight;
-        float top    =(-mY+mHeight/2)/mHeight;
-        float near= (float)( (top-bottom) / (2*Math.tan(mFOV*Math.PI/360)) );
-        mDistance = (int)(mHeight*near/(top-bottom));
-        float far = 2*mDistance-near;
-        mDepth = (int)((far-near)/2);
+        float left   = (-mX-mWidth/2 )/mHeight;
+        float right  = (-mX+mWidth/2 )/mHeight;
+        float bottom = (-mY-mHeight/2)/mHeight;
+        float top    = (-mY+mHeight/2)/mHeight;
+        float near   = (float)( (top-bottom) / (2*Math.tan(mFOV*Math.PI/360)) );
+        mDistance    = (int)(mHeight*near/(top-bottom));
+        float far    = 2*mDistance-near;
+        mDepth       = (int)((far-near)/2);
 
         Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
       else             // parallel projection
         {
-        float left   =-mX-mWidth/2;
-        float right  =-mX+mWidth/2;
-        float bottom =-mY-mHeight/2;
-        float top    =-mY+mHeight/2;
-        float near= (float)( (top-bottom) / (2*Math.tan(Math.PI/360)) );
-        mDistance = (int)(mHeight*near/(top-bottom));
-        float far = 2*mDistance-near;
-        mDepth = (int)((far-near)/2);
+        float left   = -mX-mWidth/2;
+        float right  = -mX+mWidth/2;
+        float bottom = -mY-mHeight/2;
+        float top    = -mY+mHeight/2;
+        float near   = (float)( (top-bottom) / (2*Math.tan(Math.PI/360)) );
+        mDistance    = (int)(mHeight*near/(top-bottom));
+        float far    = 2*mDistance-near;
+        mDepth       = (int)((far-near)/2);
 
         Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
@@ -193,11 +193,32 @@ public class DistortedFramebuffer
       mListMarked = false;
       }
     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Set this as the Framebuffer to write to.
+
+  void setAsOutput()
+    {
+    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
+
+    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Set this as the Framebuffer to read from.
+
+  void setAsInput()
+    {
+    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
+
+    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
- * Create a new offscreen FBO.
+ * Create a new offscreen Framebuffer.
  *
  * @param width Width of the COLOR attachment.
  * @param height Height of the COLOR attachment.
@@ -206,22 +227,23 @@ public class DistortedFramebuffer
     {
     mProjectionMatrix = new float[16];
 
-    mHeight        = height;
-    mWidth         = width;
-    fboIds[0]      = -1;
-    texIds[0]      = TEXTURE_NOT_CREATED_YET;
-    mFOV           = 60.0f;
-    mX             = 0.0f;
-    mY             = 0.0f;
-
-    mMarked = false;
+    mHeight  = height;
+    mWidth   = width;
+    fboIds[0]= -1;
+    texIds[0]= TEXTURE_NOT_CREATED_YET;
+    mFOV     = 60.0f;
+    mX       = 0.0f;
+    mY       = 0.0f;
+    mMarked  = false;
 
     createProjection();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
- * Create a new DistortedFramebuffer from an already created OpenGL Framebuffer.
+ * Create a new Framebuffer from an already created OpenGL Framebuffer.
+ * <p>
+ * Has to be followed by a 'resize()' to set the size.
  *
  * @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
  */
@@ -229,13 +251,12 @@ public class DistortedFramebuffer
     {
     mProjectionMatrix = new float[16];
 
-    fboIds[0]      = fbo;
-    texIds[0]      = TEXTURE_DONT_CREATE;
-    mFOV           = 60.0f;
-    mX             = 0.0f;
-    mY             = 0.0f;
-
-    mMarked = false;
+    fboIds[0]= fbo;
+    texIds[0]= TEXTURE_DONT_CREATE;
+    mFOV     = 60.0f;
+    mX       = 0.0f;
+    mY       = 0.0f;
+    mMarked  = false;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -292,29 +313,4 @@ public class DistortedFramebuffer
       if( texIds[0]>0 ) markForDeletion();
       }
     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Set this as the Framebuffer to write to.
- */
-
-  public void setAsOutput()
-    {
-    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
-
-    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Set this as the Framebuffer to read from.
- */
-
-  public void setAsInput()
-    {
-    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
-
-    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
-    }
-
   }
diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index b932db9..e5b850a 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -458,7 +458,6 @@ public class DistortedNode
     {  
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
-    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);  
 
     markRecursive();
     drawRecursive(currTime,Distorted.mFramebuffer);
diff --git a/src/main/java/org/distorted/library/DistortedObject.java b/src/main/java/org/distorted/library/DistortedObject.java
index 65b134a..97085e1 100644
--- a/src/main/java/org/distorted/library/DistortedObject.java
+++ b/src/main/java/org/distorted/library/DistortedObject.java
@@ -326,9 +326,8 @@ public abstract class DistortedObject
  */
   public void draw(long currTime)
     {
-    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
-    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);
+
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
 
     drawPriv(currTime, Distorted.mFramebuffer);
