commit 175f355d75148f2ca79b012419a3f4a6b990d799
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Tue Oct 18 14:40:32 2016 +0100

    Beginnings of a new example app - a waving flag.
    
    No actual waving yet!

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index c173c72..013b59e 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -38,5 +38,6 @@
         <activity android:name=".effects3d.Effects3DActivity" />
         <activity android:name=".plainmonalisa.PlainMonaLisaActivity" />
         <activity android:name=".save.SaveActivity"/>
+        <activity android:name=".flag.FlagActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index b5fc813..9a1b163 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -57,6 +57,7 @@ import org.distorted.examples.matrix3d.Matrix3DActivity;
 import org.distorted.examples.effects3d.Effects3DActivity;
 import org.distorted.examples.plainmonalisa.PlainMonaLisaActivity;
 import org.distorted.examples.save.SaveActivity;
+import org.distorted.examples.flag.FlagActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -286,6 +287,15 @@ public class TableOfContents extends ListActivity
       activityMapping.put(i++, SaveActivity.class);
    }
 
+   {
+      final Map<String, Object> item = new HashMap<>();
+      item.put(ITEM_IMAGE, R.drawable.icon_example_flag);
+      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_flag));
+      item.put(ITEM_SUBTITLE, getText(R.string.example_flag_subtitle));
+      data.add(item);
+      activityMapping.put(i++, FlagActivity.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/cubes/CubesSurfaceView.java b/src/main/java/org/distorted/examples/cubes/CubesSurfaceView.java
index 166dc48..22a78e1 100644
--- a/src/main/java/org/distorted/examples/cubes/CubesSurfaceView.java
+++ b/src/main/java/org/distorted/examples/cubes/CubesSurfaceView.java
@@ -57,7 +57,7 @@ class CubesSurfaceView extends GLSurfaceView
         
         setRenderer(mRenderer);
         
-        Toast.makeText(c, R.string.example_cubes_toast , Toast.LENGTH_SHORT).show();   
+        Toast.makeText(c, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
         }
       }
     
diff --git a/src/main/java/org/distorted/examples/flag/FlagActivity.java b/src/main/java/org/distorted/examples/flag/FlagActivity.java
new file mode 100644
index 0000000..7bea1fd
--- /dev/null
+++ b/src/main/java/org/distorted/examples/flag/FlagActivity.java
@@ -0,0 +1,69 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.flag;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+import org.distorted.library.Distorted;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class FlagActivity extends Activity
+{
+    private FlagSurfaceView mView;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onCreate(Bundle savedState) 
+      {
+      super.onCreate(savedState);
+      mView = new FlagSurfaceView(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/flag/FlagRenderer.java b/src/main/java/org/distorted/examples/flag/FlagRenderer.java
new file mode 100644
index 0000000..625e377
--- /dev/null
+++ b/src/main/java/org/distorted/examples/flag/FlagRenderer.java
@@ -0,0 +1,145 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.flag;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.opengl.GLES20;
+import android.opengl.GLSurfaceView;
+
+import org.distorted.examples.R;
+import org.distorted.library.Distorted;
+import org.distorted.library.DistortedBitmap;
+import org.distorted.library.DistortedObject;
+import org.distorted.library.EffectTypes;
+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 FlagRenderer implements GLSurfaceView.Renderer
+{
+    private GLSurfaceView mView;
+    private DistortedObject mObject;
+    private int mObjWidth, mObjHeight;
+    private DynamicQuat mQuatInt1, mQuatInt2;
+
+    Static4D mQuat1, mQuat2;
+
+    int mScreenMin;
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    FlagRenderer(GLSurfaceView v)
+      {
+      mView = v;
+
+      mObject = new DistortedBitmap(100,60,30);
+
+      mObjWidth = mObject.getWidth();
+      mObjHeight= mObject.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);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public void onDrawFrame(GL10 glUnused) 
+      {
+      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
+      
+      mObject.draw(System.currentTimeMillis());
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
+      {
+      mScreenMin = width<height ? width:height;
+    	
+      mObject.abortEffects(EffectTypes.MATRIX);
+      float factor;
+
+      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
+        {
+        factor = (0.8f*height)/mObjHeight;
+        }
+      else
+        {
+        factor = (0.8f*width)/mObjWidth;
+        }
+
+      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
+      mObject.scale(factor);
+      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
+
+      mObject.quaternion(mQuatInt1, center);
+      mObject.quaternion(mQuatInt2, center);
+       
+      Distorted.onSurfaceChanged(width, height); 
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
+      {
+      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
+      Bitmap bitmap;
+        
+      try 
+        {
+        bitmap = BitmapFactory.decodeStream(is);
+        } 
+      finally 
+        {
+        try 
+          {
+          is.close();
+          } 
+        catch(IOException e) { }
+        }  
+      
+      mObject.setBitmap(bitmap);
+      
+      try
+        {
+        Distorted.onSurfaceCreated(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("Cubes", ex.getMessage() );
+        }
+      }
+}
diff --git a/src/main/java/org/distorted/examples/flag/FlagSurfaceView.java b/src/main/java/org/distorted/examples/flag/FlagSurfaceView.java
new file mode 100644
index 0000000..a9a606b
--- /dev/null
+++ b/src/main/java/org/distorted/examples/flag/FlagSurfaceView.java
@@ -0,0 +1,136 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.flag;
+
+import android.content.Context;
+import android.opengl.GLSurfaceView;
+import android.os.Build;
+import android.view.MotionEvent;
+import android.widget.Toast;
+
+import org.distorted.examples.R;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class FlagSurfaceView extends GLSurfaceView
+{
+    private int mX, mY;
+    private FlagRenderer mRenderer;
+	
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public FlagSurfaceView(Context c)
+      {
+      super(c);
+    
+      mX = -1;
+      mY = -1;
+      
+      if(!isInEditMode())
+        {
+        setEGLContextClientVersion(2);
+        
+        if( Build.FINGERPRINT.startsWith("generic") )
+          { 
+          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
+          }
+    
+        mRenderer = new FlagRenderer(this);
+        
+        setRenderer(mRenderer);
+        
+        Toast.makeText(c, 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_flag.png b/src/main/res/drawable-hdpi/icon_example_flag.png
new file mode 100644
index 0000000..1da885c
Binary files /dev/null and b/src/main/res/drawable-hdpi/icon_example_flag.png differ
diff --git a/src/main/res/raw/iceland.png b/src/main/res/raw/iceland.png
new file mode 100644
index 0000000..bc39db5
Binary files /dev/null and b/src/main/res/raw/iceland.png differ
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 5f7685a..eb390c6 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -100,8 +100,10 @@
     <string name="example_plainmonalisa_subtitle">MonaLisa rendered on a plain SurfaceView</string>
     <string name="example_save">Save to PNG</string>
     <string name="example_save_subtitle">Saving the output to a PNG file.</string>
+    <string name="example_flag">Waving flag</string>
+    <string name="example_flag_subtitle">See the WAVE effect.</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_cubes_toast">Rotate the cubes by swiping the screen</string>
+    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>
     <string name="example_effects2d_toast">Failed to add new effect</string>
 </resources>
