Project

General

Profile

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

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

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
    void recomputeCameraDistance(double fovInRadians)
164
      {
165
      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
166
      }
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
228
    private void continueRotation(int x, int y)
229
      {
230 dc8979ba Leszek Koltunski
      convertTouchPointToScreenSpace(x,y);
231 c6223217 Leszek Koltunski
232 8fbe9426 Leszek Koltunski
      mDiff[0] = mPoint[0]-mTouchPoint[0];
233
      mDiff[1] = mPoint[1]-mTouchPoint[1];
234
      mDiff[2] = mPoint[2]-mTouchPoint[2];
235 c6223217 Leszek Koltunski
236 dc8979ba Leszek Koltunski
      int xAxis= retFaceXaxis(mLastTouchedFace);
237
      int yAxis= retFaceYaxis(mLastTouchedFace);
238
      int sign = retFaceRotationSign(mLastTouchedFace);
239
      float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
240 c6223217 Leszek Koltunski
241 7f62afde Leszek Koltunski
      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
242 c6223217 Leszek Koltunski
      }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    private void finishRotation()
247
      {
248
      mRenderer.finishRotation();
249
      }
250
251 e4c0057e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252
// return quat1*quat2
253
254 8be29dfc Leszek Koltunski
    static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
255 e4c0057e Leszek Koltunski
      {
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 d0418bda Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e4c0057e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
307
308
    private Static4D quatFromDrag(float dragX, float dragY)
309
      {
310
      float axisX = dragY;  // inverted X and Y - rotation axis is
311 d0418bda Leszek Koltunski
      float axisY = dragX;  // perpendicular to (dragX,dragY)   Why not (-dragY, dragX) ? because Y axis is also inverted!
312 e4c0057e Leszek Koltunski
      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 18709ae0 Leszek Koltunski
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
322 e4c0057e Leszek Koltunski
        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 dc8979ba Leszek Koltunski
    private boolean faceIsVisible(int face, float cubeHalfSize)
333 e4c0057e Leszek Koltunski
      {
334 dc8979ba Leszek Koltunski
      int sign = retFaceSign(face);
335
      int zAxis= retFaceZaxis(face);
336 e4c0057e Leszek Koltunski
337 8fbe9426 Leszek Koltunski
      return sign*mCamera[zAxis] > cubeHalfSize;
338 dc8979ba Leszek Koltunski
      }
339 e4c0057e Leszek Koltunski
340 dc8979ba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
341 e4c0057e Leszek Koltunski
342 dc8979ba Leszek Koltunski
    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 8fbe9426 Leszek Koltunski
      mPoint[0] = rotatedTouchPoint.get1();
350
      mPoint[1] = rotatedTouchPoint.get2();
351
      mPoint[2] = rotatedTouchPoint.get3();
352 e4c0057e Leszek Koltunski
      }
353
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356 dc8979ba Leszek Koltunski
    private void convertCameraPointToScreenSpace()
357 e4c0057e Leszek Koltunski
      {
358 dc8979ba Leszek Koltunski
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
359
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
360 e4c0057e Leszek Koltunski
361 8fbe9426 Leszek Koltunski
      mCamera[0] = rotatedCamera.get1();
362
      mCamera[1] = rotatedCamera.get2();
363
      mCamera[2] = rotatedCamera.get3();
364 dc8979ba Leszek Koltunski
      }
365 e4c0057e Leszek Koltunski
366 dc8979ba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e4c0057e Leszek Koltunski
371 dc8979ba Leszek Koltunski
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
372
      {
373
      int sign = retFaceSign(face);
374
      int zAxis= retFaceZaxis(face);
375 8fbe9426 Leszek Koltunski
      float diff = mPoint[zAxis]-mCamera[zAxis];
376 e4c0057e Leszek Koltunski
377 8fbe9426 Leszek Koltunski
      float ratio =  diff!=0.0f ? (sign*cubeHalfSize-mCamera[zAxis])/diff : 0.0f;
378 e4c0057e Leszek Koltunski
379 8fbe9426 Leszek Koltunski
      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 e4c0057e Leszek Koltunski
      }
383 840d83be Leszek Koltunski
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
386 dc8979ba Leszek Koltunski
    private int retFaceSign(int face)
387 840d83be Leszek Koltunski
      {
388 dc8979ba Leszek Koltunski
      return (face==FRONT || face==RIGHT || face==TOP) ? 1:-1;
389
      }
390
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392 840d83be Leszek Koltunski
393 dc8979ba Leszek Koltunski
    private int retFaceRotationSign(int face)
394
      {
395
      return (face==BACK || face==RIGHT || face==TOP) ? 1:-1;
396 c6223217 Leszek Koltunski
      }
397 840d83be Leszek Koltunski
398 c6223217 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
399
400 dc8979ba Leszek Koltunski
    private int retFaceXaxis(int face)
401 c6223217 Leszek Koltunski
      {
402 dc8979ba Leszek Koltunski
      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 840d83be Leszek Koltunski
412 dc8979ba Leszek Koltunski
      return -1;
413 c6223217 Leszek Koltunski
      }
414 840d83be Leszek Koltunski
415 c6223217 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
416
417 dc8979ba Leszek Koltunski
    private int retFaceYaxis(int face)
418 c6223217 Leszek Koltunski
      {
419
      switch(face)
420
        {
421 dc8979ba Leszek Koltunski
        case FRONT :
422
        case BACK  : return VECTY;
423
        case LEFT  :
424
        case RIGHT : return VECTY;
425
        case TOP   :
426
        case BOTTOM: return VECTZ;
427 c6223217 Leszek Koltunski
        }
428
429 dc8979ba Leszek Koltunski
      return -1;
430 c6223217 Leszek Koltunski
      }
431
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
434 dc8979ba Leszek Koltunski
    private int retFaceZaxis(int face)
435 c6223217 Leszek Koltunski
      {
436
      switch(face)
437
        {
438
        case FRONT :
439 dc8979ba Leszek Koltunski
        case BACK  : return VECTZ;
440 c6223217 Leszek Koltunski
        case LEFT  :
441 dc8979ba Leszek Koltunski
        case RIGHT : return VECTX;
442 c6223217 Leszek Koltunski
        case TOP   :
443 dc8979ba Leszek Koltunski
        case BOTTOM: return VECTY;
444 c6223217 Leszek Koltunski
        }
445
446 dc8979ba Leszek Koltunski
      return -1;
447 840d83be Leszek Koltunski
      }
448 5b4a2d76 Leszek Koltunski
}