commit 3a91bfe199a6b71c56cfba621502795821237d25
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Tue May 30 16:27:16 2017 +0100

    Progress with GLOW.
    
    Serious bug sorting Surface's children into postprocessing Buckets detected.

diff --git a/src/main/java/org/distorted/examples/glow/GlowRenderer.java b/src/main/java/org/distorted/examples/glow/GlowRenderer.java
index f6c6b14..c611aca 100644
--- a/src/main/java/org/distorted/examples/glow/GlowRenderer.java
+++ b/src/main/java/org/distorted/examples/glow/GlowRenderer.java
@@ -26,6 +26,7 @@ import android.opengl.GLSurfaceView;
 import org.distorted.examples.R;
 import org.distorted.library.Distorted;
 import org.distorted.library.DistortedEffects;
+import org.distorted.library.DistortedEffectsPostprocess;
 import org.distorted.library.DistortedNode;
 import org.distorted.library.DistortedScreen;
 import org.distorted.library.DistortedTexture;
@@ -33,9 +34,13 @@ import org.distorted.library.EffectNames;
 import org.distorted.library.EffectTypes;
 import org.distorted.library.MeshFlat;
 import org.distorted.library.MeshObject;
+import org.distorted.library.message.EffectListener;
+import org.distorted.library.message.EffectMessage;
 import org.distorted.library.type.Dynamic1D;
+import org.distorted.library.type.Dynamic4D;
 import org.distorted.library.type.Static1D;
 import org.distorted.library.type.Static3D;
+import org.distorted.library.type.Static4D;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,7 +50,7 @@ import javax.microedition.khronos.opengles.GL10;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-class GlowRenderer implements GLSurfaceView.Renderer
+class GlowRenderer implements GLSurfaceView.Renderer,EffectListener
 {
    private static final int[] colors  = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0,  1,1,1}; // blue, black, red, yellow, green, white
    private static final int LEAF_SIZE = 100;
@@ -55,7 +60,9 @@ class GlowRenderer implements GLSurfaceView.Renderer
    private DistortedNode mRoot;
    private DistortedTexture mLeaf;
    private DistortedScreen mScreen;
+   private DistortedEffectsPostprocess[] mLeafGlow = new DistortedEffectsPostprocess[NUM_LEAVES];
    private int mRootW, mRootH;
+   private int mGlowing;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -80,87 +87,130 @@ class GlowRenderer implements GLSurfaceView.Renderer
 
       for(int j=0; j<NUM_LEAVES; j++)
         {
+        mLeafGlow[j] = new DistortedEffectsPostprocess();
+        mLeafGlow[j].registerForMessages(this);
+
         leafEffects[j] = new DistortedEffects();
         leafEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
         leafEffects[j].move(moveVector);
         leafEffects[j].chroma( chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2]) );
-
-        mRoot.attach( mLeaf, leafEffects[j], mesh);
+        DistortedNode node = new DistortedNode( mLeaf, leafEffects[j], mesh);
+        node.setPostprocessEffects(mLeafGlow[j]);
+        mRoot.attach(node);
         }
 
+      makeGlow(0);
+
       mScreen = new DistortedScreen(mView);
       mScreen.attach(mRoot);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-   
-    public void onDrawFrame(GL10 glUnused) 
-      {
-      mScreen.render(System.currentTimeMillis());
-      }
+
+   private void makeGlow(int leaf)
+     {
+     //android.util.Log.e("glow", "glowing: "+leaf);
+
+     mGlowing = leaf;
+
+     Dynamic1D radius = new Dynamic1D(5000,1.0f);
+     Static1D startR  = new Static1D( 0);
+     Static1D endR    = new Static1D(50);
+     radius.add(startR);
+     radius.add(endR);
+
+     Dynamic4D color  = new Dynamic4D(5000,1.0f);
+     Static4D startC  = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0);
+     Static4D endC    = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
+     color.add(startC);
+     color.add(endC);
+
+     mLeafGlow[leaf].glow( radius, color );
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-    
-    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
-      {
-      float qw = (float)width /mRootW;
-      float qh = (float)height/mRootH;
-      float factor = 0.6f* (qw<qh ? qw:qh);
-      int w = (int)(factor*mRootW);
-      int h = (int)(factor*mRootH);
-
-      Dynamic1D rot = new Dynamic1D(5000,0.0f);
-      rot.setMode(Dynamic1D.MODE_JUMP);
-      rot.add(new Static1D(  0));
-      rot.add(new Static1D(360));
-
-      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
-      Static3D axis   = new Static3D(0,0,1);
-
-      DistortedEffects effects = mRoot.getEffects();
-      effects.abortEffects(EffectTypes.MATRIX);
-      effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) );
-      effects.scale( factor );
-      effects.rotate( rot, axis, center );
-
-      mScreen.resize(width, height);
-      }
+// Glow finished. Make the next Leaf glow.
+
+   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
+     {
+     switch(em)
+       {
+       case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
+                             int glowing = mGlowing+1;
+                             if( glowing>=NUM_LEAVES ) glowing = 0;
+                             makeGlow(glowing);
+                             break;
+       default:              //android.util.Log.e("glow", "effectMessage REMOVED");
+                             break;
+       }
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-    
-    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
-      {
-      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
-      Bitmap leaf;
-      
-      try 
-        {
-        leaf = BitmapFactory.decodeStream(is);
-        } 
-      finally 
-        {
-        try 
-          {
-          is.close();
-          } 
-        catch(IOException e) { }
-        }  
-      
-      mLeaf.setTexture(leaf);
 
-      DistortedEffects.enableEffect(EffectNames.CHROMA);
-      DistortedEffects.enableEffect(EffectNames.GLOW);
+   public void onDrawFrame(GL10 glUnused)
+     {
+     mScreen.render(System.currentTimeMillis());
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+   public void onSurfaceChanged(GL10 glUnused, int width, int height)
+     {
+     float qw = (float)width /mRootW;
+     float qh = (float)height/mRootH;
+     float factor = 0.6f* (qw<qh ? qw:qh);
+     int w = (int)(factor*mRootW);
+     int h = (int)(factor*mRootH);
+
+     Dynamic1D rot = new Dynamic1D(5000,0.0f);
+     rot.setMode(Dynamic1D.MODE_JUMP);
+     rot.add(new Static1D(  0));
+     rot.add(new Static1D(360));
+
+     Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
+     Static3D axis   = new Static3D(0,0,1);
+
+     DistortedEffects effects = mRoot.getEffects();
+     effects.abortEffects(EffectTypes.MATRIX);
+     effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) );
+     effects.scale( factor );
+     effects.rotate( rot, axis, center );
+
+     mScreen.resize(width, height);
+     }
 
-      try
-        {
-        Distorted.onCreate(mView.getContext());
-        }
-      catch(Exception ex)
-        {
-        android.util.Log.e("Glow", ex.getMessage() );
-        }
-      }
- 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
     
