Project

General

Profile

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

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

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
      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 ) { mBeginRot = true; mDragging = false; }
111
                                       else                           { mDragging = true; mBeginRot = false; }
112
                                       break;
113
         case MotionEvent.ACTION_MOVE: if( mDragging )
114
                                         {
115
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
116
                                         mRenderer.setQuatCurrent(mQuatCurrent);
117
                                         }
118
                                       else if( mBeginRot )
119
                                         {
120
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100;
121

    
122
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
123
                                           {
124
                                           addNewRotation(x,y);
125
                                           mBeginRot = false;
126
                                           }
127
                                         }
128
                                       else
129
                                         {
130
                                         continueRotation(x,y);
131
                                         }
132
                                       break;
133
         case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
134
                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
135
                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
136
                                       mRenderer.setQuatCurrent(mQuatCurrent);
137
                                       mRenderer.setQuatAccumulated(mQuatAccumulated);
138
                                       break;
139
         }
140

    
141
      return true;
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    void setScreenSize(int width, int height)
147
      {
148
      mScreenWidth = width;
149
      mScreenHeight= height;
150

    
151
      mScreenMin = width<height ? width:height;
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    void setCameraDist(float distance)
157
      {
158
      mCameraDistance = distance;
159
      }
160

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

    
163
    private int faceTouched(int xTouch, int yTouch)
164
      {
165
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
166

    
167
      convertTouchPointToScreenSpace(xTouch,yTouch);
168
      convertCameraPointToScreenSpace();
169

    
170
      for(int face=FRONT; face<=BOTTOM; face++)
171
        {
172
        if( faceIsVisible(face,cubeHalfSize) )
173
          {
174
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
175

    
176
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
177
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
178
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
179

    
180
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
181
          }
182
        }
183

    
184
      return NONE;
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    private void addNewRotation(int x, int y)
190
      {
191
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
192

    
193
      convertTouchPointToScreenSpace(x,y);
194
      castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
195

    
196
      mDiff[0] -= mTouchPointCastOntoFace[0];
197
      mDiff[1] -= mTouchPointCastOntoFace[1];
198
      mDiff[2] -= mTouchPointCastOntoFace[2];
199

    
200
      int xAxis = retFaceXaxis(mLastTouchedFace);
201
      int yAxis = retFaceYaxis(mLastTouchedFace);
202
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
203
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
204

    
205
      mTouchPoint[0] = mPoint[0];
206
      mTouchPoint[1] = mPoint[1];
207
      mTouchPoint[2] = mPoint[2];
208

    
209
      mCube.addNewRotation(mRotationVect,offset);
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    private boolean isVertical(float x, float y)
215
      {
216
      return (y>x) ? (y>=-x) : (y< -x);
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
221
// given face by 240/2 = 120 degrees.
222

    
223
    private void continueRotation(int x, int y)
224
      {
225
      convertTouchPointToScreenSpace(x,y);
226

    
227
      mDiff[0] = mPoint[0]-mTouchPoint[0];
228
      mDiff[1] = mPoint[1]-mTouchPoint[1];
229
      mDiff[2] = mPoint[2]-mTouchPoint[2];
230

    
231
      int xAxis= retFaceXaxis(mLastTouchedFace);
232
      int yAxis= retFaceYaxis(mLastTouchedFace);
233
      int sign = retFaceRotationSign(mLastTouchedFace);
234
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
235

    
236
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    private void finishRotation()
242
      {
243
      mRenderer.finishRotation();
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
// return quat1*quat2
248

    
249
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
250
      {
251
      float qx = quat1.get1();
252
      float qy = quat1.get2();
253
      float qz = quat1.get3();
254
      float qw = quat1.get4();
255

    
256
      float rx = quat2.get1();
257
      float ry = quat2.get2();
258
      float rz = quat2.get3();
259
      float rw = quat2.get4();
260

    
261
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
262
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
263
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
264
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
265

    
266
      return new Static4D(tx,ty,tz,tw);
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
271

    
272
    static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
273
      {
274
      float qx = quat.get1();
275
      float qy = quat.get2();
276
      float qz = quat.get3();
277
      float qw = quat.get4();
278

    
279
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
280
      Static4D tmp = quatMultiply(quatInverted,vector);
281

    
282
      return quatMultiply(tmp,quat);
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
287

    
288
    static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
289
      {
290
      float qx = quat.get1();
291
      float qy = quat.get2();
292
      float qz = quat.get3();
293
      float qw = quat.get4();
294

    
295
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
296
      Static4D tmp = quatMultiply(quat,vector);
297

    
298
      return quatMultiply(tmp,quatInverted);
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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

    
310
      if( axisL>0 )
311
        {
312
        axisX /= axisL;
313
        axisY /= axisL;
314
        axisZ /= axisL;
315

    
316
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
317
        float sinA = (float)Math.sqrt(1-cosA*cosA);
318

    
319
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
320
        }
321

    
322
      return new Static4D(0f, 0f, 0f, 1f);
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    private boolean faceIsVisible(int face, float cubeHalfSize)
328
      {
329
      int sign = retFaceSign(face);
330
      int zAxis= retFaceZaxis(face);
331

    
332
      return sign*mCamera[zAxis] > cubeHalfSize;
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    private void convertTouchPointToScreenSpace(int x, int y)
338
      {
339
      float halfScrWidth  = mScreenWidth *0.5f;
340
      float halfScrHeight = mScreenHeight*0.5f;
341
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
342
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
343

    
344
      mPoint[0] = rotatedTouchPoint.get1();
345
      mPoint[1] = rotatedTouchPoint.get2();
346
      mPoint[2] = rotatedTouchPoint.get3();
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
    private void convertCameraPointToScreenSpace()
352
      {
353
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
354
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
355

    
356
      mCamera[0] = rotatedCamera.get1();
357
      mCamera[1] = rotatedCamera.get2();
358
      mCamera[2] = rotatedCamera.get3();
359
      }
360

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

    
366
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
367
      {
368
      int sign = retFaceSign(face);
369
      int zAxis= retFaceZaxis(face);
370
      float diff = mPoint[zAxis]-mCamera[zAxis];
371

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

    
374
      output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
375
      output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
376
      output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
377
      }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    private int retFaceSign(int face)
382
      {
383
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
384
      }
385

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

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

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

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

    
410
      return -1;
411
      }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

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

    
427
      return -1;
428
      }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

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

    
444
      return -1;
445
      }
446
}
447

    
(4-4/4)