Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 9621255f

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