Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikSurfaceView.java @ 483ae94e

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, mBeginRot;
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
      mDragging = false;
67
      mBeginRot = false;
68
      mRotationVect = VECT[0];
69

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

    
76
      mScreenWidth = mScreenHeight = mScreenMin = 0;
77

    
78
      mRenderer = new RubikRenderer(this);
79
      mCube = mRenderer.getCube();
80

    
81
      mQuatCurrent     = new Static4D(0,0,0,1);
82
      mQuatAccumulated = mRenderer.initializeQuat();
83

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
    public RubikRenderer getRenderer()
93
      {
94
      return mRenderer;
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

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

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

    
112
                                       if( mLastTouchedFace != NONE ) mBeginRot = true;
113
                                       else                           mDragging = true;
114

    
115
                                       break;
116
         case MotionEvent.ACTION_MOVE: if( mDragging )
117
                                         {
118
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
119
                                         mRenderer.setQuatCurrent(mQuatCurrent);
120
                                         }
121
                                       else if( mBeginRot )
122
                                         {
123
                                         int minimumToRotate = (mScreenMin*mScreenMin)/100;
124

    
125
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
126
                                           {
127
                                           addNewRotation(x,y);
128
                                           mBeginRot = false;
129
                                           }
130
                                         }
131
                                       else
132
                                         {
133
                                         continueRotation(x,y);
134
                                         }
135
                                       break;
136
         case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
137

    
138
                                       mDragging = false;
139
                                       mBeginRot = false;
140

    
141
                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
142
                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
143
                                       mRenderer.setQuatCurrent(mQuatCurrent);
144
                                       mRenderer.setQuatAccumulated(mQuatAccumulated);
145
                                       break;
146
         }
