commit 90940cafaf99060d5d132ae822d5c88389d83510
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Jun 5 00:57:05 2020 +0100

    Skeleton of the SingleMesh app.

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 12d954c..11e36ef 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -60,5 +60,6 @@
         <activity android:name=".predeform.PredeformActivity"/>
         <activity android:name=".predeform.PredeformActivity2"/>
         <activity android:name=".deferredjob.DeferredJobActivity"/>
+        <activity android:name=".singlemesh.SingleMeshActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index 8c214a1..4ac2652 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -73,6 +73,7 @@ import org.distorted.examples.earth.EarthActivity;
 import org.distorted.examples.rubik.RubikActivity;
 import org.distorted.examples.meshjoin.MeshJoinActivity;
 import org.distorted.examples.predeform.PredeformActivity;
+import org.distorted.examples.singlemesh.SingleMeshActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -123,6 +124,7 @@ public class TableOfContents extends ListActivity
     MESHJOIN          (R.drawable.icon_example_meshjoin        , R.string.example_meshjoin           , R.string.example_meshjoin_subtitle           ,            MeshJoinActivity.class),
     PREDEFORM         (R.drawable.icon_example_predeform       , R.string.example_predeform           , R.string.example_predeform_subtitle           ,            PredeformActivity.class),
     DEFERREDJOB       (R.drawable.icon_example_deferredjob     , R.string.example_deferredjob           , R.string.example_deferredjob_subtitle           ,            DeferredJobActivity.class),
+    SINGLEMESH        (R.drawable.icon_example_wip             , R.string.example_singlemesh           , R.string.example_singlemesh_subtitle           ,            SingleMeshActivity.class),
     ;
 
     final int icon, title, subtitle;
