commit 42aa970fd5d87a4a40591383dff98a2114104678
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Jun 13 01:12:40 2020 +0100

    Skeleton of a new App: MeshFile

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 11e36ef..f948e1a 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -61,5 +61,6 @@
         <activity android:name=".predeform.PredeformActivity2"/>
         <activity android:name=".deferredjob.DeferredJobActivity"/>
         <activity android:name=".singlemesh.SingleMeshActivity"/>
+        <activity android:name=".meshfile.MeshFileActivity"/>
     </application>
 </manifest>
diff --git a/src/main/java/org/distorted/examples/TableOfContents.java b/src/main/java/org/distorted/examples/TableOfContents.java
index d87901e..35fe7e5 100644
--- a/src/main/java/org/distorted/examples/TableOfContents.java
+++ b/src/main/java/org/distorted/examples/TableOfContents.java
@@ -74,6 +74,7 @@ import org.distorted.examples.rubik.RubikActivity;
 import org.distorted.examples.meshjoin.MeshJoinActivity;
 import org.distorted.examples.predeform.PredeformActivity;
 import org.distorted.examples.singlemesh.SingleMeshActivity;
+import org.distorted.examples.meshfile.MeshFileActivity;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -125,6 +126,7 @@ public class TableOfContents extends ListActivity
     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_singlemesh      , R.string.example_singlemesh           , R.string.example_singlemesh_subtitle           ,            SingleMeshActivity.class),
+    MESHFILE          (R.drawable.icon_example_wip             , R.string.example_meshfile           , R.string.example_meshfile_subtitle           ,            MeshFileActivity.class),
     ;
 
     final int icon, title, subtitle;
