Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 4e248bcc

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 12ad3fca Leszek Koltunski
78 45686da2 Leszek Koltunski
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
79
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
80
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
81
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
82 ee5c2ae1 Leszek Koltunski
83 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85
    void setScreenSize(int width, int height)
86
      {
87
      mScreenWidth = width;
88
      mScreenHeight= height;
89
90 12ad3fca Leszek Koltunski
      mScreenMin = Math.min(width, height);
91 0fd3ac1f Leszek Koltunski
      }
92
93 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
    boolean isVertical()
96
      {
97
      return mScreenHeight>mScreenWidth;
98
      }
99
100 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
    RubikRenderer getRenderer()
103
      {
104
      return mRenderer;
105
      }
106
107 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
    RubikPostRender getPostRender()
110
      {
111
      return mPostRender;
112
      }
113
114 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116
    void setQuatAccumulated()
117
      {
118
      mQuatAccumulated.set(mTempAccumulated);
119
      }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123
    void setQuatCurrent()
124
      {
125
      mQuatCurrent.set(mTempCurrent);
126
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
    Static4D getQuatAccumulated()
131
      {
132
      return mQuatAccumulated;
133
      }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137
    Static4D getQuatCurrent()
138
      {
139
      return mQuatCurrent;
140
      }
141
142 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
    void setMovement(RubikObjectMovement movement)
145
      {
146
      mMovement = movement;
147
      }
148
149 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
    private Static4D quatFromDrag(float dragX, float dragY)
152
      {
153
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
154
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
155
      float axisZ = 0;
156
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
157
158
      if( axisL>0 )
159
        {
160
        axisX /= axisL;
161
        axisY /= axisL;
162
        axisZ /= axisL;
163
164
        float ratio = axisL;
165
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
166
167
        float cosA = (float)Math.cos(Math.PI*ratio);
168
        float sinA = (float)Math.sqrt(1-cosA*cosA);
169
170
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
171
        }
172
173
      return new Static4D(0f, 0f, 0f, 1f);
174
      }
175
176 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178 9621255f Leszek Koltunski
    private void setUpDragOrRotate(boolean down, float x, float y)
179 12ad3fca Leszek Koltunski
      {
180 473611ee Leszek Koltunski
      int mode = RubikState.getMode();
181
182
      if( mode==MODE_DRAG )
183 12ad3fca Leszek Koltunski
        {
184 a6d3b158 Leszek Koltunski
        mDragging           = true;
185
        mBeginningRotation  = false;
186 12ad3fca Leszek Koltunski
        mContinuingRotation = false;
187
        }
188
      else
189
        {
190 a6d3b158 Leszek Koltunski
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
191
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
192
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
193
194
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
195
          {
196
          mDragging           = false;
197
          mContinuingRotation = false;
198 473611ee Leszek Koltunski
199
          if( mode==MODE_ROTATE )
200
            {
201
            mBeginningRotation= mPostRender.canRotate();
202
            }
203
          else if( mode==MODE_REPLACE )
204
            {
205
            mBeginningRotation= false;
206 9621255f Leszek Koltunski
207
            if( down )
208
              {
209
              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
210 46a961fd Leszek Koltunski
              mLastCubitFace = mMovement.getTouchedFace();
211 9621255f Leszek Koltunski
              float[] point = mMovement.getTouchedPoint3D();
212
              int color = solver.getCurrentColor();
213
              RubikObject object = mPostRender.getObject();
214 46a961fd Leszek Koltunski
              mLastCubit = object.getCubit(point);
215
              mPostRender.setTextureMap( mLastCubit, mLastCubitFace, color );
216
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
217 9621255f Leszek Koltunski
              }
218 473611ee Leszek Koltunski
            }
219 a6d3b158 Leszek Koltunski
          }
220
        else
221
          {
222 a42e25a6 Leszek Koltunski
          mDragging           = true;
223 a6d3b158 Leszek Koltunski
          mBeginningRotation  = false;
224
          mContinuingRotation = false;
225
          }
226 12ad3fca Leszek Koltunski
        }
227
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
231
232
    private void computeCurrentAxis(Static3D axis)
233
      {
234
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
235
      Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
236
237
      mAxisX =result.get0();
238
      mAxisY =result.get1();
239
240
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
241
      mAxisX /= len;
242
      mAxisY /= len;
243
      }
244
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    private float continueRotation(float dx, float dy)
248
      {
249
      float alpha = dx*mAxisX + dy*mAxisY;
250
      float x = dx - alpha*mAxisX;
251
      float y = dy - alpha*mAxisY;
252
253
      float len = (float)Math.sqrt(x*x + y*y);
254
255 167ba2d6 Leszek Koltunski
      // we have the length of 1D vector 'angle', now the direction:
256
      float tmp = mAxisY==0 ? -mAxisX*y : mAxisY*x;
257 12ad3fca Leszek Koltunski
258 e46e17fb Leszek Koltunski
      return (tmp>0 ? 1:-1)*len*mRotationFactor;
259 12ad3fca Leszek Koltunski
      }
260
261 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262 47ba5ddc Leszek Koltunski
// return quat1*quat2
263 0c52af30 Leszek Koltunski
264 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
265 45686da2 Leszek Koltunski
      {
266 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
267
      float qy = quat1.get1();
268
      float qz = quat1.get2();
269
      float qw = quat1.get3();
270 45686da2 Leszek Koltunski
271 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
272
      float ry = quat2.get1();
273
      float rz = quat2.get2();
274
      float rw = quat2.get3();
275 45686da2 Leszek Koltunski
276 47ba5ddc Leszek Koltunski
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
277
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
278
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
279
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
280 45686da2 Leszek Koltunski
281 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
282 45686da2 Leszek Koltunski
      }
283
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
286 45686da2 Leszek Koltunski
287 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
288 45686da2 Leszek Koltunski
      {
289 348dfe69 Leszek Koltunski
      float qx = quat.get0();
290
      float qy = quat.get1();
291
      float qz = quat.get2();
292
      float qw = quat.get3();
293 45686da2 Leszek Koltunski
294 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
295 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
296 0c52af30 Leszek Koltunski
297 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
298 0c52af30 Leszek Koltunski
      }
299
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
302 0c52af30 Leszek Koltunski
303 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
304 0c52af30 Leszek Koltunski
      {
305 348dfe69 Leszek Koltunski
      float qx = quat.get0();
306
      float qy = quat.get1();
307
      float qz = quat.get2();
308
      float qw = quat.get3();
309 0c52af30 Leszek Koltunski
310 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
311 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
312 0c52af30 Leszek Koltunski
313 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
314 0c52af30 Leszek Koltunski
      }
315
316 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
317
// PUBLIC API
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320
    public RubikSurfaceView(Context context, AttributeSet attrs)
321
      {
322
      super(context,attrs);
323
324
      if(!isInEditMode())
325
        {
326 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
327
328 8becce57 Leszek Koltunski
        mRenderer   = new RubikRenderer(this);
329
        mPostRender = new RubikPostRender(this);
330 47ba5ddc Leszek Koltunski
331
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
332
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
333
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
334
        setRenderer(mRenderer);
335
        }
336
      }
337
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
    @Override
341
    public boolean onTouchEvent(MotionEvent event)
342
      {
343
      int action = event.getAction();
344 775e675d Leszek Koltunski
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
345
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
346 47ba5ddc Leszek Koltunski
347
      switch(action)
348
         {
349
         case MotionEvent.ACTION_DOWN: mX = x;
350
                                       mY = y;
351 9621255f Leszek Koltunski
                                       setUpDragOrRotate(true,x,y);
352 47ba5ddc Leszek Koltunski
                                       break;
353 517f9fb9 Leszek Koltunski
         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
354 47ba5ddc Leszek Koltunski
                                         {
355 775e675d Leszek Koltunski
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
356 47ba5ddc Leszek Koltunski
                                           {
357 12ad3fca Leszek Koltunski
                                           mStartRotX = x;
358
                                           mStartRotY = y;
359
360 86c73a69 Leszek Koltunski
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
361
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
362
363 e46e17fb Leszek Koltunski
                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
364 8becce57 Leszek Koltunski
                                           RubikObject object = mPostRender.getObject();
365 775e675d Leszek Koltunski
366 e46e17fb Leszek Koltunski
                                           int axis = (int)res.get0();
367
                                           float offset = res.get1();
368 12ad3fca Leszek Koltunski
                                           computeCurrentAxis( object.getRotationAxis()[axis] );
369 e46e17fb Leszek Koltunski
                                           mRotationFactor = object.returnRotationFactor(offset);
370 12ad3fca Leszek Koltunski
371 9621255f Leszek Koltunski
                                           object.beginNewRotation( axis, (int)(object.returnMultiplier()*offset) );
372 775e675d Leszek Koltunski
373 0333d81e Leszek Koltunski
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
374
                                             {
375
                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
376
                                             solving.startCounting( (RubikActivity)getContext() );
377
                                             }
378
379 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
380
                                           mContinuingRotation= true;
381
                                           }
382
                                         }
383
                                       else if( mContinuingRotation )
384
                                         {
385 12ad3fca Leszek Koltunski
                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
386 8becce57 Leszek Koltunski
                                         mPostRender.getObject().continueRotation(SWIPING_SENSITIVITY*angle);
387 47ba5ddc Leszek Koltunski
                                         }
388 bef47287 Leszek Koltunski
                                       else if( mDragging )
389 517f9fb9 Leszek Koltunski
                                         {
390
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
391 8becce57 Leszek Koltunski
                                         mPostRender.setQuatCurrentOnNextRender();
392 517f9fb9 Leszek Koltunski
393
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
394
                                           {
395
                                           mX = x;
396
                                           mY = y;
397
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
398
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
399 8becce57 Leszek Koltunski
                                           mPostRender.setQuatCurrentOnNextRender();
400
                                           mPostRender.setQuatAccumulatedOnNextRender();
401 517f9fb9 Leszek Koltunski
                                           }
402
                                         }
403 a42e25a6 Leszek Koltunski
                                       else
404 bef47287 Leszek Koltunski
                                         {
405 9621255f Leszek Koltunski
                                         setUpDragOrRotate(false,x,y);
406 bef47287 Leszek Koltunski
                                         }
407 47ba5ddc Leszek Koltunski
                                       break;
408
         case MotionEvent.ACTION_UP  : if( mDragging )
409
                                         {
410
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
411
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
412 8becce57 Leszek Koltunski
                                         mPostRender.setQuatCurrentOnNextRender();
413
                                         mPostRender.setQuatAccumulatedOnNextRender();
414 47ba5ddc Leszek Koltunski
                                         }
415
416
                                       if( mContinuingRotation )
417
                                         {
418 8becce57 Leszek Koltunski
                                         mPostRender.finishRotation();
419 47ba5ddc Leszek Koltunski
                                         }
420 46a961fd Leszek Koltunski
                                       if( mLastCubitColor>=0 )
421
                                         {
422
                                          mPostRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
423
                                         }
424 47ba5ddc Leszek Koltunski
                                       break;
425
         }
426
427
      return true;
428
      }
429 0c52af30 Leszek Koltunski
}