Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikCube.java @ 698ad0a8

1 15bcc6f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 15bcc6f7 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 15bcc6f7 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 15bcc6f7 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 15bcc6f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.rubik;
21
22 6e18bd32 Leszek Koltunski
import java.util.Random;
23
24 15bcc6f7 Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectRotate;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectSink;
33
import org.distorted.library.main.DistortedEffects;
34 e3a72781 Leszek Koltunski
import org.distorted.library.main.DistortedNode;
35 15bcc6f7 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshCubes;
38 b62eb334 Leszek Koltunski
import org.distorted.library.message.EffectListener;
39 15bcc6f7 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
class RubikCube
47
{
48 da231553 Leszek Koltunski
    private static final int VECTX = 0;
49
    private static final int VECTY = 1;
50
    private static final int VECTZ = 2;
51 6e18bd32 Leszek Koltunski
52
    private static final int ROTATION_MILLISEC = 1500;
53 687263cc Leszek Koltunski
    private static final int STRETCH_SIZE = 100;
54 15bcc6f7 Leszek Koltunski
55
    private static final Static3D VectX = new Static3D(1,0,0);
56
    private static final Static3D VectY = new Static3D(0,1,0);
57
    private static final Static3D VectZ = new Static3D(0,0,1);
58
59 da231553 Leszek Koltunski
    private static Random mRnd = new Random(0);
60
61 e3a72781 Leszek Koltunski
    private DistortedNode[][][] mNodes;
62 15bcc6f7 Leszek Koltunski
    private MeshCubes[][][] mCubes;
63
    private DistortedEffects[][][] mEffects;
64
    private Static3D[][][] mRotationAxis;
65
    private Dynamic1D[][][] mRotationAngle;
66 8d5a8e06 Leszek Koltunski
    private MatrixEffectRotate[][][] mRotate;
67 15bcc6f7 Leszek Koltunski
    private DistortedTexture mTexture;
68
    private int mSize;
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 6e18bd32 Leszek Koltunski
    RubikCube(int size, Static3D move, Static3D scale)
73 15bcc6f7 Leszek Koltunski
      {
74
      mSize = size;
75
76 687263cc Leszek Koltunski
      mTexture = new DistortedTexture();
77 15bcc6f7 Leszek Koltunski
78 e3a72781 Leszek Koltunski
      mNodes          = new DistortedNode[mSize][mSize][mSize];
79 15bcc6f7 Leszek Koltunski
      mCubes          = new MeshCubes[mSize][mSize][mSize];
80
      mEffects        = new DistortedEffects[mSize][mSize][mSize];
81
      mRotationAxis   = new Static3D[mSize][mSize][mSize];
82
      mRotationAngle  = new Dynamic1D[mSize][mSize][mSize];
83 8d5a8e06 Leszek Koltunski
      mRotate         = new MatrixEffectRotate[mSize][mSize][mSize];
84 15bcc6f7 Leszek Koltunski
85
      Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
86
87 687263cc Leszek Koltunski
      Static3D center = new Static3D(STRETCH_SIZE*0.5f, STRETCH_SIZE*0.5f, STRETCH_SIZE*0.5f);
88
      Static4D region = new Static4D(0,0,0, STRETCH_SIZE*0.72f);
89 15bcc6f7 Leszek Koltunski
90 8a40abf4 Leszek Koltunski
      VertexEffectSink        sinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), center, region );
91 15bcc6f7 Leszek Koltunski
      MatrixEffectMove        moveEffect = new MatrixEffectMove(move);
92
      MatrixEffectScale      scaleEffect = new MatrixEffectScale(scale);
93 6e18bd32 Leszek Koltunski
      MatrixEffectQuaternion  quatEffect = new MatrixEffectQuaternion( initializeQuat(), center);
94 15bcc6f7 Leszek Koltunski
95
      // 3x2 bitmap = 6 squares:
96
      //
97
      // RED     GREEN   BLUE
98
      // YELLOW  WHITE   BROWN
99
100
      final float ze = 0.0f;
101
      final float ot = 1.0f/3.0f;
102
      final float tt = 2.0f/3.0f;
103
      final float oh = 1.0f/2.0f;