diff --git a/src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java b/src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java
new file mode 100644
index 0000000..2dc6ad2
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshfile/MeshFileActivity.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.meshfile;
+
+import android.app.Activity;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+
+import org.distorted.examples.R;
+import org.distorted.library.main.DistortedLibrary;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class MeshFileActivity extends Activity
+{
+    @Override
+    protected void onCreate(Bundle icicle) 
+      {
+      super.onCreate(icicle);
+      setContentView(R.layout.meshfilelayout);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onPause() 
+      {
+      GLSurfaceView view = this.findViewById(R.id.meshfileSurfaceView);
+      view.onPause();
+      DistortedLibrary.onPause();
+      super.onPause();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onResume() 
+      {
+      super.onResume();
+      GLSurfaceView view = this.findViewById(R.id.meshfileSurfaceView);
+      view.onResume();
+      }
+    
+///////////////////////////////////////////////////////////////////////////////////////////////////
+    
+    @Override
+    protected void onDestroy() 
+      {
+      DistortedLibrary.onDestroy();
+      super.onDestroy();
+      }
+}
diff --git a/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java b/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
new file mode 100644
index 0000000..07b0552
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
@@ -0,0 +1,377 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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.meshfile;
+
+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.MatrixEffectMove;
+import org.distorted.library.effect.MatrixEffectQuaternion;
+import org.distorted.library.effect.MatrixEffectScale;
+import org.distorted.library.effect.VertexEffectDeform;
+import org.distorted.library.effect.VertexEffectMove;
+import org.distorted.library.effect.VertexEffectRotate;
+import org.distorted.library.effect.VertexEffectSink;
+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 MeshFileRenderer implements GLSurfaceView.Renderer
+{
+    private static final float DIST = 0.5f;
+
+    private static Static3D[] AXIS = new Static3D[]
+         {
+           new Static3D(1,0,0),
+           new Static3D(0,1,0),
+           new Static3D(0,0,1)
+         };
+
+    private static final int[] FACE_COLORS = new int[]
+         {
+           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
+           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
+           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
+         };
+
+    private static final int NUM_FACES = FACE_COLORS.length;
+
+    private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+    private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
+
+    private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
+         {
+             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
+             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
+             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
+             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
+             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
+             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
+             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
+             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
+         };
+
+    private static final Static3D[] CUBIT_MOVES = new Static3D[]
+         {
+           new Static3D(-DIST,-DIST,-DIST),
+           new Static3D(-DIST,-DIST,+DIST),
+           new Static3D(-DIST,+DIST,-DIST),
+           new Static3D(-DIST,+DIST,+DIST),
+           new Static3D(+DIST,-DIST,-DIST),
+           new Static3D(+DIST,-DIST,+DIST),
+           new Static3D(+DIST,+DIST,-DIST),
+           new Static3D(+DIST,+DIST,+DIST),
+         };
+
+    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;
+    private Static3D mAxis;
+
+    Static4D mQuat1, mQuat2;
+    int mScreenMin;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    MeshFileRenderer(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);
+      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
+
+      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
+      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
+
+      quatInt1.add(mQuat1);
+      quatInt2.add(mQuat2);
+
+      mAngle = new Static1D(0);
+      mAxis  = new Static3D(1,0,0);
+
+      mAngleDyn = new Dynamic1D(2000,0.5f);
+      mAngleDyn.add(new Static1D(0));
+      mAngleDyn.add( mAngle );
+
+      mRotate = new VertexEffectRotate( mAngleDyn, mAxis, 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.3f;
+      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, 15);
+      VertexEffectRotate.enable();
+
+      try
+        {
+        DistortedLibrary.onCreate(mView.getContext());
+        }
+      catch(Exception ex)
+        {
+        android.util.Log.e("DeferredJob", ex.getMessage() );
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void apply(int andAssoc, int axisIndex)
+      {
+      mRotate.setMeshAssociation(andAssoc,-1);
+
+      mAngle.set(360);
+
+      mAxis.set0(AXIS[axisIndex].get0());
+      mAxis.set1(AXIS[axisIndex].get1());
+      mAxis.set2(AXIS[axisIndex].get2());
+
+      mAngleDyn.resetToBeginning();
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private Bitmap createTexture()
+      {
+      final int NUM_FACES = 6;
+      final int TEXTURE_HEIGHT = 200;
+      final int INTERIOR_COLOR = 0xff000000;
+      final float R = TEXTURE_HEIGHT*0.10f;
+      final float M = TEXTURE_HEIGHT*0.05f;
+
+      Bitmap bitmap;
+      Paint paint = new Paint();
+      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
+      Canvas canvas = new Canvas(bitmap);
+
+      paint.setAntiAlias(true);
+      paint.setTextAlign(Paint.Align.CENTER);
+      paint.setStyle(Paint.Style.FILL);
+
+      paint.setColor(INTERIOR_COLOR);
+      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
+
+      for(int i=0; i<NUM_FACES; i++)
+        {
+        paint.setColor(FACE_COLORS[i]);
+        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
+        }
+
+      return bitmap;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+     MeshBase createCubitMesh()
+      {
+      final int MESHES=6;
+      int association = 1;
+      MeshBase[] meshes = new MeshRectangles[MESHES];
+      meshes[0] = new MeshRectangles(10,10);
+      meshes[0].setEffectAssociation(0,association,0);
+
+      for(int i=1; i<MESHES; i++)
+        {
+        association <<=1;
+        meshes[i] = meshes[0].copy(true);
+        meshes[i].setEffectAssociation(0,association,0);
+        }
+
+      MeshBase mesh = new MeshJoined(meshes);
+
+      Static3D axisY   = new Static3D(0,1,0);
+      Static3D axisX   = new Static3D(1,0,0);
+      Static3D center  = new Static3D(0,0,0);
+      Static1D angle90 = new Static1D(90);
+      Static1D angle180= new Static1D(180);
+      Static1D angle270= new Static1D(270);
+
+      float d1 = 1.0f;
+      float d2 =-0.05f;
+      float d3 = 0.12f;
+
+      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
+      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
+      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
+      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
+      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
+      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
+      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
+      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
+
+      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
+      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
+      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
+      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
+      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
+      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
+      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
+      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
+
+      Static4D dReg  = new Static4D(0,0,0,d3);
+      Static1D dRad  = new Static1D(1);
+
+      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
+      effect0.setMeshAssociation(63,-1);  // all 6 sides
+      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
+      effect1.setMeshAssociation(32,-1);  // back
+      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
+      effect2.setMeshAssociation( 8,-1);  // bottom
+      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
+      effect3.setMeshAssociation( 4,-1);  // top
+      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
+      effect4.setMeshAssociation( 2,-1);  // left
+      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
+      effect5.setMeshAssociation( 1,-1);  // right
+
+      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
+      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
+      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
+      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
+      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
+      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
+      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
+      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
+
+      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
+
+      mesh.apply(effect0);
+      mesh.apply(effect1);
+      mesh.apply(effect2);
+      mesh.apply(effect3);
+      mesh.apply(effect4);
+      mesh.apply(effect5);
+      mesh.apply(effect6);
+      mesh.apply(effect7);
+      mesh.apply(effect8);
+      mesh.apply(effect9);
+      mesh.apply(effect10);
+      mesh.apply(effect11);
+      mesh.apply(effect12);
+      mesh.apply(effect13);
+      mesh.apply(effect14);
+
+      mesh.mergeEffComponents();
+
+      return mesh;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private MeshBase createMesh()
+      {
+      final int NUM_CUBITS = CUBIT_MOVES.length;
+      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
+
+      cubits[NUM_CUBITS-1] = createCubitMesh();   // NUM_CUBITS-1 (or anything non-zero!)
+
+      for(int i=0; i<NUM_CUBITS-1; i++)
+        {
+        cubits[i] = cubits[NUM_CUBITS-1].copy(true);
+        }
+
+      for(int i=0; i<NUM_CUBITS; i++)
+        {
+        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
+        cubits[i].setTextureMap(TEXTURE_MAP[i],0);
+        }
+
+      MeshBase result = new MeshJoined(cubits);
+
+      result.setEffectAssociation( 0, (1<<4) + (1<<2) + 1, 0);
+      result.setEffectAssociation( 1, (1<<4) + (1<<2) + 2, 0);
+      result.setEffectAssociation( 2, (1<<4) + (2<<2) + 1, 0);
+      result.setEffectAssociation( 3, (1<<4) + (2<<2) + 2, 0);
+      result.setEffectAssociation( 4, (2<<4) + (1<<2) + 1, 0);
+      result.setEffectAssociation( 5, (2<<4) + (1<<2) + 2, 0);
+      result.setEffectAssociation( 6, (2<<4) + (2<<2) + 1, 0);
+      result.setEffectAssociation( 7, (2<<4) + (2<<2) + 2, 0);
+
+      return result;
+      }
+}
diff --git a/src/main/java/org/distorted/examples/meshfile/MeshFileSurfaceView.java b/src/main/java/org/distorted/examples/meshfile/MeshFileSurfaceView.java
new file mode 100644
index 0000000..2e16850
--- /dev/null
+++ b/src/main/java/org/distorted/examples/meshfile/MeshFileSurfaceView.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.meshfile;
+
+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 MeshFileSurfaceView extends GLSurfaceView
+{
+    private final static int DIRECTION_SENSITIVITY=  12;
+    private int mX, mY;
+    private MeshFileRenderer mRenderer;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    public MeshFileSurfaceView(Context context, AttributeSet attrs)
+      {
+      super(context,attrs);
+
+      mX = -1;
+      mY = -1;
+
+      if(!isInEditMode())
+        {
+        mRenderer = new MeshFileRenderer(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 MeshFileRenderer 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/meshfilelayout.xml b/src/main/res/layout/meshfilelayout.xml
new file mode 100644
index 0000000..732c4be
--- /dev/null
+++ b/src/main/res/layout/meshfilelayout.xml
@@ -0,0 +1,37 @@
+<?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.meshfile.MeshFileSurfaceView
+        android:id="@+id/meshfileSurfaceView"
+        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">
+
+        <Spinner
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.5"
+            android:id="@+id/meshfileSpinner"
+            />
+
+        <Button
+            android:id="@+id/meshfileButton"
+            android:layout_width="100dp"
+            android:layout_height="fill_parent"
+            android:gravity="center_vertical|center"
+            android:text="@string/open"
+            android:onClick="Open"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:layout_gravity="center_vertical"/>
+
+    </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 7d81156..ffd3161 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -87,6 +87,7 @@
     <string name="association">Sink Association</string>
     <string name="rotate_left">Rotate Left</string>
     <string name="rotate_right">Rotate Right</string>
+    <string name="open">Open</string>
 
     <string name="quality0">Highest</string>
     <string name="quality1">High</string>
@@ -204,6 +205,8 @@
     <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_meshfile">Mesh from File</string>
+    <string name="example_meshfile_subtitle">Explore Distorted\'s own Mesh format, dmesh. Open .dmesh files and explore their contents.</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>
