commit 508ee98f457b84a865d7cdc68f40f2db38d9da26
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Feb 28 00:19:22 2020 +0000

    New API: MeshBase.join() and a skeleton of an App to test it.

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 4371c49..77cb157 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -58,5 +58,6 @@
         <activity android:name=".movingglow.MovingGlowActivity"/>
         <activity android:name=".earth.EarthActivity"/>
         <activity android:name=".rubik.RubikActivity"/>
+        <activity android:name=".meshjoin.MeshJoinActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index 36738d3..5550491 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -70,6 +70,7 @@ import org.distorted.examples.glow.GlowActivity;
 import org.distorted.examples.movingglow.MovingGlowActivity;
 import org.distorted.examples.earth.EarthActivity;
 import org.distorted.examples.rubik.RubikActivity;
+import org.distorted.examples.meshjoin.MeshJoinActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -116,7 +117,8 @@ public class TableOfContents extends ListActivity
     GLOW              (R.drawable.icon_example_glow            , R.string.example_glow            , R.string.example_glow_subtitle            ,             GlowActivity.class),
     MOVINGGLOW        (R.drawable.icon_example_movingglow      , R.string.example_moving_glow     , R.string.example_moving_glow_subtitle     ,       MovingGlowActivity.class),
     EARTH             (R.drawable.icon_example_earth           , R.string.example_earth           , R.string.example_earth_subtitle           ,            EarthActivity.class),
-    RUBIK             (R.drawable.icon_example_rubik           , R.string.example_rubik           , R.string.example_rubik_subtitle           ,            RubikActivity.class)
+    RUBIK             (R.drawable.icon_example_rubik           , R.string.example_rubik           , R.string.example_rubik_subtitle           ,            RubikActivity.class),
+    MESHJOIN          (R.drawable.icon_example_wip             , R.string.example_meshjoin           , R.string.example_meshjoin_subtitle           ,            MeshJoinActivity.class),
     ;
 
     final int icon, title, subtitle;