104
      final float of = 1.0f/40.0f;
105
106
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
107
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
108
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
109
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
110
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
111
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
112
113
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
114
115
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
116
      float nc = 0.5f*(mSize-1);
117
      int vertices = (int)(24.0f/mSize + 2.0f);
118
119
      for(int x = 0; x< mSize; x++)
120
        for(int y = 0; y< mSize; y++)
121
          for(int z = 0; z< mSize; z++)
122
            {
123
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
124
              {
125
              tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
126
              tmpRight = (x== mSize-1 ? mapRight :mapBlack);
127
              tmpFront = (z== mSize-1 ? mapFront :mapBlack);
128
              tmpBack  = (z==       0 ? mapBack  :mapBlack);
129
              tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
130
              tmpBottom= (y==       0 ? mapBottom:mapBlack);
131
132
              mCubes[x][y][z]           = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
133 698ad0a8 Leszek Koltunski
              mCubes[x][y][z].setStretch(STRETCH_SIZE,STRETCH_SIZE,STRETCH_SIZE);
134
135 687263cc Leszek Koltunski
              cubeVectors[x][y][z]      = new Static3D( STRETCH_SIZE*(x-nc), STRETCH_SIZE*(y-nc), STRETCH_SIZE*(z-nc) );
136 15bcc6f7 Leszek Koltunski
              mRotationAngle[x][y][z]   = new Dynamic1D();
137
              mRotationAxis[x][y][z]    = new Static3D(1,0,0);
138 6e18bd32 Leszek Koltunski
139
              mRotationAngle[x][y][z].add(new Static1D(0.0f));
140
              mRotationAngle[x][y][z].add(new Static1D(0.0f));
141 8d5a8e06 Leszek Koltunski
              mRotate[x][y][z] = new MatrixEffectRotate( mRotationAngle[x][y][z], mRotationAxis[x][y][z], center);
142 15bcc6f7 Leszek Koltunski
143 698ad0a8 Leszek Koltunski
              mEffects[x][y][z] = new DistortedEffects();
144 15bcc6f7 Leszek Koltunski
              mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
145 dae661e9 Leszek Koltunski
              mEffects[x][y][z].apply( mRotate[x][y][z] );
146
              mEffects[x][y][z].apply(quatEffect);
147
              mEffects[x][y][z].apply(scaleEffect);
148
              mEffects[x][y][z].apply(moveEffect);
149
              mEffects[x][y][z].apply(sinkEffect);
150 15bcc6f7 Leszek Koltunski
              }
151
            }
152
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155 da231553 Leszek Koltunski
// Initial rotation of the cube. Something semi-random that looks good.
156 15bcc6f7 Leszek Koltunski
157 da231553 Leszek Koltunski
   private Static4D initializeQuat()
158
     {
159
     return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
160
     }
161 e3a72781 Leszek Koltunski
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 da231553 Leszek Koltunski
   void attachToScreen(DistortedScreen screen)
165
     {
166
     for(int x=0; x<mSize; x++)
167
       for(int y=0; y<mSize; y++)
168
         for(int z=0; z<mSize; z++)
169
           {
170
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
171
             {
172
             mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
173
             screen.attach(mNodes[x][y][z]);
174
             }
175
           }
176
     }
177 15bcc6f7 Leszek Koltunski
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179 da231553 Leszek Koltunski
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
180
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
181
// memory leak.
182 15bcc6f7 Leszek Koltunski
183 da231553 Leszek Koltunski
   void releaseResources()
184
     {
185
     mTexture.markForDeletion();
186
187
     for(int x=0; x<mSize; x++)
188
       for(int y=0; y<mSize; y++)
189
         for(int z=0; z<mSize; z++)
190
           {
191
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
192
             {
193
             mCubes[x][y][z].markForDeletion();
194
             mNodes[x][y][z].markForDeletion();
195
             }
196
           }
197
     }
198 b62eb334 Leszek Koltunski
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 da231553 Leszek Koltunski
   void addRotation(EffectListener listener)