147

    
148
      return true;
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    void setScreenSize(int width, int height)
154
      {
155
      mScreenWidth = width;
156
      mScreenHeight= height;
157

    
158
      mScreenMin = width<height ? width:height;
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    void setCameraDist(float distance)
164
      {
165
      mCameraDistance = distance;
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    private int faceTouched(int xTouch, int yTouch)
171
      {
172
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
173

    
174
      convertTouchPointToScreenSpace(xTouch,yTouch);
175
      convertCameraPointToScreenSpace();
176

    
177
      for(int face=FRONT; face<=BOTTOM; face++)
178
        {
179
        if( faceIsVisible(face,cubeHalfSize) )
180
          {
181
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
182

    
183
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
184
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
185
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
186

    
187
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
188
          }
189
        }
190

    
191
      return NONE;
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    private void addNewRotation(int x, int y)
197
      {
198
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
199

    
200
      convertTouchPointToScreenSpace(x,y);
201
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
202

    
203
      mDiff[0] -= mTouchPointCastOntoFace[0];
204
      mDiff[1] -= mTouchPointCastOntoFace[1];
205
      mDiff[2] -= mTouchPointCastOntoFace[2];
206

    
207
      int xAxis = retFaceXaxis(mLastTouchedFace);
208
      int yAxis = retFaceYaxis(mLastTouchedFace);
209
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
210
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
211

    
212
      mTouchPoint[0] = mPoint[0];
213
      mTouchPoint[1] = mPoint[1];
214
      mTouchPoint[2] = mPoint[2];
215

    
216
      mCube.addNewRotation(mRotationVect,offset);
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private boolean isVertical(float x, float y)
222
      {
223
      return (y>x) ? (y>=-x) : (y< -x);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
228
// given face by 240/2 = 120 degrees.
229

    
230
    private void continueRotation(int x, int y)
231
      {
232
      convertTouchPointToScreenSpace(x,y);
233

    
234
      mDiff[0] = mPoint[0]-mTouchPoint[0];
235
      mDiff[1] = mPoint[1]-mTouchPoint[1];
236
      mDiff[2] = mPoint[2]-mTouchPoint[2];
237

    
238
      int xAxis= retFaceXaxis(mLastTouchedFace);
239
      int yAxis= retFaceYaxis(mLastTouchedFace);
240
      int sign = retFaceRotationSign(mLastTouchedFace);
241
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
242

    
243
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    private void finishRotation()
249
      {
250
      mRenderer.finishRotation();
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
// return quat1*quat2
255

    
256
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
257
      {
258
      float qx = quat1.get1();
259
      float qy = quat1.get2();
260
      float qz = quat1.get3();
261
      float qw = quat1.get4();
262

    
263
      float rx = quat2.get1();
264
      float ry = quat2.get2();
265
      float rz = quat2.get3();
266
      float rw = quat2.get4();
267

    
268
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
269
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
270
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
271
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
272

    
273
      return new Static4D(tx,ty,tz,tw);
274
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
278

    
279
    static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
280
      {
281
      float qx = quat.get1();
282
      float qy = quat.get2();
283
      float qz = quat.get3();
284
      float qw = quat.get4();
285

    
286
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
287
      Static4D tmp = quatMultiply(quatInverted,vector);
288

    
289
      return quatMultiply(tmp,quat);
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
294

    
295
    static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
296
      {
297
      float qx = quat.get1();
298
      float qy = quat.get2();
299
      float qz = quat.get3();
300
      float qw = quat.get4();
301

    
302
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
303
      Static4D tmp = quatMultiply(quat,vector);
304

    
305
      return quatMultiply(tmp,quatInverted);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    private Static4D quatFromDrag(float dragX, float dragY)
311
      {
312
      float axisX = dragY;  // inverted X and Y - rotation axis is
313
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
314
      float axisZ = 0;
315
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
316

    
317
      if( axisL>0 )
318
        {
319
        axisX /= axisL;
320
        axisY /= axisL;
321
        axisZ /= axisL;
322

    
323
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
324
        float sinA = (float)Math.sqrt(1-cosA*cosA);
325

    
326
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
327
        }
328

    
329
      return new Static4D(0f, 0f, 0f, 1f);
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    private boolean faceIsVisible(int face, float cubeHalfSize)
335
      {
336
      int sign = retFaceSign(face);
337
      int zAxis= retFaceZaxis(face);
338

    
339
      return sign*mCamera[zAxis] > cubeHalfSize;
340
      }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
    private void convertTouchPointToScreenSpace(int x, int y)
345
      {
346
      float halfScrWidth  = mScreenWidth *0.5f;
347
      float halfScrHeight = mScreenHeight*0.5f;
348
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
349
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
350

    
351
      mPoint[0] = rotatedTouchPoint.get1();
352
      mPoint[1] = rotatedTouchPoint.get2();
353
      mPoint[2] = rotatedTouchPoint.get3();
354
      }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
    private void convertCameraPointToScreenSpace()
359
      {
360
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
361
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
362

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

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369
// given precomputed mCam and mPoi, respectively camera and touch point positions in ScreenSpace,
370
// cast this touch point onto the 'face' and write the cast coords to 'output'.
371
// Center of the 'face' = (0,0)
372

    
373
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
374
      {
375
      int sign = retFaceSign(face);
376
      int zAxis= retFaceZaxis(face);
377
      float diff = mPoint[zAxis]-mCamera[zAxis];
378

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

    
381
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
382
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
383
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
384
      }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
    private int retFaceSign(int face)
389
      {
390
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
391
      }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
    private int retFaceRotationSign(int face)
396
      {
397
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
398
      }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
    private int retFaceXaxis(int face)
403
      {
404
      switch(face)
405
        {
406
        case FRONT :
407
        case BACK  : return VECTX;
408
        case LEFT  :
409
        case RIGHT : return VECTZ;
410
        case TOP   :
411
        case BOTTOM: return VECTX;
412
        }
413

    
414
      return -1;
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    private int retFaceYaxis(int face)
420
      {
421
      switch(face)
422
        {
423
        case FRONT :
424
        case BACK  : return VECTY;
425
        case LEFT  :
426
        case RIGHT : return VECTY;
427
        case TOP   :
428
        case BOTTOM: return VECTZ;
429
        }
430

    
431
      return -1;
432
      }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

    
448
      return -1;
449
      }
450
}
451

    
(4-4/4)