+   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
+     {
+     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
+     Bitmap leaf;
+      
+     try
+       {
+       leaf = BitmapFactory.decodeStream(is);
+       }
+     finally
+       {
+       try
+         {
+         is.close();
+         }
+       catch(IOException e) { }
+       }
+      
+     mLeaf.setTexture(leaf);
+
+     DistortedEffects.enableEffect(EffectNames.CHROMA);
+     DistortedEffects.enableEffect(EffectNames.GLOW);
+
+     try
+       {
+       Distorted.onCreate(mView.getContext());
+       }
+     catch(Exception ex)
+       {
+       android.util.Log.e("Glow", ex.getMessage() );
+       }
+     }
 }
diff --git a/src/main/java/org/distorted/examples/listener/ListenerRenderer.java b/src/main/java/org/distorted/examples/listener/ListenerRenderer.java
index 0c2a867..8f975f0 100644
--- a/src/main/java/org/distorted/examples/listener/ListenerRenderer.java
+++ b/src/main/java/org/distorted/examples/listener/ListenerRenderer.java
@@ -109,72 +109,72 @@ class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
     
-    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
-      { 
-      mEffects.abortEffects(EffectTypes.MATRIX);
+   public void onSurfaceChanged(GL10 glUnused, int width, int height)
+     {
+     mEffects.abortEffects(EffectTypes.MATRIX);
          
-      if( (float)bmpHeight/bmpWidth > (float)height/width )
-        {
-        int w = (height*bmpWidth)/bmpHeight;
-        float factor = (float)height/bmpHeight;
-
-        mEffects.move( new Static3D((width-w)/2,0,0) );
-        mEffects.scale(factor);
-        }
-      else
-        {
-        int h = (width*bmpHeight)/bmpWidth;
-        float factor = (float)width/bmpWidth;
-
-        mEffects.move( new Static3D(0,(height-h)/2,0) );
-        mEffects.scale(factor);
-        }
+     if( (float)bmpHeight/bmpWidth > (float)height/width )
+       {
+       int w = (height*bmpWidth)/bmpHeight;
+       float factor = (float)height/bmpHeight;
+
+       mEffects.move( new Static3D((width-w)/2,0,0) );
+       mEffects.scale(factor);
+       }
+     else
+       {
+       int h = (width*bmpHeight)/bmpWidth;
+       float factor = (float)width/bmpWidth;
+
+       mEffects.move( new Static3D(0,(height-h)/2,0) );
+       mEffects.scale(factor);
+       }
       
-      mScreen.resize(width, height);
-      }
+     mScreen.resize(width, height);
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
     
-    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
-      {
-      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
-      Bitmap bitmap;
+   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
+     {
+     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
+     Bitmap bitmap;
         
-      try 
-        {
-        bitmap = BitmapFactory.decodeStream(is);
-        } 
-      finally 
-        {
-        try 
-          {
-          is.close();
-          } 
-        catch(IOException e) { }
-        }  
-
-      bmpHeight = bitmap.getHeight();
-      bmpWidth  = bitmap.getWidth();
-
-      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
-      mTexture.setTexture(bitmap);
-
-      if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
-
-      mScreen.detachAll();
-      mScreen.attach(mTexture,mEffects,mMesh);
-
-      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
-
-      DistortedEffects.enableEffect(EffectNames.DISTORT);
-
-      try
-        {
-        Distorted.onCreate(mView.getContext());
-        }
-      catch(Exception ex)
-        {
-        android.util.Log.e("Renderer", ex.getMessage() );
-        }
-      }
+     try
+       {
+       bitmap = BitmapFactory.decodeStream(is);
+       }
+     finally
+       {
+       try
+         {
+         is.close();
+         }
+       catch(IOException e) { }
+       }
+
+     bmpHeight = bitmap.getHeight();
+     bmpWidth  = bitmap.getWidth();
+
+     if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
+     mTexture.setTexture(bitmap);
+
+     if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
+
+     mScreen.detachAll();
+     mScreen.attach(mTexture,mEffects,mMesh);
+
+     for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
+
+     DistortedEffects.enableEffect(EffectNames.DISTORT);
+
+     try
+       {
+       Distorted.onCreate(mView.getContext());
+       }
+     catch(Exception ex)
+       {
+       android.util.Log.e("Renderer", ex.getMessage() );
+       }
+     }
 }
