Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikSurfaceView.java @ 7f62afde

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 recomputeCameraDistance(double fovInRadians)
164
      {
165
      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
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

    
228
    private void continueRotation(int x, int y)
229
      {
230
      convertTouchPointToScreenSpace(x,y);
231

    
232
      mDiff[0] = mPoint[0]-mTouchPoint[0];
233
      mDiff[1] = mPoint[1]-mTouchPoint[1];
234
      mDiff[2] = mPoint[2]-mTouchPoint[2];
235

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

    
241
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    private void finishRotation()
247
      {
248
      mRenderer.finishRotation();
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
// return quat1*quat2
253

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

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

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

    
271
      return new Static4D(tx,ty,tz,tw);
272
      }
273

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

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

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

    
287
      return quatMultiply(tmp,quat);
288
      }
289

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

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

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

    
303
      return quatMultiply(tmp,quatInverted);
304
      }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

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

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

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

    
324
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
325
        }
326

    
327
      return new Static4D(0f, 0f, 0f, 1f);
328
      }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

    
337
      return sign*mCamera[zAxis] > cubeHalfSize;
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

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

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

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

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

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

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

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

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

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

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

    
412
      return -1;
413
      }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

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

    
429
      return -1;
430
      }
431

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

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

    
446
      return -1;
447
      }
448
}
449

    
(4-4/4)