commit c638c1b01464719238698413a5315c6a4065dc05
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Fri Jan 6 13:46:08 2017 +0000

    Further progress with Postprocessing. Now the missing bits are:
    
    - implement Distorted.getFBO()
    - implement Distorted.clean()
    - improve compilation of DistortedPrograms so that the NUM_POSTPROCESSING and names of POSTPROCESSING effects will be #defined.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 701d6fa..7b2edcf 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -83,12 +83,22 @@ public class Distorted
   static int mainProgramH, postProgramH;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
+// private: hide this from Javadoc
 
   private Distorted()
     {
     
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static DistortedFramebuffer getFBO(int w, int h)
+    {
+    // TODO: a static factory of Framebuffers.
+
+    return null;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   static boolean isInitialized()
@@ -96,6 +106,15 @@ public class Distorted
     return (mMainProgramAttributes!=null);
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Release all internal memory caches (in particular the FBOs used as buffers for postprocessing)
+ */
+  public static void clean()
+    {
+    // TODO
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 1f2a62e..d1a88fb 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -29,6 +29,10 @@ import org.distorted.library.type.Data4D;
 import org.distorted.library.type.Data5D;
 import org.distorted.library.type.Static3D;
 
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Class containing {@link EffectTypes#LENGTH} queues, each a class derived from EffectQueue.
@@ -37,6 +41,11 @@ import org.distorted.library.type.Static3D;
  */
 public class DistortedEffects
   {
+  private static final int BYTES_PER_FLOAT   = 4; //
+  private static final int POSITION_DATA_SIZE= 2; // Size of the position data in elements
+  private static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
+  private static final FloatBuffer mQuadPositions, mQuadTexture;
+
   private static long mNextID =0;
   private long mID;
 
@@ -47,6 +56,19 @@ public class DistortedEffects
 
   private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
 
+  static
+    {
+    int dataLength = 4;
+
+    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
+    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
+
+    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
+    mQuadPositions.put(positionData).position(0);
+    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
+    mQuadTexture.put(textureData).position(0);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
     
   private void initializeEffectLists(DistortedEffects d, int flags)
@@ -121,24 +143,26 @@ public class DistortedEffects
       }
     else
       {
-      DistortedFramebuffer fbo = null;
-      // TODO : set this as output
+      DistortedFramebuffer buffer = Distorted.getFBO(df.mWidth,df.mHeight);
 
-      // render to the FBO just like above
-      /*
-      GLES20.glViewport(0, 0, fbo.mWidth, fbo.mHeight);
+      GLES20.glViewport(0, 0, buffer.mWidth, buffer.mHeight);
+      buffer.setAsOutput();
 
-      mM.send(fbo,halfInputW,halfInputH,halfZ);
+      mM.send(buffer,halfInputW,halfInputH,halfZ);
       mV.send(halfInputW,halfInputH,halfZ);
       mF.send(halfInputW,halfInputH);
 
       mesh.draw();
-      */
 
       GLES20.glUseProgram(Distorted.postProgramH);
+      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
+      buffer.setAsInput();
+      df.setAsOutput();
+      mP.send(halfInputW,halfInputH);
 
-      // apply postprocess effects
-      mP.postprocess(fbo,df);
+      GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
+      GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
+      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
       }
     }
 
diff --git a/src/main/java/org/distorted/library/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
index 7478c35..468eccd 100644
--- a/src/main/java/org/distorted/library/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
@@ -98,22 +98,6 @@ class EffectQueuePostprocess extends EffectQueue
     mTime = currTime;  
     }  
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void postprocess(DistortedFramebuffer input, DistortedFramebuffer output)
-    {
-    // 1. Set input's COLOR0 as input texture
-    // 2. Set output as output FBO
-    // 3. set Vieport
-    // 4. call send()
-    // 5. render a quad:
-    /*
-    GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[0], MeshObject.POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mMeshPositions);
-    GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[1], MeshObject.TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mMeshTexture);
-    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
-    */
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   protected void moveEffect(int index)
@@ -125,10 +109,10 @@ class EffectQueuePostprocess extends EffectQueue
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  synchronized void send(float objX, float objY)
+  synchronized void send(float halfX, float halfY)
     {
     GLES20.glUniform1i( mNumEffectsH, mNumEffects);
-    GLES20.glUniform2f( mObjDH , objX, objY);
+    GLES20.glUniform2f( mObjDH , halfX, halfY);
 
     if( mNumEffects>0 )
       {
@@ -139,10 +123,10 @@ class EffectQueuePostprocess extends EffectQueue
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  synchronized static void sendZero(float objX, float objY)
+  synchronized static void sendZero(float halfX, float halfY)
     {
     GLES20.glUniform1i( mNumEffectsH, 0);
-    GLES20.glUniform2f( mObjDH , objX, objY);
+    GLES20.glUniform2f( mObjDH , halfX, halfY);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/res/raw/post_vertex_shader.glsl b/src/main/res/raw/post_vertex_shader.glsl
index c25d32d..1f0b576 100644
--- a/src/main/res/raw/post_vertex_shader.glsl
+++ b/src/main/res/raw/post_vertex_shader.glsl
@@ -17,6 +17,9 @@
 // along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
 //////////////////////////////////////////////////////////////////////////////////////////////
 
+uniform vec2 u_objD;             // half of object width x half of object height.
+                                 // point (0,0) is the center of the object
+
 attribute vec2 a_Position;       // Per-vertex position information we will pass in.
 attribute vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
 varying   vec2 v_TexCoordinate;  //
@@ -27,5 +30,5 @@ varying   vec2 v_TexCoordinate;  //
 void main()
   {
   v_TexCoordinate = a_TexCoordinate;
-  gl_Position = vec4(a_Position, 0.0, 1.0);
+  gl_Position = vec4( 2.0*u_objD*a_Position, 0.0, 1.0);
   }
\ No newline at end of file
