commit 7f986357c851f46ed50026a058d57444c8c9a1d8
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Apr 6 20:12:33 2019 +0100

    RubikApp: simplifications.

diff --git a/src/main/java/org/distorted/examples/rubik/RubikActivity.java b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
index 76ca66d..88108c0 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikActivity.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
@@ -67,5 +67,4 @@ public class RubikActivity extends Activity
       Distorted.onDestroy();  
       super.onDestroy();
       }
-    
 }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikCube.java b/src/main/java/org/distorted/examples/rubik/RubikCube.java
index efc10e9..30ab02b 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikCube.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikCube.java
@@ -41,7 +41,7 @@ import org.distorted.library.type.Static4D;
 
 class RubikCube
 {
-    static final int TEXTURE_SIZE = 100;
+    private static final int TEXTURE_SIZE = 100;
 
     private static final Static3D VectX = new Static3D(1,0,0);
     private static final Static3D VectY = new Static3D(0,1,0);
@@ -161,7 +161,7 @@ class RubikCube
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void addNewRotation(int vector, int row )
+    void addNewRotation(int vector, float offset )
       {
       Static3D axis = VectX;
 
@@ -173,7 +173,7 @@ class RubikCube
         }
 
       mRotAxis = vector;
-      mRotRow  = row;
+      mRotRow  = (int)(mSize*offset);
 
       mRotationAngleStatic.set1(0.0f);
 
@@ -182,7 +182,7 @@ class RubikCube
           for(int z=0; z<mSize; z++)
             if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
               {
-              if( belongsToRotation(x,y,z,vector,row) )
+              if( belongsToRotation(x,y,z,vector,mRotRow) )
                 {
                 mRotationAxis[x][y][z].set(axis);
                 mRotationAngle[x][y][z].add(mRotationAngleStatic);
@@ -275,8 +275,6 @@ class RubikCube
       int roundedY = (int)(rotatedY+0.1f);
       int roundedZ = (int)(rotatedZ+0.1f);
 
-      //android.util.Log.e("rubik", "before: ("+((int)beforeX)+","+((int)beforeY)+","+((int)beforeZ)+") after: ("+roundedX+","+roundedY+","+roundedZ+")");
-
       mCurrentPosition[x][y][z].set1(roundedX);
       mCurrentPosition[x][y][z].set2(roundedY);
       mCurrentPosition[x][y][z].set3(roundedZ);
@@ -346,4 +344,11 @@ class RubikCube
       {
       return mTexture.getDepth(mCubes[0][0][0]);
       }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    float getSizeInModelSpace()
+      {
+      return mSize*TEXTURE_SIZE;
+      }
 }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
index d6fa3b7..ef8d5b0 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
@@ -34,7 +34,7 @@ import javax.microedition.khronos.opengles.GL10;
 
 class RubikRenderer implements GLSurfaceView.Renderer
 {
-    static final int NUM_CUBES = 4;
+    private static final int NUM_CUBES = 4;
     private static final float CUBE_SCREEN_RATIO = 0.5f;
 
     private RubikSurfaceView mView;
@@ -98,10 +98,12 @@ class RubikRenderer implements GLSurfaceView.Renderer
     
     public void onSurfaceChanged(GL10 glUnused, int width, int height) 
       {
-      mScreen.setProjection( width>height ? 60.0f : 90.0f, 0.1f);
+      float fovInDegrees = (width>height ? 60.0f : 90.0f);
+
+      mScreen.setProjection( fovInDegrees, 0.1f);
 
       mView.setScreenSize(width,height);
-      mView.recomputeCameraDistance();
+      mView.recomputeCameraDistance(fovInDegrees*Math.PI/180);
 
       float w = mCube.getWidth();
       float h = mCube.getHeight();
@@ -145,17 +147,9 @@ class RubikRenderer implements GLSurfaceView.Renderer
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    float getFOVInRadians()
-      {
-      return (float)((mScreen.getFOV()*Math.PI) / 180.0);
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// NUM_CUBES individual little cubes, each TEXTURE_SIZE in size, times 'scaleFactor' (see onSurfaceChanged)
-
     float returnCubeSize()
       {
-      return mScaleFactor*NUM_CUBES*RubikCube.TEXTURE_SIZE;
+      return mScaleFactor*mCube.getSizeInModelSpace();
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
index e8bd324..4b2553e 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -44,7 +44,7 @@ class RubikSurfaceView extends GLSurfaceView
     static final int VECTZ = 2;
     static final int VECTN = 3;
 
-    private boolean mDragging, mRotating;
+    private boolean mDragging, mBeginRot;
     private int mX, mY;
     private Static4D mQuatCurrent, mQuatAccumulated;
     private int mRotationVect;
@@ -64,7 +64,7 @@ class RubikSurfaceView extends GLSurfaceView
       super(context);
 
       mDragging = false;
-      mRotating = false;
+      mBeginRot = false;
       mRotationVect = VECTN;
 
       mScreenWidth = mScreenHeight = mScreenMin = 0;
@@ -103,7 +103,7 @@ class RubikSurfaceView extends GLSurfaceView
                                        mY = y;
                                        mLastTouchedFace = faceTouched(x,y);
 
-                                       if( mLastTouchedFace != NONE ) mRotating = true;
+                                       if( mLastTouchedFace != NONE ) mBeginRot = true;
                                        else                           mDragging = true;
 
                                        break;
@@ -112,16 +112,14 @@ class RubikSurfaceView extends GLSurfaceView
                                          mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
                                          mRenderer.setQuatCurrent(mQuatCurrent);
                                          }
-                                       else if( mRotating )
+                                       else if( mBeginRot )
                                          {
                                          int minimumToRotate = (mScreenMin*mScreenMin)/100;
 
                                          if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
                                            {
                                            addNewRotation(x,y);
-                                           mX = x;
-                                           mY = y;
-                                           mRotating = false;
+                                           mBeginRot = false;
                                            }
                                          }
                                        else
@@ -132,7 +130,7 @@ class RubikSurfaceView extends GLSurfaceView
          case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
 
                                        mDragging = false;
-                                       mRotating = false;
+                                       mBeginRot = false;
 
                                        mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
                                        mQuatCurrent.set(0f, 0f, 0f, 1f);
@@ -144,6 +142,23 @@ class RubikSurfaceView extends GLSurfaceView
       return true;
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void setScreenSize(int width, int height)
+      {
+      mScreenWidth = width;
+      mScreenHeight= height;
+
+      mScreenMin = width<height ? width:height;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void recomputeCameraDistance(double fovInRadians)
+      {
+      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     private void addNewRotation(int x, int y)
@@ -157,10 +172,6 @@ class RubikSurfaceView extends GLSurfaceView
       float diffY = (mPoiY-mCamY)*A + mCamY - mStartY;
       float diffZ = (mPoiZ-mCamZ)*A + mCamZ - mStartZ;
 
-      mStartX = diffX + mStartX;
-      mStartY = diffY + mStartY;
-      mStartZ = diffZ + mStartZ;
-
       switch(mLastTouchedFace)
         {
         case FRONT :
@@ -171,17 +182,20 @@ class RubikSurfaceView extends GLSurfaceView
         case BOTTOM: mRotationVect = (isVertical(diffX, diffZ) ? VECTX:VECTZ); break;
         }
 
-      int row=0;
-      float tmp = RubikRenderer.NUM_CUBES/(2*cubeHalfSize);
+      float offset=0;
 
       switch(mRotationVect)
         {
-        case VECTX: row = (int)(tmp*(mStartX+cubeHalfSize)); break;
-        case VECTY: row = (int)(tmp*(mStartY+cubeHalfSize)); break;
-        case VECTZ: row = (int)(tmp*(mStartZ+cubeHalfSize)); break;
+        case VECTX: offset = (mStartX+cubeHalfSize)/(2*cubeHalfSize); break;
+        case VECTY: offset = (mStartY+cubeHalfSize)/(2*cubeHalfSize); break;
+        case VECTZ: offset = (mStartZ+cubeHalfSize)/(2*cubeHalfSize); break;
         }
 
-      mCube.addNewRotation(mRotationVect,row);
+      mStartX = diffX + mStartX;
+      mStartY = diffY + mStartY;
+      mStartZ = diffZ + mStartZ;
+
+      mCube.addNewRotation(mRotationVect,offset);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -206,33 +220,14 @@ class RubikSurfaceView extends GLSurfaceView
 
       float angle=0.0f;
 
-      switch(mRotationVect)
+      switch(mLastTouchedFace)
         {
-        case VECTX: switch(mLastTouchedFace)
-                      {
-                      case FRONT : angle = -diffY; break;
-                      case BACK  : angle =  diffY; break;
-                      case TOP   : angle =  diffZ; break;
-                      case BOTTOM: angle = -diffZ; break;
-                      }
-                    break;
-        case VECTY: switch(mLastTouchedFace)
-                      {
-                      case FRONT : angle =  diffX; break;
-                      case BACK  : angle = -diffX; break;
-                      case LEFT  : angle =  diffZ; break;
-                      case RIGHT : angle = -diffZ; break;
-                      }
-                    break;
-        case VECTZ: switch(mLastTouchedFace)
-                      {
-                      case TOP   : angle = -diffX; break;
-                      case BOTTOM: angle =  diffX; break;
-                      case LEFT  : angle = -diffY; break;
-                      case RIGHT : angle =  diffY; break;
-                      }
-                    break;
-        default   : android.util.Log.e("View", "impossible vector: "+mRotationVect);
+        case FRONT : angle = (mRotationVect==VECTX ? -diffY : diffX); break;
+        case BACK  : angle = (mRotationVect==VECTX ?  diffY :-diffX); break;
+        case LEFT  : angle = (mRotationVect==VECTY ?  diffZ :-diffY); break;
+        case RIGHT : angle = (mRotationVect==VECTY ? -diffZ : diffY); break;
+        case TOP   : angle = (mRotationVect==VECTZ ? -diffX : diffZ); break;
+        case BOTTOM: angle = (mRotationVect==VECTZ ?  diffX :-diffZ); break;
         }
 
       mCube.continueRotation(200.0f*angle/mScreenMin);
@@ -329,7 +324,7 @@ class RubikSurfaceView extends GLSurfaceView
 
     private boolean faceIsVisible(int face)
       {
-      float cubeHalfSize   = mRenderer.returnCubeSize()*0.5f;
+      float cubeHalfSize= mRenderer.returnCubeSize()*0.5f;
 
       Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,mCameraDistance,0), mQuatAccumulated);
 
@@ -378,7 +373,7 @@ class RubikSurfaceView extends GLSurfaceView
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void fillTouchPoint(int x, int y)
+    private void fillTouchPoint(int x, int y)
       {
       float halfScrWidth  = mScreenWidth *0.5f;
       float halfScrHeight = mScreenHeight*0.5f;
@@ -392,7 +387,7 @@ class RubikSurfaceView extends GLSurfaceView
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void fillCamera()
+    private void fillCamera()
       {
       Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
       Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
@@ -404,7 +399,7 @@ class RubikSurfaceView extends GLSurfaceView
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    float retA(int face, float cubeHalfSize)
+    private float retA(int face, float cubeHalfSize)
       {
       switch(face)
         {
@@ -421,7 +416,7 @@ class RubikSurfaceView extends GLSurfaceView
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    boolean faceCondition(int face)
+    private boolean faceCondition(int face)
       {
       switch(face)
         {
@@ -435,23 +430,5 @@ class RubikSurfaceView extends GLSurfaceView
 
       return false;
       }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void setScreenSize(int width, int height)
-      {
-      mScreenWidth = width;
-      mScreenHeight= height;
-
-      mScreenMin = width<height ? width:height;
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    void recomputeCameraDistance()
-      {
-      double fovInRadians = mRenderer.getFOVInRadians();
-      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
-      }
 }
 
