Project

General

Profile

Download (18.7 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 0e5ad27c

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.util.AttributeSet;
27
import android.view.MotionEvent;
28
29 775e675d Leszek Koltunski
import org.distorted.library.type.Static2D;
30 12ad3fca Leszek Koltunski
import org.distorted.library.type.Static3D;
31 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
32 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObject;
33
import org.distorted.objects.RubikObjectMovement;
34 46a961fd Leszek Koltunski
import org.distorted.solvers.SolverMain;
35 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikState;
36 473611ee Leszek Koltunski
import org.distorted.states.RubikStateSolver;
37 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikStateSolving;
38 0c52af30 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
42 0c52af30 Leszek Koltunski
{
43 473611ee Leszek Koltunski
    public static final int MODE_ROTATE  = 0;
44
    public static final int MODE_DRAG    = 1;
45
    public static final int MODE_REPLACE = 2;
46
47 775e675d Leszek Koltunski
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
48
    // given face by SWIPING_SENSITIVITY/2 degrees.
49
    private final static int SWIPING_SENSITIVITY  = 240;
50 14a4712f Leszek Koltunski
    // Moving the finger by 1/15 the distance of min(scrWidth,scrHeight) will start a Rotation.
51
    private final static int ROTATION_SENSITIVITY =  15;
52 eac805bd Leszek Koltunski
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
53
    private final static int DIRECTION_SENSITIVITY=  12;
54
55 84ddf691 Leszek Koltunski
    // Where did we get this sqrt(3)/2 ? From the (default, i.e. 60 degrees - see InternalOutputSurface!)
56
    // FOV of the projection matrix of the Node onto the Screen.
57
    // Take a look how the CAMERA_POINT is used in onTouchEvent - (x,y) there are expressed in sort of
58
    // 'half-NDC' coordinates i.e. they range from +0.5 to -0.5; thus CAMERA_POINT also needs to be
59
    // in 'half-NDC'. Since in this coordinate system the height of the screen is equal to 1, then the
60
    // Z-distance from the center of the object to the camera is equal to (scrHeight/2)/tan(FOV/2) =
61
    // 0.5/tan(30) = sqrt(3)/2.
62
    // Why is the Z-distance between the camera and the object equal to (scrHeight/2)/tan(FOV/2)?
63
    // Because of the way the View part of the ModelView matrix is constructed in EffectQueueMatrix.send().
64
    private final Static4D CAMERA_POINT = new Static4D(0, 0, (float)Math.sqrt(3)*0.5f, 0);
65 86c73a69 Leszek Koltunski
66 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
67 8becce57 Leszek Koltunski
    private RubikPostRender mPostRender;
68 27a70eae Leszek Koltunski
    private RubikObjectMovement mMovement;
69 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
70 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
71 0c52af30 Leszek Koltunski
72 12ad3fca Leszek Koltunski
    private float mX, mY;
73
    private float mStartRotX, mStartRotY;
74
    private float mAxisX, mAxisY;
75 e46e17fb Leszek Koltunski
    private float mRotationFactor;
76 46a961fd Leszek Koltunski
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
77 0e5ad27c Leszek Koltunski
    private int mCurrentAxis, mCurrentRow;
78
    private float mCurrentAngle;
79 12ad3fca Leszek Koltunski
80 45686da2 Leszek Koltunski
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
81
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
82
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
83
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
84 ee5c2ae1 Leszek Koltunski
85 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
    void setScreenSize(int width, int height)
88
      {
89
      mScreenWidth = width;
90
      mScreenHeight= height;
91
92 12ad3fca Leszek Koltunski
      mScreenMin = Math.min(width, height);
93 0fd3ac1f Leszek Koltunski
      }
94
95 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97
    boolean isVertical()
98
      {
99
      return mScreenHeight>mScreenWidth;
100
      }
101
102 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
    RubikRenderer getRenderer()
105
      {
106
      return mRenderer;
107
      }
108
109 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
    RubikPostRender getPostRender()
112
      {
113
      return mPostRender;
114
      }
115
116 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
    void setQuatAccumulated()
119
      {
120
      mQuatAccumulated.set(mTempAccumulated);
121
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
    void setQuatCurrent()
126
      {
127
      mQuatCurrent.set(mTempCurrent);
128
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132
    Static4D getQuatAccumulated()
133
      {
134
      return mQuatAccumulated;
135
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
    Static4D getQuatCurrent()
140
      {
141
      return mQuatCurrent;
142
      }
143
144 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    void setMovement(RubikObjectMovement movement)
147
      {
148
      mMovement = movement;
149
      }
150
151 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    private Static4D quatFromDrag(float dragX, float dragY)
154
      {
155
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
156
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
157
      float axisZ = 0;
158
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
159
160
      if( axisL>0 )
161
        {
162
        axisX /= axisL;
163
        axisY /= axisL;
164
        axisZ /= axisL;
165
166
        float ratio = axisL;
167
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
168
169
        float cosA = (float)Math.cos(Math.PI*ratio);
170
        float sinA = (float)Math.sqrt(1-cosA*cosA);
171
172
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
173
        }
174
175
      return new Static4D(0f, 0f, 0f, 1f);
176
      }
177
178 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 9621255f Leszek Koltunski
    private void setUpDragOrRotate(boolean down, float x, float y)
181 12ad3fca Leszek Koltunski
      {
182 473611ee Leszek Koltunski
      int mode = RubikState.getMode();
183
184
      if( mode==MODE_DRAG )
185 12ad3fca Leszek Koltunski
        {
186 a6d3b158 Leszek Koltunski
        mDragging           = true;
187
        mBeginningRotation  = false;
188 12ad3fca Leszek Koltunski
        mContinuingRotation = false;
189
        }
190
      else
191
        {
192 a6d3b158 Leszek Koltunski
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
193
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
194
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
195
196
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
197
          {
198
          mDragging           = false;
199
          mContinuingRotation = false;
200 473611ee Leszek Koltunski
201
          if( mode==MODE_ROTATE )
202
            {
203
            mBeginningRotation= mPostRender.canRotate();
204
            }
205
          else if( mode==MODE_REPLACE )
206
            {
207
            mBeginningRotation= false;
208 9621255f Leszek Koltunski
209
            if( down )
210
              {
211
              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
212 46a961fd Leszek Koltunski
              mLastCubitFace = mMovement.getTouchedFace();
213 9621255f Leszek Koltunski
              float[] point = mMovement.getTouchedPoint3D();
214
              int color = solver.getCurrentColor();
215
              RubikObject object = mPostRender.getObject();
216 46a961fd Leszek Koltunski
              mLastCubit = object.getCubit(point);
217
              mPostRender.setTextureMap( mLastCubit, mLastCubitFace, color );
218
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
219 9621255f Leszek Koltunski
              }
220 473611ee Leszek Koltunski
            }
221 a6d3b158 Leszek Koltunski
          }
222
        else
223
          {
224 a42e25a6 Leszek Koltunski
          mDragging           = true;
225 a6d3b158 Leszek Koltunski
          mBeginningRotation  = false;
226
          mContinuingRotation = false;
227
          }
228 12ad3fca Leszek Koltunski
        }
229
      }
230
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
233
234
    private void computeCurrentAxis(Static3D axis)
235
      {
236
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
237
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
238
239
      mAxisX =result.get0();
240
      mAxisY =result.get1();
241
242
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
243
      mAxisX /= len;
244
      mAxisY /= len;
245
      }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
    private float continueRotation(float dx, float dy)
250
      {
251
      float alpha = dx*mAxisX + dy*mAxisY;
252
      float x = dx - alpha*mAxisX;
253
      float y = dy - alpha*mAxisY;
254
255
      float len = (float)Math.sqrt(x*x + y*y);
256
257 167ba2d6 Leszek Koltunski
      // we have the length of 1D vector 'angle', now the direction:
258
      float tmp = mAxisY==0 ? -mAxisX*y : mAxisY*x;
259 12ad3fca Leszek Koltunski
260 e46e17fb Leszek Koltunski
      return (tmp>0 ? 1:-1)*len*mRotationFactor;
261 12ad3fca Leszek Koltunski
      }
262
263 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264 47ba5ddc Leszek Koltunski
// return quat1*quat2
265 0c52af30 Leszek Koltunski
266 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
267 45686da2 Leszek Koltunski
      {
268 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
269
      float qy = quat1.get1();
270
      float qz = quat1.get2();
271
      float qw = quat1.get3();
272 45686da2 Leszek Koltunski
273 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
274
      float ry = quat2.get1();
275
      float rz = quat2.get2();
276
      float rw = quat2.get3();
277 45686da2 Leszek Koltunski
278 47ba5ddc Leszek Koltunski
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
279
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
280
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
281
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
282 45686da2 Leszek Koltunski
283 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
284 45686da2 Leszek Koltunski
      }
285
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
288 45686da2 Leszek Koltunski
289 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
290 45686da2 Leszek Koltunski
      {
291 348dfe69 Leszek Koltunski
      float qx = quat.get0();
292
      float qy = quat.get1();
293
      float qz = quat.get2();
294
      float qw = quat.get3();
295 45686da2 Leszek Koltunski
296 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
297 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
298 0c52af30 Leszek Koltunski
299 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
300 0c52af30 Leszek Koltunski
      }
301
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
304 0c52af30 Leszek Koltunski
305 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
306 0c52af30 Leszek Koltunski
      {
307 348dfe69 Leszek Koltunski
      float qx = quat.get0();
308
      float qy = quat.get1();
309
      float qz = quat.get2();
310
      float qw = quat.get3();
311 0c52af30 Leszek Koltunski
312 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
313 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
314 0c52af30 Leszek Koltunski
315 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
316 0c52af30 Leszek Koltunski
      }
317
318 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
319
// PUBLIC API
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321
322
    public RubikSurfaceView(Context context, AttributeSet attrs)
323
      {
324
      super(context,attrs);
325
326
      if(!isInEditMode())
327
        {
328 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
329
330 8becce57 Leszek Koltunski
        mRenderer   = new RubikRenderer(this);
331
        mPostRender = new RubikPostRender(this);
332 47ba5ddc Leszek Koltunski
333
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
334
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
335
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
336
        setRenderer(mRenderer);
337
        }
338
      }
339
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342
    @Override
343
    public boolean onTouchEvent(MotionEvent event)
344
      {
345
      int action = event.getAction();
346 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
347
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
348 47ba5ddc Leszek Koltunski
349
      switch(action)
350
         {
351
         case MotionEvent.ACTION_DOWN: mX = x;
352
                                       mY = y;
353 9621255f Leszek Koltunski
                                       setUpDragOrRotate(true,x,y);
354 47ba5ddc Leszek Koltunski
                                       break;
355 517f9fb9 Leszek Koltunski
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
356 47ba5ddc Leszek Koltunski
                                         {
357 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
358 47ba5ddc Leszek Koltunski
                                           {
359 12ad3fca Leszek Koltunski
                                           mStartRotX = x;
360
                                           mStartRotY = y;
361
362 86c73a69 Leszek Koltunski
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
363
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
364
365 e46e17fb Leszek Koltunski
                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
366 8becce57 Leszek Koltunski
                                           RubikObject object = mPostRender.getObject();
367 775e675d Leszek Koltunski
368 0e5ad27c Leszek Koltunski
                                           mCurrentAxis = (int)res.get0();
369 e46e17fb Leszek Koltunski
                                           float offset = res.get1();
370 0e5ad27c Leszek Koltunski
                                           mCurrentRow = (int)(object.returnMultiplier()*offset);
371
                                           computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
372 e46e17fb Leszek Koltunski
                                           mRotationFactor = object.returnRotationFactor(offset);
373 12ad3fca Leszek Koltunski
374 0e5ad27c Leszek Koltunski
                                           object.beginNewRotation( mCurrentAxis, mCurrentRow );
375 775e675d Leszek Koltunski
376 0333d81e Leszek Koltunski
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
377
                                             {
378
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
379
                                             solving.startCounting( (RubikActivity)getContext() );
380
                                             }
381
382 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
383
                                           mContinuingRotation= true;
384
                                           }
385
                                         }
386
                                       else if( mContinuingRotation )
387
                                         {
388 12ad3fca Leszek Koltunski
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
389 0e5ad27c Leszek Koltunski
                                         mCurrentAngle = SWIPING_SENSITIVITY*angle;
390
                                         mPostRender.getObject().continueRotation(mCurrentAngle);
391 47ba5ddc Leszek Koltunski
                                         }
392 bef47287 Leszek Koltunski
                                       else if( mDragging )
393 517f9fb9 Leszek Koltunski
                                         {
394
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
395 8becce57 Leszek Koltunski
                                         mPostRender.setQuatCurrentOnNextRender();
396 517f9fb9 Leszek Koltunski
397
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
398
                                           {
399
                                           mX = x;
400
                                           mY = y;
401
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
402
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
403 8becce57 Leszek Koltunski
                                           mPostRender.setQuatCurrentOnNextRender();
404
                                           mPostRender.setQuatAccumulatedOnNextRender();
405 517f9fb9 Leszek Koltunski
                                           }
406
                                         }
407 a42e25a6 Leszek Koltunski
                                       else
408 bef47287 Leszek Koltunski
                                         {
409 9621255f Leszek Koltunski
                                         setUpDragOrRotate(false,x,y);
410 bef47287 Leszek Koltunski
                                         }
411 47ba5ddc Leszek Koltunski
                                       break;
412
         case MotionEvent.ACTION_UP  : if( mDragging )
413
                                         {
414
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
415
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
416 8becce57 Leszek Koltunski
                                         mPostRender.setQuatCurrentOnNextRender();
417
                                         mPostRender.setQuatAccumulatedOnNextRender();
418 47ba5ddc Leszek Koltunski
                                         }
419
420
                                       if( mContinuingRotation )
421
                                         {
422 8becce57 Leszek Koltunski
                                         mPostRender.finishRotation();
423 0e5ad27c Leszek Koltunski
424
                                         if( RubikState.getCurrentState()==RubikState.SOLV )
425
                                           {
426
                                           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
427
428
                                           int angle = mPostRender.getObject().computeNearestAngle(mCurrentAngle);
429
430
                                           if( angle!=0 )
431
                                             solving.addMove(mCurrentAxis, mCurrentRow, angle);
432
                                           }
433 47ba5ddc Leszek Koltunski
                                         }
434 46a961fd Leszek Koltunski
                                       if( mLastCubitColor>=0 )
435
                                         {
436
                                          mPostRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
437
                                         }
438 47ba5ddc Leszek Koltunski
                                       break;
439
         }
440
441
      return true;
442
      }
443 0c52af30 Leszek Koltunski
}