202 6e18bd32 Leszek Koltunski
     {
203 da231553 Leszek Koltunski
     mRnd.setSeed(System.currentTimeMillis());
204
205
     int vector = mRnd.nextInt(3);
206
     int rotRow = mRnd.nextInt(mSize);
207
208
     boolean first = true;
209
     Static3D axis = VectX;
210
211
     switch(vector)
212
       {
213
       case VECTX: axis = VectX; break;
214
       case VECTY: axis = VectY; break;
215
       case VECTZ: axis = VectZ; break;
216
       }
217
218
     for(int x=0; x<mSize; x++)
219
       for(int y=0; y<mSize; y++)
220
         for(int z=0; z<mSize; z++)
221
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
222
             {
223
             if( belongsToRotation(x,y,z,vector,rotRow) )
224
               {
225
               mRotationAxis[x][y][z].set(axis);
226 2666a48c Leszek Koltunski
               mRotationAngle[x][y][z].setDuration(ROTATION_MILLISEC);
227
               mRotationAngle[x][y][z].resetToBeginning();
228 da231553 Leszek Koltunski
               mRotationAngle[x][y][z].setPoint(1,90.0f);
229
230
               if( first )
231
                 {
232
                 first = false;
233 8d5a8e06 Leszek Koltunski
                 mRotate[x][y][z].notifyWhenFinished(listener);
234 da231553 Leszek Koltunski
                 }
235
               }
236
             }
237 6e18bd32 Leszek Koltunski
     }
238 15bcc6f7 Leszek Koltunski
239 6e18bd32 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
240 15bcc6f7 Leszek Koltunski
241 da231553 Leszek Koltunski
   private boolean belongsToRotation(int x, int y, int z, int vector, int row)
242
     {
243
     switch(vector)
244
       {
245
       case VECTX: return x==row;
246
       case VECTY: return y==row;
247
       case VECTZ: return z==row;
248
       }
249
250
     return false;
251
     }
252 15bcc6f7 Leszek Koltunski
253 8a40abf4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255 da231553 Leszek Koltunski
   private float getSinkStrength()
256
     {
257
     switch(mSize)
258
       {
259
       case 1 : return 1.1f;
260
       case 2 : return 1.5f;
261
       case 3 : return 1.8f;
262
       case 4 : return 2.0f;
263
       default: return 3.0f - 4.0f/mSize;
264
       }
265
     }
266 8a40abf4 Leszek Koltunski
267 15bcc6f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269 da231553 Leszek Koltunski
   void createTexture()
270
     {
271
     Bitmap bitmap;
272
273
     final int S = 128;
274
     final int W = 3*S;
275
     final int H = 2*S;
276
     final int R = S/10;
277
     final int M = S/20;
278
279
     Paint paint = new Paint();
280
     bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
281
     Canvas canvas = new Canvas(bitmap);
282
283
     paint.setAntiAlias(true);
284
     paint.setTextAlign(Paint.Align.CENTER);
285
     paint.setStyle(Paint.Style.FILL);
286
287
     // 3x2 bitmap = 6 squares:
288
     //
289
     // RED     GREEN   BLUE
290
     // YELLOW  WHITE   BROWN
291
292
     paint.setColor(0xff000000);                                  // BLACK BACKGROUND
293
     canvas.drawRect(0, 0, W, H, paint);                          //
294
295
     paint.setColor(0xffff0000);                                  // RED
296
     canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
297
     paint.setColor(0xff00ff00);                                  // GREEN
298
     canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
299
     paint.setColor(0xff0000ff);                                  // BLUE
300
     canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
301
     paint.setColor(0xffffff00);                                  // YELLOW
302
     canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
303
     paint.setColor(0xffffffff);                                  // WHITE
304
     canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
305
     paint.setColor(0xffb5651d);                                  // BROWN
306
     canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
307
308
     mTexture.setTexture(bitmap);
309
     }
310 15bcc6f7 Leszek Koltunski
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313 687263cc Leszek Koltunski
   float getStretchSize()
314 da231553 Leszek Koltunski
     {
315 687263cc Leszek Koltunski
     return STRETCH_SIZE;
316 da231553 Leszek Koltunski
     }
317 7f986357 Leszek Koltunski
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320 da231553 Leszek Koltunski
   int getSize()
321
     {
322
     return mSize;
323
     }
324 15bcc6f7 Leszek Koltunski
}