commit 58059374c4ec9b453468b44a3c2c25b9aabfacd4
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Fri Jan 27 21:42:28 2017 +0000

    Beginnings of the 'Multiblur' app.

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index fc46ec7..b0afc56 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -44,5 +44,6 @@
         <activity android:name=".aroundtheworld.AroundTheWorldActivity"/>
         <activity android:name=".mirror.MirrorActivity"/>
         <activity android:name=".blur.BlurActivity"/>
+        <activity android:name=".multiblur.MultiblurActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index 6484d93..9012fd8 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -62,6 +62,7 @@ import org.distorted.examples.flag.FlagActivity;
 import org.distorted.examples.wind.WindActivity;
 import org.distorted.examples.mirror.MirrorActivity;
 import org.distorted.examples.blur.BlurActivity;
+import org.distorted.examples.multiblur.MultiblurActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -336,6 +337,15 @@ public class TableOfContents extends ListActivity
       activityMapping.put(i++, BlurActivity.class);
    }
 
+   {
+      final Map<String, Object> item = new HashMap<>();
+      item.put(ITEM_IMAGE, R.drawable.icon_example_multiblur);
+      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_multiblur));
+      item.put(ITEM_SUBTITLE, getText(R.string.example_multiblur_subtitle));
+      data.add(item);
+      activityMapping.put(i++, MultiblurActivity.class);
+   }
+
    final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
    setListAdapter(dataAdapter);  
       