diff --git a/src/main/java/org/distorted/examples/singlemesh/SingleMeshActivity.java b/src/main/java/org/distorted/examples/singlemesh/SingleMeshActivity.java
new file mode 100644
index 0000000..d2a4271
--- /dev/null
+++ b/src/main/java/org/distorted/examples/singlemesh/SingleMeshActivity.java
@@ -0,0 +1,86 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.singlemesh;
+
+import android.app.Activity;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+
+import org.distorted.examples.R;
+import org.distorted.library.main.DistortedLibrary;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class SingleMeshActivity extends Activity
+{
+    @Override
+    protected void onCreate(Bundle icicle) 
+      {
+      super.onCreate(icicle);
+      setContentView(R.layout.singlemeshlayout);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      GLSurfaceView view = this.findViewById(R.id.singlemeshSurfaceView);
+      view.onPause();
+      DistortedLibrary.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      GLSurfaceView view = this.findViewById(R.id.singlemeshSurfaceView);
+      view.onResume();
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      DistortedLibrary.onDestroy();
+      super.onDestroy();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public void onClick(View view)
+      {
+      Button butt = (Button)view;
+      int id = butt.getId();
+      SingleMeshSurfaceView sView = findViewById(R.id.singlemeshSurfaceView);
+
+      switch(id)
+        {
+        case R.id.singlemeshButton0 : sView.getRenderer().apply(0); break;
+        case R.id.singlemeshButton1 : sView.getRenderer().apply(1); break;
+        }
+      }
+}
diff --git a/src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.java b/src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.java
new file mode 100644
index 0000000..07b8a03
--- /dev/null
+++ b/src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.java
@@ -0,0 +1,241 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.singlemesh;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.opengl.GLSurfaceView;
+
+import org.distorted.library.effect.EffectType;
+import org.distorted.library.effect.MatrixEffectQuaternion;
+import org.distorted.library.effect.MatrixEffectScale;
+import org.distorted.library.effect.VertexEffectMove;
+import org.distorted.library.effect.VertexEffectRotate;
+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.MeshJoined;
+import org.distorted.library.mesh.MeshRectangles;
+import org.distorted.library.type.Dynamic1D;
+import org.distorted.library.type.DynamicQuat;
+import org.distorted.library.type.Static1D;
+import org.distorted.library.type.Static3D;
+import org.distorted.library.type.Static4D;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class SingleMeshRenderer implements GLSurfaceView.Renderer
+{
+    private GLSurfaceView mView;
+    private DistortedTexture mTexture;
+    private DistortedScreen mScreen;
+    private DistortedEffects mEffects;
+    private Static3D mScale;
+    private MeshBase mMesh;
+    private VertexEffectRotate mRotate;
+    private Dynamic1D mAngleDyn;
+    private Static1D mAngle;
+
+    Static4D mQuat1, mQuat2;
+    int mScreenMin;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    SingleMeshRenderer(GLSurfaceView v)
+      {
+      mView = v;
+      mScreen = new DistortedScreen();
+      mScale= new Static3D(1,1,1);
+      Static3D center=new Static3D(0,0,0);
+
+      Dynamic1D sink = new Dynamic1D(5000,0.0f);
+      sink.add( new Static1D(0.5f) );
+      sink.add( new Static1D(2.0f) );
+
+      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);
+
+      mAngle = new Static1D(0);
+
+      mAngleDyn = new Dynamic1D(2000,0.5f);
+      mAngleDyn.add(new Static1D(0));
+      mAngleDyn.add( mAngle );
+
+      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
+
+      mEffects = new DistortedEffects();
+      mEffects.apply( mRotate );
+      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
+      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
+      mEffects.apply( new MatrixEffectScale(mScale));
+
+      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+   
+    public void onDrawFrame(GL10 glUnused) 
+      {
+      mScreen.render( System.currentTimeMillis() );
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
+      {
+      final float SCALE = 0.4f;
+      mScreenMin = Math.min(width, height);
+      float factor = SCALE*mScreenMin;
+      mScale.set(factor,factor,factor);
+      mScreen.resize(width, height);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
+      {
+      if( mTexture==null ) mTexture = new DistortedTexture();
+      mTexture.setTexture( createTexture() );
+
+      if( mMesh==null ) mMesh = createMesh();
+
+      mScreen.detachAll();
+      mScreen.attach(mTexture,mEffects,mMesh);
+
+      DistortedLibrary.setMax(EffectType.VERTEX, 11);
+      VertexEffectRotate.enable();
+
+      try
+        {
+        DistortedLibrary.onCreate(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("DeferredJob", ex.getMessage() );
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void apply(int number)
+      {
+      mRotate.setMeshAssociation(0,number);
+      mAngle.set(360);
+      mAngleDyn.resetToBeginning();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private Bitmap createTexture()
+      {
+      final int[] FACE_COLORS = new int[] { 0xffff0000, 0xff00ff00 };
+      final int FACES=FACE_COLORS.length;
+      int SIZE = 200;
+
+      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
+      Canvas canvas = new Canvas(result);
+      Paint paint = new Paint();
+      paint.setStyle(Paint.Style.FILL);
+
+      for(int i=0; i<FACES; i++)
+        {
+        paint.setColor(FACE_COLORS[i]);
+        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
+        }
+
+      return result;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private MeshBase createMesh()
+      {
+      final int MESHES=2;
+
+      MeshBase[] meshes = new MeshRectangles[MESHES];
+
+      meshes[0] = new MeshRectangles(1,1);
+      meshes[0].setEffectAssociation(0,1,0);
+
+      for(int i=1; i<MESHES; i++)
+        {
+        meshes[i] = meshes[0].copy(true);
+        meshes[i].setEffectAssociation(0,1,i);
+        }
+
+      Static4D[] lTextureMaps = new Static4D[MESHES];
+      Static4D[] rTextureMaps = new Static4D[MESHES];
+
+      for(int i=0; i<MESHES; i++)
+        {
+        lTextureMaps[i] = new Static4D(0.0f,0.0f,0.5f,1.0f);
+        rTextureMaps[i] = new Static4D(0.5f,0.0f,0.5f,1.0f);
+        }
+
+      MeshBase[] tmp = new MeshBase[2];
+
+      tmp[0] = new MeshJoined(meshes);
+      tmp[0].setTextureMap(lTextureMaps);
+
+      VertexEffectMove   effect0 = new VertexEffectMove  ( new Static3D(0,0,0.5f) );
+      VertexEffectRotate effect1 = new VertexEffectRotate( new Static1D(180), new Static3D(1,0,0), new Static3D(0,0,0) );
+
+      effect0.setMeshAssociation(1,0);  // apply only to Components whose andAssoc has the least significant bit set, i.e.
+                                        // to both meshes[0] and meshes[1]
+      effect1.setMeshAssociation(0,1);  // apply only to Components whose equAssoc is equal to 1, i.e. only to mesh[1]
+
+      tmp[0].apply(effect0);
+      tmp[0].apply(effect1);
+
+      tmp[1] = tmp[0].copy(true);
+      tmp[1].setTextureMap(rTextureMaps);
+
+      tmp[0].mergeEffComponents();
+      tmp[1].mergeEffComponents();
+
+      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
+      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
+
+      MeshBase combined = new MeshJoined(tmp);
+
+      VertexEffectMove moveL = new VertexEffectMove  ( new Static3D(-0.6f,0,0) );
+      VertexEffectMove moveR = new VertexEffectMove  ( new Static3D(+0.6f,0,0) );
+
+      moveL.setMeshAssociation(0,0);  // apply only to tmp[0]
+      moveR.setMeshAssociation(0,1);  // apply only to tmp[1]
+
+      combined.apply(moveL);
+      combined.apply(moveR);
+
+      return combined;
+      }
+}
diff --git a/src/main/java/org/distorted/examples/singlemesh/SingleMeshSurfaceView.java b/src/main/java/org/distorted/examples/singlemesh/SingleMeshSurfaceView.java
new file mode 100644
index 0000000..e77adf2
--- /dev/null
+++ b/src/main/java/org/distorted/examples/singlemesh/SingleMeshSurfaceView.java
@@ -0,0 +1,149 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.singlemesh;
+
+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;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class SingleMeshSurfaceView extends GLSurfaceView
+{
+    private final static int DIRECTION_SENSITIVITY=  12;
+    private int mX, mY;
+    private SingleMeshRenderer mRenderer;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public SingleMeshSurfaceView(Context context, AttributeSet attrs)
+      {
+      super(context,attrs);
+
+      mX = -1;
+      mY = -1;
+
+      if(!isInEditMode())
+        {
+        mRenderer = new SingleMeshRenderer(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 SingleMeshRenderer getRenderer()
+      {
+      return mRenderer;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private void resetQuats()
+      {
+      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);
+       }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    @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);
+                                           }
+                                         }
+
+                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
+                                         {
+                                         mX = x;
+                                         mY = y;
+                                         resetQuats();
+                                         }
+
+                                       break;
+                                       
+         case MotionEvent.ACTION_UP  : mX = -1;
+                                       mY = -1;
+                                       resetQuats();
+                                       break;
+         }
+             
+      return true;
+      }
+         
+}
+
diff --git a/src/main/res/layout/singlemeshlayout.xml b/src/main/res/layout/singlemeshlayout.xml
new file mode 100644
index 0000000..92770e8
--- /dev/null
+++ b/src/main/res/layout/singlemeshlayout.xml
@@ -0,0 +1,35 @@
+<?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.singlemesh.SingleMeshSurfaceView
+        android:id="@+id/singlemeshSurfaceView"
+        android:layout_width="fill_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+
+    <LinearLayout
+        android:orientation="horizontal"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent"
+            android:id="@+id/singlemeshButton0"
+            android:text="@string/color_red"
+            android:onClick="onClick"
+            android:layout_weight="1"/>
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent"
+            android:id="@+id/singlemeshButton1"
+            android:text="@string/color_green"
+            android:onClick="onClick"
+            android:layout_weight="1"/>
+
+    </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 db96a87..02a21f5 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -200,6 +200,8 @@
     <string name="example_predeform_subtitle">Create a more advanced Mesh by taking one of the simple ones and pre-applying a queue of Vertex Effects to it.</string>
     <string name="example_deferredjob">Deferred Mesh Jobs</string>
     <string name="example_deferredjob_subtitle">Create an advanced mesh in steps, using deferred mesh jobs: apply vertex effects, copy meshes, join them, merge their components.</string>
+    <string name="example_singlemesh">Single Mesh Rubik Cube</string>
+    <string name="example_singlemesh_subtitle">Use the new MeshJoined + MeshBase.apply() to create a single, movable Mesh representing a 2x2x2 RubikCube.</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>
