commit c90b9e010a29699dba82776f058b93fa10459f14
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed Jan 18 16:01:00 2017 +0000

    Progress with getting Mesh'es bounding rectangle. Still doesn't quite work yet (visible : 'Cubes' app with a 0111 Mesh)

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 452eb4f..873cd35 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -113,28 +113,6 @@ public class Distorted
     {
     final Resources resources = context.getResources();
 
-    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
-    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
-
-    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + DistortedEffects.getMaxVertex()+"\n");
-
-    for(EffectNames name: EffectNames.values() )
-      {
-      if( name.getType()== EffectTypes.VERTEX)
-        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
-      }
-
-    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + DistortedEffects.getMaxFragment()+"\n");
-
-    for(EffectNames name: EffectNames.values() )
-      {
-      if( name.getType()== EffectTypes.FRAGMENT)
-        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
-      }
-
-    DistortedProgram mainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
-    int mainProgramH = mainProgram.getProgramHandle();
-
     GLES20.glDepthFunc(GLES20.GL_LEQUAL);
     GLES20.glEnable(GLES20.GL_BLEND);
     GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
@@ -142,31 +120,13 @@ public class Distorted
     GLES20.glCullFace(GLES20.GL_BACK);
     GLES20.glFrontFace(GLES20.GL_CW);
 
-    EffectQueueFragment.getUniforms(mainProgramH);
-    EffectQueueVertex.getUniforms(mainProgramH);
-    EffectQueueMatrix.getUniforms(mainProgramH);
-    DistortedTexture.getUniforms(mainProgramH);
+    DistortedEffects.createProgram(resources);
+    EffectQueuePostprocess.createProgram(resources);
 
-    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
-    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
-
-    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
-
-    for(EffectNames name: EffectNames.values() )
-      {
-      if( name.getType()== EffectTypes.POSTPROCESS)
-        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
-      }
-
-    DistortedProgram postProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
-    int postProgramH = postProgram.getProgramHandle();
-
-    EffectQueuePostprocess.getUniforms(postProgramH);
-
-    DistortedEffects.setProgram(mainProgram);
-    EffectQueuePostprocess.setProgram(postProgram);
     DistortedTree.reset();
     EffectMessageSender.startSending();
+
+    mInitialized = true;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 83b2bd9..b4decac 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -19,11 +19,17 @@
 
 package org.distorted.library;
 
+import android.content.res.Resources;
 import android.opengl.GLES20;
 import android.opengl.Matrix;
 
 import org.distorted.library.message.EffectListener;
 import org.distorted.library.program.DistortedProgram;
+import org.distorted.library.program.FragmentCompilationException;
+import org.distorted.library.program.FragmentUniformsException;
+import org.distorted.library.program.LinkingException;
+import org.distorted.library.program.VertexCompilationException;
+import org.distorted.library.program.VertexUniformsException;
 import org.distorted.library.type.Data1D;
 import org.distorted.library.type.Data2D;
 import org.distorted.library.type.Data3D;
@@ -31,6 +37,7 @@ import org.distorted.library.type.Data4D;
 import org.distorted.library.type.Data5D;
 import org.distorted.library.type.Static3D;
 
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
@@ -49,6 +56,21 @@ public class DistortedEffects
 
   private static DistortedProgram mProgram;
 
+  /// DEBUG ONLY /////
+  private static DistortedProgram mDebugProgram;
+  private static final FloatBuffer mQuadPositions;
+
+  static
+    {
+    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
+    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
+    mQuadPositions.put(positionData).position(0);
+    }
+
+  private static int mObjDH;
+  private static int mMVPMatrixH;
+  /// END DEBUG //////
+
   private static float[] mMVPMatrix = new float[16];
   private static float[] mTmpMatrix = new float[16];
 