diff --git a/src/main/java/org/distorted/examples/meshjoin/MeshJoinActivity.java b/src/main/java/org/distorted/examples/meshjoin/MeshJoinActivity.java
new file mode 100644
index 0000000..fd7b264
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshjoin/MeshJoinActivity.java
@@ -0,0 +1,71 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.meshjoin;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+import org.distorted.library.main.DistortedLibrary;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class MeshJoinActivity extends Activity
+{
+    private MeshJoinSurfaceView mView;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onCreate(Bundle icicle) 
+      {
+      super.onCreate(icicle);
+      mView = new MeshJoinSurfaceView(this);
+      setContentView(mView);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      mView.onPause();
+      DistortedLibrary.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      mView.onResume();
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      DistortedLibrary.onDestroy();
+      super.onDestroy();
+      }
+    
+}
diff --git a/src/main/java/org/distorted/examples/meshjoin/MeshJoinRenderer.java b/src/main/java/org/distorted/examples/meshjoin/MeshJoinRenderer.java
new file mode 100644
index 0000000..6b34203
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshjoin/MeshJoinRenderer.java
@@ -0,0 +1,156 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.meshjoin;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.opengl.GLSurfaceView;
+
+import org.distorted.examples.R;
+import org.distorted.library.effect.MatrixEffectMove;
+import org.distorted.library.effect.MatrixEffectQuaternion;
+import org.distorted.library.effect.MatrixEffectScale;
+import org.distorted.library.main.DistortedEffects;
+import org.distorted.library.main.DistortedLibrary;
+import org.distorted.library.main.DistortedScreen;
+import org.distorted.library.main.DistortedTexture;
+import org.distorted.library.mesh.MeshBase;
+import org.distorted.library.mesh.MeshQuad;
+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 MeshJoinRenderer implements GLSurfaceView.Renderer
+{
+    private GLSurfaceView mView;
+    private DistortedTexture mTexture;
+    private DistortedScreen mScreen;
+    private DistortedEffects mEffects;
+    private Static3D mMove, mScale, mCenter;
+    private MeshBase mMesh;
+    private float mObjHeight, mObjWidth, mObjDepth;
+
+    Static4D mQuat1, mQuat2;
+    int mScreenMin;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    MeshJoinRenderer(GLSurfaceView v)
+      {
+      mView = v;
+      mScreen = new DistortedScreen();
+      mMove = new Static3D(0,0,0);
+      mScale= new Static3D(1,1,1);
+      mCenter=new Static3D(0,0,0);
+
+      mQuat1 = new Static4D(0,0,0,1);  // unity
+      mQuat2 = new Static4D(0,0,0,1);  // quaternions
+
+      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
+      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
+
+      quatInt1.add(mQuat1);
+      quatInt2.add(mQuat2);
+
+      mEffects = new DistortedEffects();
+      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
+      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
+      mEffects.apply( new MatrixEffectScale(mScale));
+      mEffects.apply( new MatrixEffectMove(mMove) );
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public void onDrawFrame(GL10 glUnused) 
+      {
+      mScreen.render( System.currentTimeMillis() );
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
+      {
+      final float SCALE = 0.75f;
+
+      mScreenMin = width<height ? width:height;
+      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
+      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , -factor*mObjDepth/2 );
+      mScale.set(factor,factor,factor);
+      mCenter.set( mObjWidth/2, mObjHeight/2, mObjDepth/2 );
+      mScreen.resize(width, height);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
+      {
+      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
+      Bitmap bitmap;
+
+      try
+        {
+        bitmap = BitmapFactory.decodeStream(is);
+        }
+      finally
+        {
+        try
+          {
+          is.close();
+          }
+        catch(IOException e) { }
+        }
+
+      if( mTexture==null ) mTexture = new DistortedTexture();
+      mTexture.setTexture(bitmap);
+
+      if( mMesh==null ) mMesh = createJoinedMesh();
+
+      mObjWidth = mMesh.getStretchX();
+      mObjHeight= mMesh.getStretchY();
+      mObjDepth = mMesh.getStretchZ();
+
+      mScreen.detachAll();
+      mScreen.attach(mTexture,mEffects,mMesh);
+
+      try
+        {
+        DistortedLibrary.onCreate(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("MeshJoin", ex.getMessage() );
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private MeshBase createJoinedMesh()
+      {
+      return new MeshQuad();
+      }
+}
diff --git a/src/main/java/org/distorted/examples/meshjoin/MeshJoinSurfaceView.java b/src/main/java/org/distorted/examples/meshjoin/MeshJoinSurfaceView.java
new file mode 100644
index 0000000..1c35afa
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshjoin/MeshJoinSurfaceView.java
@@ -0,0 +1,131 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.meshjoin;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.pm.ConfigurationInfo;
+import android.opengl.GLSurfaceView;
+import android.view.MotionEvent;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class MeshJoinSurfaceView extends GLSurfaceView
+{
+    private int mX, mY;
+    private MeshJoinRenderer mRenderer;
+	
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public MeshJoinSurfaceView(Context context)
+      {
+      super(context);
+    
+      mX = -1;
+      mY = -1;
+
+      mRenderer = new MeshJoinRenderer(this);
+      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
+      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
+      setRenderer(mRenderer);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public MeshJoinRenderer 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.get0();
+                                       float qy = mRenderer.mQuat1.get1();
+                                       float qz = mRenderer.mQuat1.get2();
+                                       float qw = mRenderer.mQuat1.get3();
+
+                                       float rx = mRenderer.mQuat2.get0();
+                                       float ry = mRenderer.mQuat2.get1();
+                                       float rz = mRenderer.mQuat2.get2();
+                                       float rw = mRenderer.mQuat2.get3();
+
+                                       // 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/values/strings.xml b/src/main/res/values/strings.xml
index 0f6a1d5..30e8fb6 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -193,6 +193,8 @@
     <string name="example_earth_subtitle">Test the sphere Mesh by showing the Earth in cosmos.</string>
     <string name="example_rubik">Memory Test</string>
     <string name="example_rubik_subtitle">Keep an eye on memory consumption while allocating and deallocating Nodes.</string>
+    <string name="example_meshjoin">Joining Meshes</string>
+    <string name="example_meshjoin_subtitle">See how one can join several simple meshes to form a single, more advanced one.</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>
