Project

General

Profile

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

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

1 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 8647eae2 Leszek Koltunski
// Copyright 2019 Leszek Koltunski                                                               //
3 5b4a2d76 Leszek Koltunski
//                                                                                               //
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 e4c0057e Leszek Koltunski
import org.distorted.library.type.Static4D;
29
30 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32
class RubikSurfaceView extends GLSurfaceView
33
{
34 840d83be Leszek Koltunski
    private final static int NONE   =-1;
35 c6223217 Leszek Koltunski
    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 e4c0057e Leszek Koltunski
42 dc8979ba Leszek Koltunski
    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 840d83be Leszek Koltunski
48 7f986357 Leszek Koltunski
    private boolean mDragging, mBeginRot;
49 5b4a2d76 Leszek Koltunski
    private int mX, mY;
50 94cc96ff Leszek Koltunski
    private Static4D mQuatCurrent, mQuatAccumulated;
51 c6223217 Leszek Koltunski
    private int mRotationVect;
52 5b4a2d76 Leszek Koltunski
    private RubikRenderer mRenderer;
53 8647eae2 Leszek Koltunski
    private RubikCube mCube;
54 5b4a2d76 Leszek Koltunski
55 8fbe9426 Leszek Koltunski
    private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
56 840d83be Leszek Koltunski
    private int mLastTouchedFace;
57 18709ae0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
58
    private float mCameraDistance;
59 840d83be Leszek Koltunski
60 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
    public RubikSurfaceView(Context context)
63
      {
64
      super(context);
65
66 e4c0057e Leszek Koltunski
      mDragging = false;
67 7f986357 Leszek Koltunski
      mBeginRot = false;
68 dc8979ba Leszek Koltunski
      mRotationVect = VECT[0];
69
70 8fbe9426 Leszek Koltunski
      mPoint = new float[3];
71
      mCamera= new float[3];
72 dc8979ba Leszek Koltunski
      mDiff  = new float[3];
73 8fbe9426 Leszek Koltunski
      mTouchPoint = new float[3];
74
      mTouchPointCastOntoFace = new float[3];
75 c6223217 Leszek Koltunski
76 18709ae0 Leszek Koltunski
      mScreenWidth = mScreenHeight = mScreenMin = 0;
77
78 5b4a2d76 Leszek Koltunski
      mRenderer = new RubikRenderer(this);
79 8647eae2 Leszek Koltunski
      mCube = mRenderer.getCube();
80 94cc96ff Leszek Koltunski
81
      mQuatCurrent     = new Static4D(0,0,0,1);
82
      mQuatAccumulated = mRenderer.initializeQuat();
83
84 5b4a2d76 Leszek Koltunski
      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 e4c0057e Leszek Koltunski
    @Override
100
    public boolean onTouchEvent(MotionEvent event)
101 5b4a2d76 Leszek Koltunski
      {
102
      int action = event.getAction();
103
      int x = (int)event.getX();
104
      int y = (int)event.getY();
105
106
      switch(action)
107
         {
108 840d83be Leszek Koltunski
         case MotionEvent.ACTION_DOWN: mX = x;
109
                                       mY = y;
110
                                       mLastTouchedFace = faceTouched(x,y);
111
112 7f986357 Leszek Koltunski
                                       if( mLastTouchedFace != NONE ) mBeginRot = true;
113 d0418bda Leszek Koltunski
                                       else                           mDragging = true;
114
115 5b4a2d76 Leszek Koltunski
                                       break;
116 94cc96ff Leszek Koltunski
         case MotionEvent.ACTION_MOVE: if( mDragging )
117
                                         {
118
                                         mQuatCurrent.set(quatFromDrag(mX-x,mY-y));
119
                                         mRenderer.setQuatCurrent(mQuatCurrent);
120
                                         }
121 7f986357 Leszek Koltunski
                                       else if( mBeginRot )
122 840d83be Leszek Koltunski
                                         {
123 f843630b Leszek Koltunski
                                         int minimumToRotate = (mScreenMin*mScreenMin)/100;
124 840d83be Leszek Koltunski
125
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
126
                                           {
127 c6223217 Leszek Koltunski
                                           addNewRotation(x,y);
128 7f986357 Leszek Koltunski
                                           mBeginRot = false;
129 840d83be Leszek Koltunski
                                           }
130
                                         }
131 c6223217 Leszek Koltunski
                                       else
132
                                         {
133
                                         continueRotation(x,y);
134
                                         }
135 e4c0057e Leszek Koltunski
                                       break;
136 c6223217 Leszek Koltunski
         case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
137
138
                                       mDragging = false;
139 7f986357 Leszek Koltunski
                                       mBeginRot = false;
140 840d83be Leszek Koltunski
141 94cc96ff Leszek Koltunski
                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
142
                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
143
                                       mRenderer.setQuatCurrent(mQuatCurrent);
144
                                       mRenderer.setQuatAccumulated(mQuatAccumulated);
145 5b4a2d76 Leszek Koltunski
                                       break;
146
         }
147
148
      return true;
149
      }
150
151 7f986357 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 483ae94e Leszek Koltunski
    void setCameraDist(float distance)
164 7f986357 Leszek Koltunski
      {
165 483ae94e Leszek Koltunski
      mCameraDistance = distance;
166 7f986357 Leszek Koltunski
      }
167
168 840d83be Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 dc8979ba Leszek Koltunski
    private int faceTouched(int xTouch, int yTouch)
171 840d83be Leszek Koltunski
      {
172 dc8979ba Leszek Koltunski
      float cubeHalfSize= mRenderer.returnCubeSizeInScreenSpace()*0.5f;
173 840d83be Leszek Koltunski
174 dc8979ba Leszek Koltunski
      convertTouchPointToScreenSpace(xTouch,yTouch);
175
      convertCameraPointToScreenSpace();
176 840d83be Leszek Koltunski
177 dc8979ba Leszek Koltunski
      for(int face=FRONT; face<=BOTTOM; face++)
178 840d83be Leszek Koltunski
        {
179 dc8979ba Leszek Koltunski
        if( faceIsVisible(face,cubeHalfSize) )
180
          {
181 8fbe9426 Leszek Koltunski
          castTouchPointOntoFace(face,cubeHalfSize, mTouchPointCastOntoFace);
182 840d83be Leszek Koltunski
183 8fbe9426 Leszek Koltunski
          float qX= (mTouchPointCastOntoFace[0]+cubeHalfSize) / (2*cubeHalfSize);
184
          float qY= (mTouchPointCastOntoFace[1]+cubeHalfSize) / (2*cubeHalfSize);
185
          float qZ= (mTouchPointCastOntoFace[2]+cubeHalfSize) / (2*cubeHalfSize);
186 f843630b Leszek Koltunski
187 dc8979ba Leszek Koltunski
          if( qX<=1 && qX>=0 && qY<=1 && qY>=0 && qZ<=1 && qZ>=0 ) return face;
188
          }
189 f843630b Leszek Koltunski
        }
190 c6223217 Leszek Koltunski
191 dc8979ba Leszek Koltunski
      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 8fbe9426 Leszek Koltunski
      mDiff[0] -= mTouchPointCastOntoFace[0];
204
      mDiff[1] -= mTouchPointCastOntoFace[1];
205
      mDiff[2] -= mTouchPointCastOntoFace[2];
206 dc8979ba Leszek Koltunski
207
      int xAxis = retFaceXaxis(mLastTouchedFace);
208
      int yAxis = retFaceYaxis(mLastTouchedFace);
209
      mRotationVect = (isVertical( mDiff[xAxis], mDiff[yAxis]) ? VECT[xAxis]:VECT[yAxis]);
210 8fbe9426 Leszek Koltunski
      float offset= (mTouchPointCastOntoFace[mRotationVect]+cubeHalfSize)/(2*cubeHalfSize);
211 dc8979ba Leszek Koltunski
212 8fbe9426 Leszek Koltunski
      mTouchPoint[0] = mPoint[0];
213
      mTouchPoint[1] = mPoint[1];
214
      mTouchPoint[2] = mPoint[2];
215 7f986357 Leszek Koltunski
216
      mCube.addNewRotation(mRotationVect,offset);
217 c6223217 Leszek Koltunski
      }
218
219 f843630b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
    private boolean isVertical(float x, float y)
222
      {
223
      return (y>x) ? (y>=-x) : (y< -x);
224
      }
225
226 c6223217 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227 483ae94e Leszek Koltunski
// 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 c6223217 Leszek Koltunski
230
    private void continueRotation(int x, int y)
231
      {
232 dc8979ba Leszek Koltunski
      convertTouchPointToScreenSpace(x,y);
233 c6223217 Leszek Koltunski
234 8fbe9426 Leszek Koltunski
      mDiff[0] = mPoint[0]-mTouchPoint[0];
235
      mDiff[1] = mPoint[1]-mTouchPoint[1];
236
      mDiff[2] = mPoint[2]-mTouchPoint[2];
237 c6223217 Leszek Koltunski
238 dc8979ba Leszek Koltunski
      int xAxis= retFaceXaxis(mLastTouchedFace);
239
      int yAxis= retFaceYaxis(mLastTouchedFace);
240
      int sign = retFaceRotationSign(mLastTouchedFace);
241
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
242 c6223217 Leszek Koltunski
243 7f62afde Leszek Koltunski
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
244 c6223217 Leszek Koltunski
      }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
    private void finishRotation()
249
      {
250
      mRenderer.finishRotation();
251
      }
252
253 e4c0057e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
254
// return quat1*quat2
255
256 8be29dfc Leszek Koltunski
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
257 e4c0057e Leszek Koltunski
      {
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 d0418bda Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e4c0057e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
    private Static4D quatFromDrag(float dragX, float dragY)
311
      {
312
      float axisX = dragY;  // inverted X and Y - rotation axis is
313 d0418bda Leszek Koltunski
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
314 e4c0057e Leszek Koltunski
      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 18709ae0 Leszek Koltunski
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
324 e4c0057e Leszek Koltunski
        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 dc8979ba Leszek Koltunski
    private boolean faceIsVisible(int face, float cubeHalfSize)
335 e4c0057e Leszek Koltunski
      {
336 dc8979ba Leszek Koltunski
      int sign = retFaceSign(face);
337
      int zAxis= retFaceZaxis(face);
338 e4c0057e Leszek Koltunski
339 8fbe9426 Leszek Koltunski
      return sign*mCamera[zAxis] > cubeHalfSize;
340 dc8979ba Leszek Koltunski
      }
341 e4c0057e Leszek Koltunski
342 dc8979ba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
343 e4c0057e Leszek Koltunski
344 dc8979ba Leszek Koltunski
    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 8fbe9426 Leszek Koltunski
      mPoint[0] = rotatedTouchPoint.get1();
352
      mPoint[1] = rotatedTouchPoint.get2();
353
      mPoint[2] = rotatedTouchPoint.get3();
354 e4c0057e Leszek Koltunski
      }
355
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
358 dc8979ba Leszek Koltunski
    private void convertCameraPointToScreenSpace()
359 e4c0057e Leszek Koltunski
      {
360 dc8979ba Leszek Koltunski
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
361
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
362 e4c0057e Leszek Koltunski
363 8fbe9426 Leszek Koltunski
      mCamera[0] = rotatedCamera.get1();
364
      mCamera[1] = rotatedCamera.get2();
365
      mCamera[2] = rotatedCamera.get3();
366 dc8979ba Leszek Koltunski
      }
367 e4c0057e Leszek Koltunski
368 dc8979ba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e4c0057e Leszek Koltunski
373 dc8979ba Leszek Koltunski
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
374
      {
375
      int sign = retFaceSign(face);
376
      int zAxis= retFaceZaxis(face);
377 8fbe9426 Leszek Koltunski
      float diff = mPoint[zAxis]-mCamera[zAxis];
378 e4c0057e Leszek Koltunski
379 8fbe9426 Leszek Koltunski
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
380 e4c0057e Leszek Koltunski
381 8fbe9426 Leszek Koltunski
      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 e4c0057e Leszek Koltunski
      }
385 840d83be Leszek Koltunski
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387
388 dc8979ba Leszek Koltunski
    private int retFaceSign(int face)
389 840d83be Leszek Koltunski
      {
390 dc8979ba Leszek Koltunski
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
391
      }
392
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394 840d83be Leszek Koltunski
395 dc8979ba Leszek Koltunski
    private int retFaceRotationSign(int face)
396
      {
397
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
398 c6223217 Leszek Koltunski
      }
399 840d83be Leszek Koltunski
400 c6223217 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
401
402 dc8979ba Leszek Koltunski
    private int retFaceXaxis(int face)
403 c6223217 Leszek Koltunski
      {
404 dc8979ba Leszek Koltunski
      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 840d83be Leszek Koltunski
414 dc8979ba Leszek Koltunski
      return -1;
415 c6223217 Leszek Koltunski
      }
416 840d83be Leszek Koltunski
417 c6223217 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
418
419 dc8979ba Leszek Koltunski
    private int retFaceYaxis(int face)
420 c6223217 Leszek Koltunski
      {
421
      switch(face)
422
        {
423 dc8979ba Leszek Koltunski
        case FRONT :
424
        case BACK  : return VECTY;
425
        case LEFT  :
426
        case RIGHT : return VECTY;
427
        case TOP   :
428
        case BOTTOM: return VECTZ;
429 c6223217 Leszek Koltunski
        }
430
431 dc8979ba Leszek Koltunski
      return -1;
432 c6223217 Leszek Koltunski
      }
433
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
436 dc8979ba Leszek Koltunski
    private int retFaceZaxis(int face)
437 c6223217 Leszek Koltunski
      {
438
      switch(face)
439
        {
440
        case FRONT :
441 dc8979ba Leszek Koltunski
        case BACK  : return VECTZ;
442 c6223217 Leszek Koltunski
        case LEFT  :
443 dc8979ba Leszek Koltunski
        case RIGHT : return VECTX;
444 c6223217 Leszek Koltunski
        case TOP   :
445 dc8979ba Leszek Koltunski
        case BOTTOM: return VECTY;
446 c6223217 Leszek Koltunski
        }
447
448 dc8979ba Leszek Koltunski
      return -1;
449 840d83be Leszek Koltunski
      }
450 5b4a2d76 Leszek Koltunski
}