@@ -66,9 +88,46 @@ public class DistortedEffects
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  static void setProgram(DistortedProgram p)
+  static void createProgram(Resources resources)
+  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
     {
-    mProgram = p;
+    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
+    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
+
+    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + getMaxVertex()+"\n");
+
+    for(EffectNames name: EffectNames.values() )
+      {
+      if( name.getType()== EffectTypes.VERTEX)
+        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
+      }
+
+    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + getMaxFragment()+"\n");
+
+    for(EffectNames name: EffectNames.values() )
+      {
+      if( name.getType()== EffectTypes.FRAGMENT)
+        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
+      }
+
+    mProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
+
+    int mainProgramH = mProgram.getProgramHandle();
+    EffectQueueFragment.getUniforms(mainProgramH);
+    EffectQueueVertex.getUniforms(mainProgramH);
+    EffectQueueMatrix.getUniforms(mainProgramH);
+    DistortedTexture.getUniforms(mainProgramH);
+
+    // DEBUG ONLY //////////////////////////////////////
+    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
+    final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
+
+    mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, "#version 100\n", "#version 100\n");
+
+    int debugProgramH = mDebugProgram.getProgramHandle();
+    mObjDH      = GLES20.glGetUniformLocation( debugProgramH, "u_objD");
+    mMVPMatrixH = GLES20.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
+    // END DEBUG  //////////////////////////////////////
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -121,7 +180,62 @@ public class DistortedEffects
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-   
+// DEBUG ONLY
+
+  private void displayBoundingRect(float halfX, float halfY, float halfZ, DistortedFramebuffer df, float[] mvp, float[] vertices)
+    {
+    int len  = vertices.length/3;
+    int minx = Integer.MAX_VALUE;
+    int maxx = Integer.MIN_VALUE;
+    int miny = Integer.MAX_VALUE;
+    int maxy = Integer.MIN_VALUE;
+    int wx,wy;
+
+    float x,y,z, X,Y,W, ndcX,ndcY;
+
+    for(int i=0; i<len; i++)
+      {
+      x = 2*halfX*vertices[3*i  ];
+      y = 2*halfY*vertices[3*i+1];
+      z = 2*halfZ*vertices[3*i+2];
+
+      X = mvp[ 0]*x + mvp[ 4]*y + mvp[ 8]*z + mvp[12];
+      Y = mvp[ 1]*x + mvp[ 5]*y + mvp[ 9]*z + mvp[13];
+      W = mvp[ 3]*x + mvp[ 7]*y + mvp[11]*z + mvp[15];
+
+      ndcX = X/W;
+      ndcY = Y/W;
+
+      wx = (int)(df.mWidth *(ndcX+1)/2);
+      wy = (int)(df.mHeight*(ndcY+1)/2);
+
+      if( wx<minx ) minx = wx;
+      if( wx>maxx ) maxx = wx;
+      if( wy<miny ) miny = wy;
+      if( wy>maxy ) maxy = wy;
+      }
+
+    mDebugProgram.useProgram();
+    df.setAsOutput();
+
+    Matrix.setIdentityM(mTmpMatrix, 0);
+    Matrix.translateM(mTmpMatrix, 0, -df.mWidth/2, df.mHeight/2, -df.mDistance);
+
+    Matrix.translateM(mTmpMatrix, 0, minx, -df.mHeight+maxy, 0.0f);
+    Matrix.scaleM(mTmpMatrix, 0, (float)(maxx-minx)/(2*halfX), (float)(maxy-miny)/(2*halfY), 1.0f);
+
+    Matrix.translateM(mTmpMatrix, 0, halfX,-halfY, 0);
+    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
+
+    GLES20.glUniform2f( mObjDH , 2*halfX, 2*halfY);
+    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
+
+    GLES20.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES20.GL_FLOAT, false, 0, mQuadPositions);
+    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
   void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df, long currTime)
     {
     mM.compute(currTime);
@@ -174,6 +288,10 @@ public class DistortedEffects
         mP.render(df.mWidth, df.mHeight, mMVPMatrix, df);
         }
       }
+
+    /// DEBUG ONLY //////
+    displayBoundingRect(halfInputH, halfInputW, halfZ, df, mM.getMVP(), mesh.getBoundingVertices() );
+    /// END DEBUG ///////
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
index b347a74..c163ba6 100644
--- a/src/main/java/org/distorted/library/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
@@ -19,10 +19,16 @@
 
 package org.distorted.library;
 
