Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikCube.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.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectRotate;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectSink;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshCubes;
35
import org.distorted.library.message.EffectListener;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class RubikCube
44
{
45
    private static final int TEXTURE_SIZE = 100;
46

    
47
    private static final Static3D VectX = new Static3D(1,0,0);
48
    private static final Static3D VectY = new Static3D(0,1,0);
49
    private static final Static3D VectZ = new Static3D(0,0,1);
50

    
51
    private MeshCubes[][][] mCubes;
52
    private DistortedEffects[][][] mEffects;
53
    private Static4D[][][] mQuatScramble;
54
    private Static3D[][][] mRotationAxis;
55
    private Dynamic1D[][][] mRotationAngle;
56
    private Static3D[][][] mCurrentPosition;
57
    private Static1D mRotationAngleStatic, mRotationAngleNearest;
58
    private DistortedTexture mTexture;
59
    private DistortedEffects mEffectsListeningForNow;
60

    
61
    private int mRotAxis, mRotRow;
62
    private int mSize;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    RubikCube(int size, Static3D move, Static3D scale, Static4D quatC, Static4D quatA)
67
      {
68
      mSize = size;
69

    
70
      mRotationAngleStatic  = new Static1D(0);
71
      mRotationAngleNearest = new Static1D(0);
72

    
73
      mRotAxis= RubikSurfaceView.VECTX;
74
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
75

    
76
      mCubes          = new MeshCubes[mSize][mSize][mSize];
77
      mEffects        = new DistortedEffects[mSize][mSize][mSize];
78
      mQuatScramble   = new Static4D[mSize][mSize][mSize];
79
      mRotationAxis   = new Static3D[mSize][mSize][mSize];
80
      mRotationAngle  = new Dynamic1D[mSize][mSize][mSize];
81
      mCurrentPosition= new Static3D[mSize][mSize][mSize];
82

    
83
      Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
84

    
85
      Static3D center = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
86
      Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
87

    
88
      VertexEffectSink        sinkEffect = new VertexEffectSink( new Static1D(3.0f - 1.8f/mSize), center, region );
89
      MatrixEffectMove        moveEffect = new MatrixEffectMove(move);
90
      MatrixEffectScale      scaleEffect = new MatrixEffectScale(scale);
91
      MatrixEffectQuaternion quatCEffect = new MatrixEffectQuaternion(quatC, center);
92
      MatrixEffectQuaternion quatAEffect = new MatrixEffectQuaternion(quatA, center);
93

    
94
      // 3x2 bitmap = 6 squares:
95
      //
96
      // RED     GREEN   BLUE
97
      // YELLOW  WHITE   BROWN
98

    
99
      final float ze = 0.0f;
100
      final float ot = 1.0f/3.0f;
101
      final float tt = 2.0f/3.0f;
102
      final float oh = 1.0f/2.0f;
103
      final float of = 1.0f/40.0f;
104

    
105
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
106
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
107
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
108
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
109
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
110
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
111

    
112
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
113

    
114
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
115
      float nc = 0.5f*(mSize-1);
116
      int vertices = (int)(24.0f/mSize + 2.0f);
117

    
118
      for(int x = 0; x< mSize; x++)
119
        for(int y = 0; y< mSize; y++)
120
          for(int z = 0; z< mSize; z++)
121
            {
122
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
123
              {
124
              tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
125
              tmpRight = (x== mSize-1 ? mapRight :mapBlack);
126
              tmpFront = (z== mSize-1 ? mapFront :mapBlack);
127
              tmpBack  = (z==       0 ? mapBack  :mapBlack);
128
              tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
129
              tmpBottom= (y==       0 ? mapBottom:mapBlack);
130

    
131
              mCubes[x][y][z]           = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
132
              cubeVectors[x][y][z]      = new Static3D( TEXTURE_SIZE*(x-nc), TEXTURE_SIZE*(y-nc), TEXTURE_SIZE*(z-nc) );
133
              mQuatScramble[x][y][z]    = new Static4D(0,0,0,1);
134
              mRotationAngle[x][y][z]   = new Dynamic1D();
135
              mRotationAxis[x][y][z]    = new Static3D(1,0,0);
136
              mCurrentPosition[x][y][z] = new Static3D(x,y,z);
137

    
138
              mEffects[x][y][z] = new DistortedEffects();
139
              mEffects[x][y][z].apply(sinkEffect);
140
              mEffects[x][y][z].apply(moveEffect);
141
              mEffects[x][y][z].apply(scaleEffect);
142
              mEffects[x][y][z].apply(quatCEffect);
143
              mEffects[x][y][z].apply(quatAEffect);
144
              mEffects[x][y][z].apply( new MatrixEffectRotate( mRotationAngle[x][y][z], mRotationAxis[x][y][z], center));
145
              mEffects[x][y][z].apply( new MatrixEffectQuaternion(mQuatScramble[x][y][z], center));
146
              mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
147
              }
148
            }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    void attachToScreen(DistortedScreen screen)
154
      {
155
      for(int x=0; x<mSize; x++)
156
        for(int y=0; y<mSize; y++)
157
          for(int z=0; z<mSize; z++)
158
            {
159
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
160
              {
161
              screen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
162
              }
163
            }
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    void addNewRotation(int vector, float offset )
169
      {
170
      Static3D axis = VectX;
171

    
172
      switch(vector)
173
        {
174
        case RubikSurfaceView.VECTX: axis = VectX; break;
175
        case RubikSurfaceView.VECTY: axis = VectY; break;
176
        case RubikSurfaceView.VECTZ: axis = VectZ; break;
177
        }
178

    
179
      mRotAxis = vector;
180
      mRotRow  = (int)(mSize*offset);
181

    
182
      mRotationAngleStatic.set1(0.0f);
183

    
184
      for(int x=0; x<mSize; x++)
185
        for(int y=0; y<mSize; y++)
186
          for(int z=0; z<mSize; z++)
187
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
188
              {
189
              if( belongsToRotation(x,y,z,vector,mRotRow) )
190
                {
191
                mRotationAxis[x][y][z].set(axis);
192
                mRotationAngle[x][y][z].add(mRotationAngleStatic);
193
                }
194
              }
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    void continueRotation(float angleInDegrees)
200
      {
201
      mRotationAngleStatic.set1(angleInDegrees);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    private int computeNearestAngle(float angle)
207
      {
208
      final int NEAREST = 90;
209

    
210
      int tmp = (int)((angle+NEAREST/2)/NEAREST);
211
      if( angle< -(NEAREST/2) ) tmp-=1;
212

    
213
      return NEAREST*tmp;
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    void finishRotationCalledOnNextRender(EffectListener listener)
219
      {
220
      boolean first = true;
221
      int nearestAngleInDegrees = computeNearestAngle(mRotationAngleStatic.get1());
222

    
223

    
224
      android.util.Log.e("cube", "finish: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
225

    
226

    
227
      mRotationAngleNearest.set1(nearestAngleInDegrees);
228

    
229
      for(int x=0; x<mSize; x++)
230
        for(int y=0; y<mSize; y++)
231
          for(int z=0; z<mSize; z++)
232
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
233
              {
234
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
235
                {
236
                mRotationAngle[x][y][z].makeRunNowFor(2000);
237
                mRotationAngle[x][y][z].add(mRotationAngleNearest);
238

    
239
                if( first )
240
                  {
241
                  first = false;
242
                  mEffectsListeningForNow = mEffects[x][y][z];
243
                  mEffectsListeningForNow.registerForMessages(listener);
244
                  }
245
                }
246
              }
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    void removeRotationCalledOnNextRender(EffectListener listener)
252
      {
253
      mEffectsListeningForNow.deregisterForMessages(listener);
254

    
255
      int nearestAngleInDegrees = computeNearestAngle(mRotationAngleStatic.get1());
256
      double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
257
      float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
258
      float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
259

    
260
      android.util.Log.e("cube", "remove: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
261

    
262
      mRotationAngleStatic.set1(0);
263
      mRotationAngleNearest.set1(0);
264

    
265
      float qx=0,qy=0,qz=0;
266

    
267
      switch(mRotAxis)
268
        {
269
        case RubikSurfaceView.VECTX: qx=1; break;
270
        case RubikSurfaceView.VECTY: qy=1; break;
271
        case RubikSurfaceView.VECTZ: qz=1; break;
272
        }
273

    
274
      Static4D quat = new Static4D(qx*sinA, qy*sinA, qz*sinA, cosA);
275

    
276
      for(int x=0; x<mSize; x++)
277
        for(int y=0; y<mSize; y++)
278
          for(int z=0; z<mSize; z++)
279
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
280
              {
281
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
282
                {
283
                mRotationAngle[x][y][z].makeRunNowFor(0);
284
                mRotationAngle[x][y][z].removeAll();
285
                mQuatScramble[x][y][z].set(RubikSurfaceView.quatMultiply(quat,mQuatScramble[x][y][z]));
286
                modifyCurrentPosition(x,y,z,quat);
287
                }
288
              }
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    private boolean belongsToRotation(int x, int y, int z, int vector, int row)
294
      {
295
      switch(vector)
296
        {
297
        case RubikSurfaceView.VECTX: return mCurrentPosition[x][y][z].get1()==row;
298
        case RubikSurfaceView.VECTY: return mCurrentPosition[x][y][z].get2()==row;
299
        case RubikSurfaceView.VECTZ: return mCurrentPosition[x][y][z].get3()==row;
300
        }
301

    
302
      return false;
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    private void modifyCurrentPosition(int x, int y, int z, Static4D quat)
308
      {
309
      Static3D current = mCurrentPosition[x][y][z];
310
      float diff = 0.5f*(mSize-1);
311
      float cubitCenterX = current.get1() - diff;
312
      float cubitCenterY = current.get2() - diff;
313
      float cubitCenterZ = current.get3() - diff;
314

    
315
      Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
316
      Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
317

    
318
      float rotatedX = rotatedCenter.get1() + diff;
319
      float rotatedY = rotatedCenter.get2() + diff;
320
      float rotatedZ = rotatedCenter.get3() + diff;
321

    
322
      int roundedX = (int)(rotatedX+0.1f);
323
      int roundedY = (int)(rotatedY+0.1f);
324
      int roundedZ = (int)(rotatedZ+0.1f);
325

    
326
      mCurrentPosition[x][y][z].set1(roundedX);
327
      mCurrentPosition[x][y][z].set2(roundedY);
328
      mCurrentPosition[x][y][z].set3(roundedZ);
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    void createTexture()
334
      {
335
      Bitmap bitmap;
336

    
337
      final int S = 128;
338
      final int W = 3*S;
339
      final int H = 2*S;
340
      final int R = S/10;
341
      final int M = S/20;
342

    
343
      Paint paint = new Paint();
344
      bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
345
      Canvas canvas = new Canvas(bitmap);
346

    
347
      paint.setAntiAlias(true);
348
      paint.setTextAlign(Paint.Align.CENTER);
349
      paint.setStyle(Paint.Style.FILL);
350

    
351
      // 3x2 bitmap = 6 squares:
352
      //
353
      // RED     GREEN   BLUE
354
      // YELLOW  WHITE   BROWN
355

    
356
      paint.setColor(0xff000000);                                  // BLACK BACKGROUND
357
      canvas.drawRect(0, 0, W, H, paint);                          //
358

    
359
      paint.setColor(0xffff0000);                                  // RED
360
      canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
361
      paint.setColor(0xff00ff00);                                  // GREEN
362
      canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
363
      paint.setColor(0xff0000ff);                                  // BLUE
364
      canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
365
      paint.setColor(0xffffff00);                                  // YELLOW
366
      canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
367
      paint.setColor(0xffffffff);                                  // WHITE
368
      canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
369
      paint.setColor(0xffb5651d);                                  // BROWN
370
      canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
371

    
372
      mTexture.setTexture(bitmap);
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    float getTextureSize()
378
      {
379
      return TEXTURE_SIZE;
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    float getSize()
385
      {
386
      return mSize;
387
      }
388
}
(2-2/4)