Project

General

Profile

Download (13.1 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / object / RubikObject.java @ 10a2e360

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 4f9f99a2 Leszek Koltunski
package org.distorted.object;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26 27a70eae Leszek Koltunski
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
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.DistortedNode;
33
import org.distorted.library.main.DistortedTexture;
34 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
35 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
37 f16ff19d Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 4f9f99a2 Leszek Koltunski
42 ba740a0c Leszek Koltunski
import static org.distorted.magic.RubikRenderer.NODE_FBO_SIZE;
43 d41742f7 Leszek Koltunski
44 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 27a70eae Leszek Koltunski
public abstract class RubikObject extends DistortedNode
47 fdec60a3 Leszek Koltunski
  {
48 411c6285 Leszek Koltunski
  static final int TEXTURE_HEIGHT = 128;
49 27a70eae Leszek Koltunski
  static final float OBJECT_SCREEN_RATIO = 0.5f;
50 a10ada2a Leszek Koltunski
  final float[] LEGAL_QUATS;
51 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
52 27a70eae Leszek Koltunski
53 f16ff19d Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
54 a10ada2a Leszek Koltunski
  private final int NUM_CUBITS;
55 3c4a326c Leszek Koltunski
  private int mRotRow;
56 efef689c Leszek Koltunski
  private int mRotAxis;
57 5ba13c05 Leszek Koltunski
  private Static3D mScale, mNodeScale;
58 27a70eae Leszek Koltunski
  private Static4D mQuatAccumulated;
59 b32444ee Leszek Koltunski
  private Cubit[] mCubits;
60 27a70eae Leszek Koltunski
61 a10ada2a Leszek Koltunski
  int mSize;
62 fdec60a3 Leszek Koltunski
63 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
64
  DistortedTexture mTexture;
65
66
  VertexEffectSink mSinkEffect;
67
  MatrixEffectScale mScaleEffect;
68
  MatrixEffectQuaternion mQuatCEffect;
69
  MatrixEffectQuaternion mQuatAEffect;
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72 fdec60a3 Leszek Koltunski
73 411c6285 Leszek Koltunski
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture, MeshRectangles nodeMesh, DistortedEffects nodeEffects)
74 fdec60a3 Leszek Koltunski
    {
75 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
76 fdec60a3 Leszek Koltunski
77 ba740a0c Leszek Koltunski
    resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
78 d41742f7 Leszek Koltunski
79 10a2e360 Leszek Koltunski
    Static3D[] positions = getCubitPositions(size);
80
81 f16ff19d Leszek Koltunski
    LEGAL_QUATS = getLegalQuats();
82 10a2e360 Leszek Koltunski
    NUM_CUBITS  = positions.length;
83 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
84 a10ada2a Leszek Koltunski
85 27a70eae Leszek Koltunski
    mSize = size;
86
87
    mRotationAngleStatic = new Static1D(0);
88
    mRotationAngleMiddle = new Static1D(0);
89
    mRotationAngleFinal  = new Static1D(0);
90
91
    mScale    = new Static3D(1,1,1);
92
    mNodeScale= new Static3D(1,1,1);
93
94
    mQuatAccumulated = quatAcc;
95
96 5ba13c05 Leszek Koltunski
    Static3D center = new Static3D(0,0,0);
97 5974d2ae Leszek Koltunski
    Static4D region = new Static4D(0,0,0,0.72f);
98 27a70eae Leszek Koltunski
99 5ba13c05 Leszek Koltunski
    mSinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), center, region );
100 27a70eae Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mScale);
101 5ba13c05 Leszek Koltunski
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, center);
102
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, center);
103 27a70eae Leszek Koltunski
104
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
105 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
106 a10ada2a Leszek Koltunski
107
    mCubits = new Cubit[NUM_CUBITS];
108 5974d2ae Leszek Koltunski
    mTexture = new DistortedTexture();
109 a10ada2a Leszek Koltunski
110
    int vertices = (int)(24.0f/mSize + 2.0f);
111
112
    for(int i=0; i<NUM_CUBITS; i++)
113
      {
114 efef689c Leszek Koltunski
      MeshBase cubitMesh = createCubitMesh(vertices);
115 10a2e360 Leszek Koltunski
      mCubits[i] = new Cubit(this,cubitMesh,positions[i]);
116 efef689c Leszek Koltunski
      textureCubitMesh(cubitMesh,i);
117 a10ada2a Leszek Koltunski
118
      attach(mCubits[i].mNode);
119
      }
120 27a70eae Leszek Koltunski
    }