+import android.content.res.Resources;
 import android.opengl.GLES20;
 
 import org.distorted.library.message.EffectMessage;
 import org.distorted.library.program.DistortedProgram;
+import org.distorted.library.program.FragmentCompilationException;
+import org.distorted.library.program.FragmentUniformsException;
+import org.distorted.library.program.LinkingException;
+import org.distorted.library.program.VertexCompilationException;
+import org.distorted.library.program.VertexUniformsException;
 import org.distorted.library.type.Data1D;
 import org.distorted.library.type.Data2D;
 import org.distorted.library.type.Dynamic1D;
@@ -30,6 +36,7 @@ import org.distorted.library.type.Dynamic2D;
 import org.distorted.library.type.Static1D;
 import org.distorted.library.type.Static2D;
 
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
@@ -75,6 +82,28 @@ class EffectQueuePostprocess extends EffectQueue
     super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static void createProgram(Resources resources)
+  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
+    {
+    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
+    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
+
+    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
+
+    for(EffectNames name: EffectNames.values() )
+      {
+      if( name.getType()== EffectTypes.POSTPROCESS)
+        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
+      }
+
+    mProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
+
+    int postProgramH = mProgram.getProgramHandle();
+    getUniforms(postProgramH);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   static void getUniforms(int mProgramH)
@@ -86,13 +115,6 @@ class EffectQueuePostprocess extends EffectQueue
     mMVPMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  static void setProgram(DistortedProgram p)
-    {
-    mProgram = p;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
   
   synchronized void compute(long currTime) 
diff --git a/src/main/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index 246e846..b5d9085 100644
--- a/src/main/res/raw/main_fragment_shader.glsl
+++ b/src/main/res/raw/main_fragment_shader.glsl
@@ -105,5 +105,5 @@ void main()
     }
 #endif
 
-  gl_FragColor = vec4(pixel.rgb * v_Normal.z, pixel.a);
+  gl_FragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/test_fragment_shader.glsl b/src/main/res/raw/test_fragment_shader.glsl
new file mode 100644
index 0000000..b107bb2
--- /dev/null
+++ b/src/main/res/raw/test_fragment_shader.glsl
@@ -0,0 +1,27 @@
+//////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                          //
+//                                                                                          //
+// This file is part of Distorted.                                                          //
+//                                                                                          //
+// Distorted is free software: you can redistribute it and/or modify                        //
+// it under the terms of the GNU General Public License as published by                     //
+// the Free Software Foundation, either version 2 of the License, or                        //
+// (at your option) any later version.                                                      //
+//                                                                                          //
+// Distorted is distributed in the hope that it will be useful,                             //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
+// GNU General Public License for more details.                                             //
+//                                                                                          //
+// You should have received a copy of the GNU General Public License                        //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+precision lowp float;
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+void main()
+  {
+  gl_FragColor = vec4(1.0,0.0,0.0,0.2);
+  }
\ No newline at end of file
diff --git a/src/main/res/raw/test_vertex_shader.glsl b/src/main/res/raw/test_vertex_shader.glsl
new file mode 100644
index 0000000..8670197
--- /dev/null
+++ b/src/main/res/raw/test_vertex_shader.glsl
@@ -0,0 +1,31 @@
+//////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                          //
+//                                                                                          //
+// This file is part of Distorted.                                                          //
+//                                                                                          //
+// Distorted is free software: you can redistribute it and/or modify                        //
+// it under the terms of the GNU General Public License as published by                     //
+// the Free Software Foundation, either version 2 of the License, or                        //
+// (at your option) any later version.                                                      //
+//                                                                                          //
+// Distorted is distributed in the hope that it will be useful,                             //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
+// GNU General Public License for more details.                                             //
+//                                                                                          //
+// You should have received a copy of the GNU General Public License                        // 
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+uniform vec2 u_objD;             // object width X object height.
+                                 // point (0,0) is the center of the object
+uniform mat4 u_MVPMatrix;        // the combined model/view/projection matrix.
+
+attribute vec2 a_Position;       // Per-vertex position information we will pass in.
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+void main()
+  {
+  gl_Position = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
+  }
\ No newline at end of file