diff --git a/src/main/java/org/distorted/examples/multiblur/MultiblurActivity.java b/src/main/java/org/distorted/examples/multiblur/MultiblurActivity.java
new file mode 100644
index 0000000..a745926
--- /dev/null
+++ b/src/main/java/org/distorted/examples/multiblur/MultiblurActivity.java
@@ -0,0 +1,68 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.multiblur;
+
+import android.app.Activity;
+import android.os.Bundle;
+import org.distorted.library.Distorted;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class MultiblurActivity extends Activity
+{
+    private MultiblurSurfaceView mView;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    @Override
+    protected void onCreate(Bundle savedState) 
+      {
+      super.onCreate(savedState);
+      mView = new MultiblurSurfaceView(this);
+      setContentView(mView);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      mView.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      mView.onResume();
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      Distorted.onDestroy();  
+      super.onDestroy();
+      }
+}
diff --git a/src/main/java/org/distorted/examples/multiblur/MultiblurRenderer.java b/src/main/java/org/distorted/examples/multiblur/MultiblurRenderer.java
new file mode 100644
index 0000000..a9bf53e
--- /dev/null
+++ b/src/main/java/org/distorted/examples/multiblur/MultiblurRenderer.java
@@ -0,0 +1,184 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.multiblur;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.opengl.GLES30;
+import android.opengl.GLSurfaceView;
+
+import org.distorted.examples.R;
+import org.distorted.library.Distorted;
+import org.distorted.library.DistortedEffects;
+import org.distorted.library.DistortedFramebuffer;
+import org.distorted.library.DistortedTexture;
+import org.distorted.library.EffectTypes;
+import org.distorted.library.MeshCubes;
+import org.distorted.library.MeshObject;
+import org.distorted.library.type.DynamicQuat;
+import org.distorted.library.type.Static3D;
+import org.distorted.library.type.Static4D;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class MultiblurRenderer implements GLSurfaceView.Renderer
+{
+    private static final int[] MOVE_VEC =
+        {
+        +1,+1,+1,
+        +1,-1,+1,
+        +1,+1,-1,
+        +1,-1,-1,
+        -1,+1,+1,
+        -1,-1,+1,
+        -1,+1,-1,
+        -1,-1,-1
+        };
+
+    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
+
+    private GLSurfaceView mView;
+    private DistortedTexture mTex1, mTex2;
+    private DistortedEffects[] mEffects;
+    private Static3D[] mMoveVector;
+    private MeshObject mMesh;
+    private DistortedFramebuffer mScreen;
+    private DynamicQuat mQuatInt1, mQuatInt2;
+    private int mObjWidth, mObjHeight;
+
+    Static4D mQuat1, mQuat2;
+    int mScreenMin;
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    MultiblurRenderer(GLSurfaceView v)
+      {
+      mView = v;
+
+      mMoveVector = new Static3D[NUM_OBJECTS];
+      for(int i=0; i<NUM_OBJECTS; i++) mMoveVector[i] = new Static3D(0,0,0);
+
+      mEffects = new DistortedEffects[NUM_OBJECTS];
+      for(int i=0; i<NUM_OBJECTS; i++) mEffects[i] = new DistortedEffects();
+
+      mMesh = new MeshCubes(1,1,false);
+
+      mTex1 = new DistortedTexture(100,100);
+      mTex2 = new DistortedTexture(100,100);
+
+      mObjWidth = mTex1.getWidth();
+      mObjHeight= mTex2.getHeight();
+
+      mQuat1 = new Static4D(0,0,0,1);  // unity
+      mQuat2 = new Static4D(0,0,0,1);  // quaternions
+      
+      mQuatInt1 = new DynamicQuat(0,0.5f);
+      mQuatInt2 = new DynamicQuat(0,0.5f);
+
+      mQuatInt1.add(mQuat1);
+      mQuatInt2.add(mQuat2);
+
+      mScreen = new DistortedFramebuffer(0);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public void onDrawFrame(GL10 glUnused) 
+      {
+      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
+
+      long time = System.currentTimeMillis();
+
+      for(int i=0; i<NUM_OBJECTS; i++)
+        mScreen.renderTo( i<NUM_OBJECTS/2 ? mTex1:mTex2, mMesh, mEffects[i], time );
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
+      {
+      mScreenMin = width<height ? width:height;
+    	
+      Static3D center = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, 0.0f );
+      float factor = Math.min((0.15f*height)/mObjHeight,(0.15f*width )/mObjWidth);
+      float xMove  =(width -factor*mObjWidth )/2;
+      float yMove  =(height-factor*mObjHeight)/2;
+      float size   = 1.3f*mObjWidth;
+
+      for(int i=0; i<NUM_OBJECTS; i++)
+        {
+        mEffects[i].abortEffects(EffectTypes.MATRIX);
+
+        mMoveVector[i].set(xMove,yMove,0);
+        mEffects[i].move(mMoveVector[i]);
+        mEffects[i].scale(factor);
+        mEffects[i].quaternion(mQuatInt1, center);
+        mEffects[i].quaternion(mQuatInt2, center);
+        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
+        mEffects[i].move(mMoveVector[i]);
+        }
+
+      mScreen.resize(width, height);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
+      {
+      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
+      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
+      Bitmap bitmap1,bitmap2;
+        
+      try 
+        {
+        bitmap1 = BitmapFactory.decodeStream(is1);
+        bitmap2 = BitmapFactory.decodeStream(is2);
+        }
+      finally 
+        {
+        try 
+          {
+          is1.close();
+          is2.close();
+          }
+        catch(IOException e) { }
+        }  
+
+      mTex1.setTexture(bitmap1);
+      mTex2.setTexture(bitmap2);
+
+      try
+        {
+        Distorted.onCreate(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("Multiblur", ex.getMessage() );
+        }
+      }
+}
diff --git a/src/main/java/org/distorted/examples/multiblur/MultiblurSurfaceView.java b/src/main/java/org/distorted/examples/multiblur/MultiblurSurfaceView.java
new file mode 100644
index 0000000..f38f82a
--- /dev/null
+++ b/src/main/java/org/distorted/examples/multiblur/MultiblurSurfaceView.java
@@ -0,0 +1,133 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.examples.multiblur;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.pm.ConfigurationInfo;
+import android.opengl.GLSurfaceView;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.widget.Toast;
+
+import org.distorted.examples.R;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class MultiblurSurfaceView extends GLSurfaceView
+{
+    private int mX, mY;
+    private MultiblurRenderer mRenderer;
+	
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public MultiblurSurfaceView(Context context)
+      {
+      super(context);
+    
+      mX = -1;
+      mY = -1;
+      
+      if(!isInEditMode())
+        {
+        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
+        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
+        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
+        mRenderer = new MultiblurRenderer(this);
+        setRenderer(mRenderer);
+        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
+        }
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override public boolean onTouchEvent(MotionEvent event) 
+      {
+      int action = event.getAction();
+      int x = (int)event.getX();
+      int y = (int)event.getY();
+           
+      switch(action)
+         {
+         case MotionEvent.ACTION_DOWN: mX = x;
+                                       mY = y;
+                                       break;
+                                       
+         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
+                                         {
+                                         float px = mY-y;
+                                         float py = mX-x;
+                                         float pz = 0;
+                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
+                                         
+                                         if( plen>0 )
+                                           {
+                                           px /= plen;
+                                           py /= plen;
+                                           pz /= plen;
+
+                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
+                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
+                                         
+                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
+                                           }
+                                         }                             
+                                       break;
+                                       
+         case MotionEvent.ACTION_UP  : mX = -1;
+                                       mY = -1;
+        	                           
+                                       float qx = mRenderer.mQuat1.getX();
+                                       float qy = mRenderer.mQuat1.getY();
+                                       float qz = mRenderer.mQuat1.getZ();
+                                       float qw = mRenderer.mQuat1.getW();
+
+                                       float rx = mRenderer.mQuat2.getX();
+                                       float ry = mRenderer.mQuat2.getY();
+                                       float rz = mRenderer.mQuat2.getZ();
+                                       float rw = mRenderer.mQuat2.getW();
+
+                                       // This is quaternion multiplication. (tx,ty,tz,tw)
+                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
+                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
+                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
+                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
+                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
+
+                                       // The point of this is so that there are always
+                                       // exactly 2 quaternions: Quat1 representing the rotation
+                                       // accumulating only since the last screen touch, and Quat2
+                                       // which remembers the combined effect of all previous
+                                       // swipes.
+                                       // We cannot be accumulating an ever-growing list of quaternions
+                                       // and add a new one every time user swipes the screen - there
+                                       // is a limited number of slots in the EffectQueueMatrix!
+                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
+                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
+                                       
+                                       break;
+         }
+             
+      return true;
+      }
+         
+}
+
diff --git a/src/main/res/drawable-hdpi/icon_example_multiblur.png b/src/main/res/drawable-hdpi/icon_example_multiblur.png
new file mode 100644
index 0000000..4b3580d
Binary files /dev/null and b/src/main/res/drawable-hdpi/icon_example_multiblur.png differ
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 346851f..75b95d0 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -133,6 +133,8 @@
     <string name="example_mirror_subtitle">Ping-pong between offscreen buffers to achieve the \'infinite mirror\' effect.</string>
     <string name="example_blur">Blur</string>
     <string name="example_blur_subtitle">Postprocessing effect: Blur.</string>
+    <string name="example_multiblur">Multiblur</string>
+    <string name="example_multiblur_subtitle">Blur multiple objects which obstruct each other.</string>
 
     <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
     <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>
