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.util.AttributeSet;
|
27
|
import android.view.MotionEvent;
|
28
|
|
29
|
import org.distorted.library.type.Static4D;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
class RubikSurfaceView extends GLSurfaceView
|
34
|
{
|
35
|
private final static int NONE =-1;
|
36
|
private final static int FRONT = 0; // has to be 6 consecutive ints
|
37
|
private final static int BACK = 1; // FRONT ... BOTTOM
|
38
|
private final static int LEFT = 2; //
|
39
|
private final static int RIGHT = 3; //
|
40
|
private final static int TOP = 4; //
|
41
|
private final static int BOTTOM = 5; //
|
42
|
|
43
|
static final int VECTX = 0; //
|
44
|
static final int VECTY = 1; // dont change this
|
45
|
static final int VECTZ = 2; //
|
46
|
|
47
|
private static final int[] VECT = {VECTX,VECTY,VECTZ};
|
48
|
|
49
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
50
|
private int mX, mY;
|
51
|
private Static4D mQuatCurrent, mQuatAccumulated;
|
52
|
private int mRotationVect;
|
53
|
private RubikRenderer mRenderer;
|
54
|
private RubikCube mCube;
|
55
|
|
56
|
private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
|
57
|
private int mLastTouchedFace;
|
58
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
59
|
private float mCameraDistance;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
public RubikSurfaceView(Context context, AttributeSet attrs)
|
64
|
{
|
65
|
super(context,attrs);
|
66
|
|
67
|
if(!isInEditMode())
|
68
|
{
|
69
|
mRotationVect = VECT[0];
|
70
|
|
71
|
mPoint = new float[3];
|
72
|
mCamera= new float[3];
|
73
|
mDiff = new float[3];
|
74
|
mTouchPoint = new float[3];
|
75
|
mTouchPointCastOntoFace = new float[3];
|
76
|
|
77
|
mScreenWidth = mScreenHeight = mScreenMin = 0;
|
78
|
|
79
|
mRenderer = new RubikRenderer(this);
|
80
|
mCube = mRenderer.getCube();
|
81
|
|
82
|
mQuatCurrent = new Static4D(0,0,0,1);
|
83
|
mQuatAccumulated = mRenderer.initializeQuat();
|
84
|
|
85
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
86
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
87
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
88
|
setRenderer(mRenderer);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
public RubikRenderer getRenderer()
|
95
|
{
|
96
|
return mRenderer;
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
@Override
|
102
|
public boolean onTouchEvent(MotionEvent event)
|
103
|
{
|
104
|
int action = event.getAction();
|
105
|
int x = (int)event.getX();
|
106
|
int y = (int)event.getY();
|
107
|
|
108
|
switch(action)
|
109
|
{
|
110
|
case MotionEvent.ACTION_DOWN: mX = x;
|
111
|
mY = y;
|
112
|
mLastTouchedFace = faceTouched(x,y);
|
113
|
|
114
|
if( mLastTouchedFace != NONE )
|
115
|
{
|
116
|
mDragging = false;
|
117
|
mBeginningRotation = mRenderer.canRotate();
|
118
|
mContinuingRotation = false;
|
119
|
}
|
120
|
else
|
121
|
{
|
122
|
mDragging = true;
|
123
|
mBeginningRotation = false;
|
124
|
mContinuingRotation = false;
|
125
|
}
|
126
|
break;
|
127
|
case MotionEvent.ACTION_MOVE: if( mDragging )
|
128
|
{
|
129
|
mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
|
130
|
mRenderer.setQuatCurrent(mQuatCurrent);
|
131
|
}
|
132
|
if( mBeginningRotation )
|
133
|
{
|
134
|
int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100;
|
135
|
|
136
|
if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
|
137
|
{
|
138
|
addNewRotation(x,y);
|
139
|
mBeginningRotation = false;
|
140
|
mContinuingRotation= true;
|
141
|
}
|
142
|
}
|
143
|
else if( mContinuingRotation )
|
144
|
{
|
145
|
continueRotation(x,y);
|
146
|
}
|
147
|
break;
|
148
|
case MotionEvent.ACTION_UP : if( mDragging )
|
149
|
{
|
150
|
mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
151
|
mQuatCurrent.set(0f, 0f, 0f, 1f);
|
152
|
mRenderer.setQuatCurrent(mQuatCurrent);
|
153
|
mRenderer.setQuatAccumulated(mQuatAccumulated);
|
154
|
}
|
155
|
|
156
|
if( mContinuingRotation )
|
157
|
{
|
158
|
finishRotation();
|
159
|
}
|
160
|
|
161
|
break;
|
162
|
}
|
163
|
|
164
|
return true;
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
void setNewCubeSize(int newCubeSize)
|
170
|
{
|
171
|
android.util.Log.e("view", "new size="+newCubeSize);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
void scrambleCube()
|
177
|
{
|
178
|
android.util.Log.e("view", "scrambling...");
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
void setScreenSize(int width, int height)
|
184
|
{
|
185
|
mScreenWidth = width;
|
186
|
mScreenHeight= height;
|
187
|
|
188
|
mScreenMin = width<height ? width:height;
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
void setCameraDist(float distance)
|
194
|
{
|
195
|
mCameraDistance = distance;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
private int faceTouched(int xTouch, int yTouch)
|
201
|
{
|
202
|
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
|
203
|
|
204
|
convertTouchPointToScreenSpace(xTouch,yTouch);
|
205
|
convertCameraPointToScreenSpace();
|
206
|
|
207
|
for(int face=FRONT; face<=BOTTOM; face++)
|
208
|
{
|
209
|
if( faceIsVisible(face,cubeHalfSize) )
|
210
|
{
|
211
|
castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
|
212
|
|
213
|
float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
|
214
|
float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
|
215
|
float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
|
216
|
|
217
|
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
|
218
|
}
|
219
|
}
|
220
|
|
221
|
return NONE;
|
222
|
}
|
223
|
|
224
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225
|
|
226
|
private void addNewRotation(int x, int y)
|
227
|
{
|
228
|
float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
|
229
|
|
230
|
convertTouchPointToScreenSpace(x,y);
|
231
|
castTouchPointOntoFace(mLastTouchedFace,cubeHalfSize,mDiff);
|
232
|
|
233
|
mDiff[0] -= mTouchPointCastOntoFace[0];
|
234
|
mDiff[1] -= mTouchPointCastOntoFace[1];
|
235
|
mDiff[2] -= mTouchPointCastOntoFace[2];
|
236
|
|
237
|
int xAxis = retFaceXaxis(mLastTouchedFace);
|
238
|
int yAxis = retFaceYaxis(mLastTouchedFace);
|
239
|
mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
|
240
|
float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
|
241
|
|
242
|
mTouchPoint[0] = mPoint[0];
|
243
|
mTouchPoint[1] = mPoint[1];
|
244
|
mTouchPoint[2] = mPoint[2];
|
245
|
|
246
|
mCube.addNewRotation(mRotationVect,offset);
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
private boolean isVertical(float x, float y)
|
252
|
{
|
253
|
return (y>x) ? (y>=-x) : (y< -x);
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
|
258
|
// given face by 240/2 = 120 degrees.
|
259
|
|
260
|
private void continueRotation(int x, int y)
|
261
|
{
|
262
|
convertTouchPointToScreenSpace(x,y);
|
263
|
|
264
|
mDiff[0] = mPoint[0]-mTouchPoint[0];
|
265
|
mDiff[1] = mPoint[1]-mTouchPoint[1];
|
266
|
mDiff[2] = mPoint[2]-mTouchPoint[2];
|
267
|
|
268
|
int xAxis= retFaceXaxis(mLastTouchedFace);
|
269
|
int yAxis= retFaceYaxis(mLastTouchedFace);
|
270
|
int sign = retFaceRotationSign(mLastTouchedFace);
|
271
|
float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
|
272
|
|
273
|
mCube.continueRotation(240.0f*sign*angle/mScreenMin);
|
274
|
}
|
275
|
|
276
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
277
|
|
278
|
private void finishRotation()
|
279
|
{
|
280
|
mRenderer.finishRotation();
|
281
|
}
|
282
|
|
283
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
284
|
// return quat1*quat2
|
285
|
|
286
|
static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
287
|
{
|
288
|
float qx = quat1.get1();
|
289
|
float qy = quat1.get2();
|
290
|
float qz = quat1.get3();
|
291
|
float qw = quat1.get4();
|
292
|
|
293
|
float rx = quat2.get1();
|
294
|
float ry = quat2.get2();
|
295
|
float rz = quat2.get3();
|
296
|
float rw = quat2.get4();
|
297
|
|
298
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
299
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
300
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
301
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
302
|
|
303
|
return new Static4D(tx,ty,tz,tw);
|
304
|
}
|
305
|
|
306
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
307
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
308
|
|
309
|
static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
310
|
{
|
311
|
float qx = quat.get1();
|
312
|
float qy = quat.get2();
|
313
|
float qz = quat.get3();
|
314
|
float qw = quat.get4();
|
315
|
|
316
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
317
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
318
|
|
319
|
return quatMultiply(tmp,quat);
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
324
|
|
325
|
static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
326
|
{
|
327
|
float qx = quat.get1();
|
328
|
float qy = quat.get2();
|
329
|
float qz = quat.get3();
|
330
|
float qw = quat.get4();
|
331
|
|
332
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
333
|
Static4D tmp = quatMultiply(quat,vector);
|
334
|
|
335
|
return quatMultiply(tmp,quatInverted);
|
336
|
}
|
337
|
|
338
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
339
|
|
340
|
private Static4D quatFromDrag(float dragX, float dragY)
|
341
|
{
|
342
|
float axisX = dragY; // inverted X and Y - rotation axis is
|
343
|
float axisY = dragX; // perpendicular to (dragX,dragY) Why not (-dragY, dragX) ? because Y axis is also inverted!
|
344
|
float axisZ = 0;
|
345
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
346
|
|
347
|
if( axisL>0 )
|
348
|
{
|
349
|
axisX /= axisL;
|
350
|
axisY /= axisL;
|
351
|
axisZ /= axisL;
|
352
|
|
353
|
float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
|
354
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
355
|
|
356
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
357
|
}
|
358
|
|
359
|
return new Static4D(0f, 0f, 0f, 1f);
|
360
|
}
|
361
|
|
362
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
363
|
|
364
|
private boolean faceIsVisible(int face, float cubeHalfSize)
|
365
|
{
|
366
|
int sign = retFaceSign(face);
|
367
|
int zAxis= retFaceZaxis(face);
|
368
|
|
369
|
return sign*mCamera[zAxis] > cubeHalfSize;
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
|
374
|
private void convertTouchPointToScreenSpace(int x, int y)
|
375
|
{
|
376
|
float halfScrWidth = mScreenWidth *0.5f;
|
377
|
float halfScrHeight = mScreenHeight*0.5f;
|
378
|
Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
|
379
|
Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
|
380
|
|
381
|
mPoint[0] = rotatedTouchPoint.get1();
|
382
|
mPoint[1] = rotatedTouchPoint.get2();
|
383
|
mPoint[2] = rotatedTouchPoint.get3();
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
|
388
|
private void convertCameraPointToScreenSpace()
|
389
|
{
|
390
|
Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
|
391
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
|
392
|
|
393
|
mCamera[0] = rotatedCamera.get1();
|
394
|
mCamera[1] = rotatedCamera.get2();
|
395
|
mCamera[2] = rotatedCamera.get3();
|
396
|
}
|
397
|
|
398
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
399
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
400
|
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
|
401
|
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
|
402
|
|
403
|
private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
|
404
|
{
|
405
|
int sign = retFaceSign(face);
|
406
|
int zAxis= retFaceZaxis(face);
|
407
|
float diff = mPoint[zAxis]-mCamera[zAxis];
|
408
|
|
409
|
float ratio = diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
|
410
|
|
411
|
output[0] = (mPoint[0]-mCamera[0])*ratio + mCamera[0];
|
412
|
output[1] = (mPoint[1]-mCamera[1])*ratio + mCamera[1];
|
413
|
output[2] = (mPoint[2]-mCamera[2])*ratio + mCamera[2];
|
414
|
}
|
415
|
|
416
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417
|
|
418
|
private int retFaceSign(int face)
|
419
|
{
|
420
|
return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
|
421
|
}
|
422
|
|
423
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
424
|
|
425
|
private int retFaceRotationSign(int face)
|
426
|
{
|
427
|
return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
|
428
|
}
|
429
|
|
430
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
431
|
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
|
432
|
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
|
433
|
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
|
434
|
|
435
|
private int retFaceXaxis(int face)
|
436
|
{
|
437
|
switch(face)
|
438
|
{
|
439
|
case FRONT :
|
440
|
case BACK : return VECTX;
|
441
|
case LEFT :
|
442
|
case RIGHT : return VECTZ;
|
443
|
case TOP :
|
444
|
case BOTTOM: return VECTX;
|
445
|
}
|
446
|
|
447
|
return -1;
|
448
|
}
|
449
|
|
450
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
451
|
|
452
|
private int retFaceYaxis(int face)
|
453
|
{
|
454
|
switch(face)
|
455
|
{
|
456
|
case FRONT :
|
457
|
case BACK : return VECTY;
|
458
|
case LEFT :
|
459
|
case RIGHT : return VECTY;
|
460
|
case TOP :
|
461
|
case BOTTOM: return VECTZ;
|
462
|
}
|
463
|
|
464
|
return -1;
|
465
|
}
|
466
|
|
467
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
468
|
|
469
|
private int retFaceZaxis(int face)
|
470
|
{
|
471
|
switch(face)
|
472
|
{
|
473
|
case FRONT :
|
474
|
case BACK : return VECTZ;
|
475
|
case LEFT :
|
476
|
case RIGHT : return VECTX;
|
477
|
case TOP :
|
478
|
case BOTTOM: return VECTY;
|
479
|
}
|
480
|
|
481
|
return -1;
|
482
|
}
|
483
|
}
|
484
|
|