121
122 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private void textureCubitMesh(MeshBase mesh, int cubit)
125
    {
126
    boolean belongs;
127
    final int numFaces = getNumFaces();
128
    final Static4D[] maps = new Static4D[numFaces];
129
    final float ratio = 1.0f/(numFaces+1);
130
131
    if( 2*ROTATION_AXIS.length == numFaces )
132
      {
133
      for(int i=0; i<numFaces; i++)
134
        {
135
        belongs = belongsToRotation(cubit, i/2, i%2==0 ? mSize-1:0 );
136
        maps[i] = new Static4D( (belongs?i:6)*ratio, 0.0f, ratio, 1.0f);
137
        }
138
      }
139
    else if( ROTATION_AXIS.length == numFaces )
140
      {
141
      for(int i=0; i<numFaces; i++)
142
        {
143
        belongs = belongsToRotation(cubit, i, mSize-1 );
144
        maps[i] = new Static4D( (belongs?i:6)*ratio, 0.0f, ratio, 1.0f);
145
        }
146
      }
147
148
    mesh.setTextureMap(maps);
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
  private boolean belongsToRotation( int cubit, int axis, int row)
154
    {
155 10a2e360 Leszek Koltunski
    return mCubits[cubit].mRotationRow[axis]==row;
156 efef689c Leszek Koltunski
    }
157
158 f16ff19d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 a10ada2a Leszek Koltunski
  private void resetRotationAngle(Dynamic1D rotationAngle)
161 f16ff19d Leszek Koltunski
    {
162
    rotationAngle.setDuration(POST_ROTATION_MILLISEC);
163
    rotationAngle.resetToBeginning();
164
    rotationAngle.removeAll();
165
    rotationAngle.add(mRotationAngleStatic);
166
    rotationAngle.add(mRotationAngleMiddle);
167
    rotationAngle.add(mRotationAngleFinal);
168
    }
169
170 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 27a70eae Leszek Koltunski
  private float getSinkStrength()
173 dd73fdab Leszek Koltunski
    {
174 27a70eae Leszek Koltunski
    switch(mSize)
175
      {
176
      case 1 : return 1.1f;
177
      case 2 : return 1.5f;
178
      case 3 : return 1.8f;
179
      case 4 : return 2.0f;
180
      default: return 3.0f - 4.0f/mSize;
181
      }
182 dd73fdab Leszek Koltunski
    }
183
184 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// the getFaceColors + final black in a horizontal strip.
186
187
  public void createTexture()
188
    {
189
    Bitmap bitmap;
190
191
    final int numColors = getNumFaces();
192
193
    Paint paint = new Paint();
194
    bitmap = Bitmap.createBitmap( (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
195
    Canvas canvas = new Canvas(bitmap);
196
197
    paint.setAntiAlias(true);
198
    paint.setTextAlign(Paint.Align.CENTER);
199
    paint.setStyle(Paint.Style.FILL);
200
201
    paint.setColor(0xff000000);
202
    canvas.drawRect(0, 0, (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
203
204
    for(int i=0; i<numColors; i++)
205
      {
206
      createFaceTexture(canvas,paint,i);
207
      }
208
209
    mTexture.setTexture(bitmap);
210
    }
211
212 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214 27a70eae Leszek Koltunski
  public int getSize()
215 fdec60a3 Leszek Koltunski
    {
216 27a70eae Leszek Koltunski
    return mSize;
217 fdec60a3 Leszek Koltunski
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
222 fdec60a3 Leszek Koltunski
    {
223 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
224 fdec60a3 Leszek Koltunski
    }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
229
      {
230
      return mQuatAccumulated;
231
      }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 5ba13c05 Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
236 fdec60a3 Leszek Koltunski
    {
237 ba740a0c Leszek Koltunski
    float factor = scrWidth>scrHeight ? scrHeight : scrWidth;
238
    float scaleFactor = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
239 27a70eae Leszek Koltunski
240 5ba13c05 Leszek Koltunski
    mNodeScale.set(factor,factor,factor);
241 27a70eae Leszek Koltunski
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
242 fdec60a3 Leszek Koltunski
    }
243 27a70eae Leszek Koltunski
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
247
    {
248
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
249
    }
250 f16ff19d Leszek Koltunski
251 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252 27a70eae Leszek Koltunski
253 a10ada2a Leszek Koltunski
  public void restorePreferences(SharedPreferences preferences)
254
    {
255
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
256
    }
257 27a70eae Leszek Koltunski
258 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
259 74686c71 Leszek Koltunski
260 a10ada2a Leszek Koltunski
  public long finishRotationNow(EffectListener listener)
261
    {
262
    boolean first = true;
263
    long effectID=0;
264
265
    for(int i=0; i<NUM_CUBITS; i++)
266
      {
267 efef689c Leszek Koltunski
      if( belongsToRotation(i,mRotAxis,mRotRow) )
268 a10ada2a Leszek Koltunski
        {
269
        if( first )
270
          {
271
          first=false;
272
          effectID = mCubits[i].finishRotationNow(listener);
273
          }
274
        resetRotationAngle(mCubits[i].mRotationAngle);
275
        }
276
      }
277
278
    return effectID;
279
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  public void releaseResources()
284
    {
285
    mTexture.markForDeletion();
286
287
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].releaseResources();
288
    }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292
  public void apply(Effect effect, int position)
293
    {
294
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.apply(effect, position);
295
    }
296
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
299
  public void remove(long effectID)
300
    {
301
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.abortById(effectID);
302
    }
303 74686c71 Leszek Koltunski
304 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
  public void solve()
307
    {
308
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve();
309
    }
310
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
  public boolean isSolved()
314
    {
315
    Static4D q = mCubits[0].mQuatScramble;
316
317
    float x = q.get0();
318
    float y = q.get1();
319
    float z = q.get2();
320
    float w = q.get3();
321
322
    for(int i=0; i<NUM_CUBITS; i++)
323
      {
324
      q = mCubits[i].mQuatScramble;
325
326
      if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) return false;
327
      }
328
329
    return true;
330
    }
331
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
334 efef689c Leszek Koltunski
  public void beginNewRotation(int axis, int row )
335 a10ada2a Leszek Koltunski
    {
336 3c4a326c Leszek Koltunski
    mRotAxis = axis;
337 a10ada2a Leszek Koltunski
    mRotRow  = row;
338
339
    mRotationAngleStatic.set0(0.0f);
340
341
    for(int i=0; i<NUM_CUBITS; i++)
342 efef689c Leszek Koltunski
      if( belongsToRotation(i,mRotAxis,mRotRow) )
343 a10ada2a Leszek Koltunski
        {
344
        mCubits[i].beginNewRotation(axis);
345
        }
346
     }
347
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
350 efef689c Leszek Koltunski
  public long addNewRotation( int axis, int row, int angle, long durationMillis, EffectListener listener )
351 a10ada2a Leszek Koltunski
     {
352
     long effectID=0;
353
     boolean first = true;
354
355 3c4a326c Leszek Koltunski
     mRotAxis = axis;
356 a10ada2a Leszek Koltunski
     mRotRow  = row;
357
358
     mRotationAngleStatic.set0(0.0f);
359
360
     for(int i=0; i<NUM_CUBITS; i++)
361 efef689c Leszek Koltunski
       if( belongsToRotation(i,mRotAxis,mRotRow) )
362 a10ada2a Leszek Koltunski
         {
363
         mCubits[i].addNewRotation(axis,durationMillis,angle);
364
365
         if( first )
366
           {
367
           first = false;
368
           effectID = mCubits[i].setUpCallback(listener);
369
           }
370
         }
371
372
     return effectID;
373
     }
374
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377
  public void removeRotationNow()
378
     {
379
     boolean first = true;
380
     Static4D quat = null;
381
382
     for(int i=0; i<NUM_CUBITS; i++)
383 efef689c Leszek Koltunski
       if( belongsToRotation(i,mRotAxis,mRotRow) )
384 a10ada2a Leszek Koltunski
         {
385
         if( first )
386
           {
387
           first = false;
388 3c4a326c Leszek Koltunski
           quat = mCubits[i].returnRotationQuat(mRotAxis);
389 a10ada2a Leszek Koltunski
           }
390
391
         mCubits[i].removeRotationNow(quat);
392
         }
393
394
     mRotationAngleStatic.set0(0);
395
     }
396
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
399 10a2e360 Leszek Koltunski
  public int getNumRotations()
400
    {
401
    return ROTATION_AXIS.length;
402
    }
403
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
406
  abstract Static3D[] getCubitPositions(int size);
407 a10ada2a Leszek Koltunski
  abstract float[] getLegalQuats();
408 411c6285 Leszek Koltunski
  abstract int getNumFaces();
409
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face);
410
  abstract MeshBase createCubitMesh(int vertices);
411 efef689c Leszek Koltunski
  abstract Static3D[] getRotationAxis();
412 fdec60a3 Leszek Koltunski
  }