commit 14122c52836ee8960be717d885048b2068afafdb
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Fri Nov 4 14:11:58 2016 +0000

    Beginnings of the 'Wind' app.

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 013b59e..85d3485 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -39,5 +39,6 @@
         <activity android:name=".plainmonalisa.PlainMonaLisaActivity" />
         <activity android:name=".save.SaveActivity"/>
         <activity android:name=".flag.FlagActivity"/>
+        <activity android:name=".wind.WindActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index 9a1b163..1d2a284 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -58,6 +58,7 @@ import org.distorted.examples.effects3d.Effects3DActivity;
 import org.distorted.examples.plainmonalisa.PlainMonaLisaActivity;
 import org.distorted.examples.save.SaveActivity;
 import org.distorted.examples.flag.FlagActivity;
+import org.distorted.examples.wind.WindActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -296,6 +297,15 @@ public class TableOfContents extends ListActivity
       activityMapping.put(i++, FlagActivity.class);
    }
 
+   {
+      final Map<String, Object> item = new HashMap<>();
+      item.put(ITEM_IMAGE, R.drawable.icon_example_wind);
+      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_wind));
+      item.put(ITEM_SUBTITLE, getText(R.string.example_wind_subtitle));
+      data.add(item);
+      activityMapping.put(i++, WindActivity.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/wind/WindActivity.java b/src/main/java/org/distorted/examples/wind/WindActivity.java
new file mode 100644
index 0000000..367cf0a
--- /dev/null
+++ b/src/main/java/org/distorted/examples/wind/WindActivity.java
@@ -0,0 +1,108 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.wind;
+
+import android.app.Activity;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+import android.widget.SeekBar;
+import android.widget.SeekBar.OnSeekBarChangeListener;
+import android.widget.TextView;
+
+import org.distorted.examples.R;
+import org.distorted.library.Distorted;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class WindActivity extends Activity implements OnSeekBarChangeListener
+{
+    private TextView windText;
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onCreate(Bundle icicle) 
+      {
+      super.onCreate(icicle);
+      setContentView(R.layout.windlayout);
+       
+      SeekBar bar = (SeekBar)findViewById(R.id.windSeek);
+      bar.setOnSeekBarChangeListener(this);
+        
+      windText = (TextView)findViewById(R.id.windText);
+     
+      bar.setProgress(50);
+      
+      windText.setText("Wind: 50");
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.windSurfaceView);
+      view.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.windSurfaceView);
+      view.onResume();
+      }
+ 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      Distorted.onDestroy();  
+      super.onDestroy();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
+      {
+      switch (bar.getId()) 
+        {
+        case R.id.windSeek:WindSurfaceView view = (WindSurfaceView) this.findViewById(R.id.windSurfaceView);
+                           view.getRenderer().setWind(progress);
+                           windText.setText("Wind: "+progress);
+                           break;
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void onStartTrackingTouch(SeekBar bar) { }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void onStopTrackingTouch(SeekBar bar)  { }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+}
diff --git a/src/main/java/org/distorted/examples/wind/WindRenderer.java b/src/main/java/org/distorted/examples/wind/WindRenderer.java
new file mode 100644
index 0000000..d32d804
--- /dev/null
+++ b/src/main/java/org/distorted/examples/wind/WindRenderer.java
@@ -0,0 +1,152 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.wind;
+
+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.DistortedCubes;
+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 WindRenderer implements GLSurfaceView.Renderer
+{
+   private GLSurfaceView mView;
+   private DistortedObject mObject;
+   private DynamicQuat mQuatInt1, mQuatInt2;
+   private int mWind;
+   private int mObjWidth, mObjHeight;
+
+   Static4D mQuat1, mQuat2;
+   int mScreenMin;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+   WindRenderer(GLSurfaceView view)
+      { 
+      mView = view;
+
+      mObject = new DistortedCubes(50,30,10,false);
+
+      mObjWidth = mObject.getWidth();
+      mObjHeight= mObject.getHeight();
+
+      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
+      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
+
+      mQuatInt1 = new DynamicQuat(0,0.5f);
+      mQuatInt2 = new DynamicQuat(0,0.5f);
+
+      mQuatInt1.add(mQuat1);
+      mQuatInt2.add(mQuat2);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+   void setWind(int wind)
+      {
+      mWind = wind;
+      }
+   
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+   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("Wind", ex.getMessage() );
+        }
+      }
+}
diff --git a/src/main/java/org/distorted/examples/wind/WindSurfaceView.java b/src/main/java/org/distorted/examples/wind/WindSurfaceView.java
new file mode 100644
index 0000000..b32be59
--- /dev/null
+++ b/src/main/java/org/distorted/examples/wind/WindSurfaceView.java
@@ -0,0 +1,138 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.wind;
+
+import android.content.Context;
+import android.opengl.GLSurfaceView;
+import android.os.Build;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class WindSurfaceView extends GLSurfaceView
+{
+    private int mX, mY;
+    private WindRenderer mRenderer;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public WindSurfaceView(Context c, AttributeSet attrs)
+      {
+      super(c, attrs);
+
+      mX = -1;
+      mY = -1;
+
+      if(!isInEditMode())
+        {
+        setEGLContextClientVersion(2);
+        
+        if( Build.FINGERPRINT.startsWith("generic") )
+          { 
+          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
+          }
+    
+        mRenderer = new WindRenderer(this);
+        setRenderer(mRenderer);
+        }
+      }
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public WindRenderer getRenderer()
+      {
+      return mRenderer;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    @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_wind.png b/src/main/res/drawable-hdpi/icon_example_wind.png
new file mode 100644
index 0000000..1da885c
Binary files /dev/null and b/src/main/res/drawable-hdpi/icon_example_wind.png differ
diff --git a/src/main/res/layout/windlayout.xml b/src/main/res/layout/windlayout.xml
new file mode 100644
index 0000000..1c01262
--- /dev/null
+++ b/src/main/res/layout/windlayout.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:orientation="vertical" >
+
+    <org.distorted.examples.wind.WindSurfaceView
+        android:id="@+id/windSurfaceView"
+        android:layout_width="fill_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+
+    <LinearLayout
+        android:id="@+id/linearLayout1"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center|fill_horizontal"
+        android:orientation="horizontal"
+        android:paddingBottom="10dp"
+        android:paddingTop="10dp" >
+
+        <TextView
+            android:id="@+id/windText"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:paddingLeft="15dp"
+            android:textAppearance="?android:attr/textAppearanceMedium" />
+
+        <SeekBar
+            android:id="@+id/windSeek"
+            android:layout_width="106dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.94"
+            android:paddingLeft="15dp"
+            android:paddingRight="10dp" />
+
+    </LinearLayout>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 72c765f..54767d6 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -106,6 +106,8 @@
     <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_wind">Variable wind</string>
+    <string name="example_wind_subtitle">A couple of effects put together to create an effect of a waving flag.</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>
