Project

General

Profile

Download (17.3 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / rubik / RubikSurfaceView.java @ d4374cd3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted is distributed in the hope that it will be useful,                                  //
12
// 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
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.rubik;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.view.MotionEvent;
27

    
28
import org.distorted.library.type.Static4D;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
class RubikSurfaceView extends GLSurfaceView
33
{
34
    private final static int NONE   =-1;
35
    private final static int FRONT  = 0;  // has to be 6 consecutive ints
36
    private final static int BACK   = 1;  // FRONT ... BOTTOM
37
    private final static int LEFT   = 2;  //
38
    private final static int RIGHT  = 3;  //
39
    private final static int TOP    = 4;  //
40
    private final static int BOTTOM = 5;  //
41

    
42
    static final int VECTX = 0;  //
43
    static final int VECTY = 1;  // dont change this
44
    static final int VECTZ = 2;  //
45

    
46
    private static final int[] VECT = {VECTX,VECTY,VECTZ};
47

    
48
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
49
    private int mX, mY;
50
    private Static4D mQuatCurrent, mQuatAccumulated;
51
    private int mRotationVect;
52
    private RubikRenderer mRenderer;
53
    private RubikCube mCube;
54

    
55
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
56
    private int mLastTouchedFace;
57
    private int mScreenWidth, mScreenHeight, mScreenMin;
58
    private float mCameraDistance;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    public RubikSurfaceView(Context context)
63
      {
64
      super(context);
65

    
66
      mRotationVect = VECT[0];
67

    
68
      mPoint = new float[3];
69
      mCamera= new float[3];
70
      mDiff  = new float[3];
71
      mTouchPoint = new float[3];
72
      mTouchPointCastOntoFace = new float[3];
73

    
74
      mScreenWidth = mScreenHeight = mScreenMin = 0;
75

    
76
      mRenderer = new RubikRenderer(this);
77
      mCube = mRenderer.getCube();
78

    
79
      mQuatCurrent     = new Static4D(0,0,0,1);
80
      mQuatAccumulated = mRenderer.initializeQuat();
81

    
82
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
83
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
84
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
85
      setRenderer(mRenderer);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
    public RubikRenderer getRenderer()
91
      {
92
      return mRenderer;
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
    @Override
98
    public boolean onTouchEvent(MotionEvent event)
99
      {
100
      int action = event.getAction();
101
      int x = (int)event.getX();
102
      int y = (int)event.getY();
103

    
104
      switch(action)
105
         {
106
         case MotionEvent.ACTION_DOWN: mX = x;
107
                                       mY = y;
108
                                       mLastTouchedFace = faceTouched(x,y);
109

    
110
                                       if( mLastTouchedFace != NONE )
111
                                         {
112
                                         mDragging           = false;
113
                                         mBeginningRotation  = mRenderer.canRotate();
114
                                         mContinuingRotation = false;
115
                                         }
116
                                       else
117
                                         {
118
                                         mDragging           = true;
119
                                         mBeginningRotation  = false;
120
                                         mContinuingRotation = false;
121
                                         }
122
                                       break;
123
         case MotionEvent.ACTION_MOVE: if( mDragging )
124
                                         {
125
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
126
                                         mRenderer.setQuatCurrent(mQuatCurrent);
127
                                         }
128
                                       if( mBeginningRotation )
129
                                         {
130
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100;
131

    
132
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
133
                                           {
134
                                           addNewRotation(x,y);
135
                                           mBeginningRotation = false;
136
                                           mContinuingRotation= true;
137
                                           }
138
                                         }
139
                                       else if( mContinuingRotation )
140
                                         {
141
                                         continueRotation(x,y);
142
                                         }
143
                                       break;
144
         case MotionEvent.ACTION_UP  : if( mDragging )
145
                                         {
146
                                         mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
147
                                         mQuatCurrent.set(0f, 0f, 0f, 1f);
148
                                         mRenderer.setQuatCurrent(mQuatCurrent);
149
                                         mRenderer.setQuatAccumulated(mQuatAccumulated);
150
                                         }
151

    
152
                                       if( mContinuingRotation )
153
                                         {
154
                                         finishRotation();
155
                                         }
156

    
157
                                       break;
158
         }
159

    
160
      return true;
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
    void setScreenSize(int width, int height)
166
      {
167
      mScreenWidth = width;
168
      mScreenHeight= height;
169

    
170
      mScreenMin = width<height ? width:height;
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    void setCameraDist(float distance)
176
      {
177
      mCameraDistance = distance;
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    private int faceTouched(int xTouch, int yTouch)
183
      {
184
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
185

    
186
      convertTouchPointToScreenSpace(xTouch,yTouch);
187
      convertCameraPointToScreenSpace();
188

    
189
      for(int face=FRONT; face<=BOTTOM; face++)
190
        {
191
        if( faceIsVisible(face,cubeHalfSize) )
192
          {
193
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
194

    
195
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
196
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
197
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
198

    
199
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
200
          }
201
        }
202

    
203
      return NONE;
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    private void addNewRotation(int x, int y)
209
      {
210
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
211

    
212
      convertTouchPointToScreenSpace(x,y);
213
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
214

    
215
      mDiff[0] -= mTouchPointCastOntoFace[0];
216
      mDiff[1] -= mTouchPointCastOntoFace[1];
217
      mDiff[2] -= mTouchPointCastOntoFace[2];
218

    
219
      int xAxis = retFaceXaxis(mLastTouchedFace);
220
      int yAxis = retFaceYaxis(mLastTouchedFace);
221
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
222
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
223

    
224
      mTouchPoint[0] = mPoint[0];
225
      mTouchPoint[1] = mPoint[1];
226
      mTouchPoint[2] = mPoint[2];
227

    
228
      mCube.addNewRotation(mRotationVect,offset);
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    private boolean isVertical(float x, float y)
234
      {
235
      return (y>x) ? (y>=-x) : (y< -x);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
240
// given face by 240/2 = 120 degrees.
241

    
242
    private void continueRotation(int x, int y)
243
      {
244
      convertTouchPointToScreenSpace(x,y);
245

    
246
      mDiff[0] = mPoint[0]-mTouchPoint[0];
247
      mDiff[1] = mPoint[1]-mTouchPoint[1];
248
      mDiff[2] = mPoint[2]-mTouchPoint[2];
249

    
250
      int xAxis= retFaceXaxis(mLastTouchedFace);
251
      int yAxis= retFaceYaxis(mLastTouchedFace);
252
      int sign = retFaceRotationSign(mLastTouchedFace);
253
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
254

    
255
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    private void finishRotation()
261
      {
262
      mRenderer.finishRotation();
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// return quat1*quat2
267

    
268
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
269
      {
270
      float qx = quat1.get1();
271
      float qy = quat1.get2();
272
      float qz = quat1.get3();
273
      float qw = quat1.get4();
274

    
275
      float rx = quat2.get1();
276
      float ry = quat2.get2();
277
      float rz = quat2.get3();
278
      float rw = quat2.get4();
279

    
280
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
281
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
282
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
283
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
284

    
285
      return new Static4D(tx,ty,tz,tw);
286
      }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
290

    
291
    static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
292
      {
293
      float qx = quat.get1();
294
      float qy = quat.get2();
295
      float qz = quat.get3();
296
      float qw = quat.get4();
297

    
298
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
299
      Static4D tmp = quatMultiply(quatInverted,vector);
300

    
301
      return quatMultiply(tmp,quat);
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
306

    
307
    static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
308
      {
309
      float qx = quat.get1();
310
      float qy = quat.get2();
311
      float qz = quat.get3();
312
      float qw = quat.get4();
313

    
314
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
315
      Static4D tmp = quatMultiply(quat,vector);
316

    
317
      return quatMultiply(tmp,quatInverted);
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    private Static4D quatFromDrag(float dragX, float dragY)
323
      {
324
      float axisX = dragY;  // inverted X and Y - rotation axis is
325
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
326
      float axisZ = 0;
327
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
328

    
329
      if( axisL>0 )
330
        {
331
        axisX /= axisL;
332
        axisY /= axisL;
333
        axisZ /= axisL;
334

    
335
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
336
        float sinA = (float)Math.sqrt(1-cosA*cosA);
337

    
338
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
339
        }
340

    
341
      return new Static4D(0f, 0f, 0f, 1f);
342
      }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
    private boolean faceIsVisible(int face, float cubeHalfSize)
347
      {
348
      int sign = retFaceSign(face);
349
      int zAxis= retFaceZaxis(face);
350

    
351
      return sign*mCamera[zAxis] > cubeHalfSize;
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
    private void convertTouchPointToScreenSpace(int x, int y)
357
      {
358
      float halfScrWidth  = mScreenWidth *0.5f;
359
      float halfScrHeight = mScreenHeight*0.5f;
360
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
361
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
362

    
363
      mPoint[0] = rotatedTouchPoint.get1();
364
      mPoint[1] = rotatedTouchPoint.get2();
365
      mPoint[2] = rotatedTouchPoint.get3();
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    private void convertCameraPointToScreenSpace()
371
      {
372
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
373
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
374

    
375
      mCamera[0] = rotatedCamera.get1();
376
      mCamera[1] = rotatedCamera.get2();
377
      mCamera[2] = rotatedCamera.get3();
378
      }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
382
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
383
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
384

    
385
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
386
      {
387
      int sign = retFaceSign(face);
388
      int zAxis= retFaceZaxis(face);
389
      float diff = mPoint[zAxis]-mCamera[zAxis];
390

    
391
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
392

    
393
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
394
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
395
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
396
      }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
    private int retFaceSign(int face)
401
      {
402
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
403
      }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    private int retFaceRotationSign(int face)
408
      {
409
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
410
      }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
414
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
415
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
416

    
417
    private int retFaceXaxis(int face)
418
      {
419
      switch(face)
420
        {
421
        case FRONT :
422
        case BACK  : return VECTX;
423
        case LEFT  :
424
        case RIGHT : return VECTZ;
425
        case TOP   :
426
        case BOTTOM: return VECTX;
427
        }
428

    
429
      return -1;
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    private int retFaceYaxis(int face)
435
      {
436
      switch(face)
437
        {
438
        case FRONT :
439
        case BACK  : return VECTY;
440
        case LEFT  :
441
        case RIGHT : return VECTY;
442
        case TOP   :
443
        case BOTTOM: return VECTZ;
444
        }
445

    
446
      return -1;
447
      }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
    private int retFaceZaxis(int face)
452
      {
453
      switch(face)
454
        {
455
        case FRONT :
456
        case BACK  : return VECTZ;
457
        case LEFT  :
458
        case RIGHT : return VECTX;
459
        case TOP   :
460
        case BOTTOM: return VECTY;
461
        }
462

    
463
      return -1;
464
      }
465
}
466

    
(4-4/4)