Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 30bc2d91

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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
24 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 27a70eae Leszek Koltunski
28 27e6c301 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29
30 27a70eae Leszek Koltunski
import org.distorted.library.effect.Effect;
31 19f0f767 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32 27a70eae Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 10585385 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
35 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
36 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37 c7e23561 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
41 ccf9fec5 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
42 19f0f767 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
43 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
44 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
45 27e6c301 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
46 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49 25445dcf Leszek Koltunski
import org.distorted.main.BuildConfig;
50 4f9f99a2 Leszek Koltunski
51 ccf9fec5 Leszek Koltunski
import java.io.DataInputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54 7c969a6d Leszek Koltunski
import java.util.Random;
55 ccf9fec5 Leszek Koltunski
56 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 9c2f0c91 Leszek Koltunski
public abstract class TwistyObject extends DistortedNode
59 fdec60a3 Leszek Koltunski
  {
60 ece1b58d Leszek Koltunski
  static final int COLOR_YELLOW = 0xffffff00;
61
  static final int COLOR_WHITE  = 0xffffffff;
62
  static final int COLOR_BLUE   = 0xff0000ff;
63 323b217c Leszek Koltunski
  static final int COLOR_GREEN  = 0xff00bb00;
64 28b54fe3 Leszek Koltunski
  static final int COLOR_RED    = 0xff990000;
65 848c7953 Leszek Koltunski
  static final int COLOR_ORANGE = 0xffff6200;
66 33b4138b Leszek Koltunski
  static final int COLOR_GREY   = 0xff727c7b;
67 5581ba2b Leszek Koltunski
  static final int COLOR_VIOLET = 0xff7700bb;
68 ee526fe0 Leszek Koltunski
  static final int COLOR_BLACK  = 0xff000000;
69 ece1b58d Leszek Koltunski
70 b89898c5 Leszek Koltunski
  static final int TEXTURE_HEIGHT = 256;
71 ae755eda Leszek Koltunski
  static final int NUM_STICKERS_IN_ROW = 4;
72 b89898c5 Leszek Koltunski
73 3f3ff476 Leszek Koltunski
  static final float SQ2 = (float)Math.sqrt(2);
74
  static final float SQ3 = (float)Math.sqrt(3);
75 bbc6da6c Leszek Koltunski
  static final float SQ5 = (float)Math.sqrt(5);
76 3f3ff476 Leszek Koltunski
  static final float SQ6 = (float)Math.sqrt(6);
77
78 ee526fe0 Leszek Koltunski
  private static final float NODE_RATIO = 1.40f;
79
  private static final float MAX_SIZE_CHANGE = 1.35f;
80 81f4fd77 Leszek Koltunski
  private static final float MIN_SIZE_CHANGE = 0.75f;
81 c7b00dfb Leszek Koltunski
82 1e6fb034 Leszek Koltunski
  private static final boolean mCreateFromDMesh = true;
83 19f0f767 Leszek Koltunski
84 8cccfb10 Leszek Koltunski
  private static final Static3D CENTER = new Static3D(0,0,0);
85 27e6c301 Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
86
87 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
88 98904e45 Leszek Koltunski
  final Static4D[] QUATS;
89 6b6504fe Leszek Koltunski
  final Cubit[] CUBITS;
90 470820a7 Leszek Koltunski
  final int NUM_FACES;
91 eab9d8f8 Leszek Koltunski
  final int NUM_TEXTURES;
92 8f53e513 Leszek Koltunski
  final int NUM_CUBIT_FACES;
93 1ebc4767 Leszek Koltunski
  final int NUM_AXIS;
94 6b6504fe Leszek Koltunski
  final int NUM_CUBITS;
95 a97e02b7 Leszek Koltunski
  final float[] CUTS;
96
  final int NUM_CUTS;
97 27a70eae Leszek Koltunski
98 b30695c6 Leszek Koltunski
  private static float mInitScreenRatio;
99
  private static float mObjectScreenRatio = 1.0f;
100 f0fa83ae Leszek Koltunski
101 5b893eee Leszek Koltunski
  private final int mNodeSize;
102 03aa05d5 Leszek Koltunski
  private final Static3D[] mOrigPos;
103
  private final Static3D mNodeScale;
104
  private final Static4D mQuat;
105
  private final int mNumLayers, mRealSize;
106
  private final ObjectList mList;
107
  private final DistortedEffects mEffects;
108
  private final VertexEffectRotate mRotateEffect;
109
  private final Dynamic1D mRotationAngle;
110
  private final Static3D mRotationAxis;
111
  private final Static3D mObjectScale;
112
  private final int[] mQuatDebug;
113 30bc2d91 Leszek Koltunski
  private final float mCameraDist;
114 03aa05d5 Leszek Koltunski
  private int mNumTexRows, mNumTexCols;
115 9224ffd2 Leszek Koltunski
  private int mRotRowBitmap;
116 efef689c Leszek Koltunski
  private int mRotAxis;
117 470820a7 Leszek Koltunski
  private MeshBase mMesh;
118 27a70eae Leszek Koltunski
119 7c969a6d Leszek Koltunski
  float[] mRowChances;
120 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
121
  DistortedTexture mTexture;
122
  MatrixEffectScale mScaleEffect;
123 4da7d87a Leszek Koltunski
  MatrixEffectQuaternion mQuatEffect;
124 27a70eae Leszek Koltunski
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126 fdec60a3 Leszek Koltunski
127 db875721 Leszek Koltunski
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
128 9c2f0c91 Leszek Koltunski
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
129 fdec60a3 Leszek Koltunski
    {
130 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
131 fdec60a3 Leszek Koltunski
132 5b893eee Leszek Koltunski
    mNodeSize = screenWidth;
133
134 c7b00dfb Leszek Koltunski
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
135 d41742f7 Leszek Koltunski
136 d99f3a48 Leszek Koltunski
    mNumLayers = numLayers;
137
    mRealSize = realSize;
138 aa171dee Leszek Koltunski
    mList = list;
139 d99f3a48 Leszek Koltunski
    mOrigPos = getCubitPositions(mNumLayers);
140 10a2e360 Leszek Koltunski
141 98904e45 Leszek Koltunski
    QUATS = getQuats();
142 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
143 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
144 1ebc4767 Leszek Koltunski
    NUM_AXIS = ROTATION_AXIS.length;
145 b30695c6 Leszek Koltunski
    mInitScreenRatio = getScreenRatio();
146 470820a7 Leszek Koltunski
    NUM_FACES = getNumFaces();
147 8f53e513 Leszek Koltunski
    NUM_CUBIT_FACES = getNumCubitFaces();
148 a64e07d0 Leszek Koltunski
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACES;
149 d99f3a48 Leszek Koltunski
    CUTS = getCuts(mNumLayers);
150 a97e02b7 Leszek Koltunski
    NUM_CUTS = CUTS.length;
151 a10ada2a Leszek Koltunski
152 a15078bb Leszek Koltunski
    mQuatDebug = new int[NUM_CUBITS];
153
154 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
155
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
156
157 c7b00dfb Leszek Koltunski
    mNodeScale= new Static3D(1,NODE_RATIO,1);
158 4da7d87a Leszek Koltunski
    mQuat = quat;
159 e844c116 Leszek Koltunski
160 a64e07d0 Leszek Koltunski
    mRowChances = getRowChances(mNumLayers);
161 7c969a6d Leszek Koltunski
162 27e6c301 Leszek Koltunski
    mRotationAngle= new Dynamic1D();
163
    mRotationAxis = new Static3D(1,0,0);
164 8cccfb10 Leszek Koltunski
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
165 27e6c301 Leszek Koltunski
166 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
167
    mRotationAngleMiddle = new Static1D(0);
168
    mRotationAngleFinal  = new Static1D(0);
169
170 d99f3a48 Leszek Koltunski
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
171 19f0f767 Leszek Koltunski
    mObjectScale = new Static3D(scale,scale,scale);
172 c7b00dfb Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mObjectScale);
173 4da7d87a Leszek Koltunski
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
174 27a70eae Leszek Koltunski
175
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
176 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
177 a10ada2a Leszek Koltunski
178 ae755eda Leszek Koltunski
    mNumTexCols = NUM_STICKERS_IN_ROW;
179
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
180
181
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
182
183 6b6504fe Leszek Koltunski
    CUBITS = new Cubit[NUM_CUBITS];
184 19f0f767 Leszek Koltunski
    createMeshAndCubits(list,res);
185 7381193e Leszek Koltunski
186 19f0f767 Leszek Koltunski
    mTexture = new DistortedTexture();
187 470820a7 Leszek Koltunski
    mEffects = new DistortedEffects();
188 10585385 Leszek Koltunski
189 98904e45 Leszek Koltunski
    int num_quats = QUATS.length;
190 10585385 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
191
      {
192 98904e45 Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
193 10585385 Leszek Koltunski
      vq.setMeshAssociation(0,q);
194
      mEffects.apply(vq);
195
      }
196
197 27e6c301 Leszek Koltunski
    mEffects.apply(mRotateEffect);
198 4da7d87a Leszek Koltunski
    mEffects.apply(mQuatEffect);
199 470820a7 Leszek Koltunski
    mEffects.apply(mScaleEffect);
200
201 dfbb340a Leszek Koltunski
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
202 b376bfd7 Leszek Koltunski
    // order for the effect to be in front of the object, we need to set the center to be behind it.
203 dfbb340a Leszek Koltunski
    getMesh().setComponentCenter(0,0,0,-0.1f);
204
205 470820a7 Leszek Koltunski
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
206
207 aa171dee Leszek Koltunski
    setupPosition(moves);
208
209 30bc2d91 Leszek Koltunski
    float fov = list.getFOV();
210
    double halfFOV = fov * (Math.PI/360);
211
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
212
213
    setProjection( fov, 0.1f);
214 27a70eae Leszek Koltunski
    }
215
216 19f0f767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218 9c2f0c91 Leszek Koltunski
  private void createMeshAndCubits(ObjectList list, Resources res)
219 19f0f767 Leszek Koltunski
    {
220
    if( mCreateFromDMesh )
221
      {
222 d99f3a48 Leszek Koltunski
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
223 19f0f767 Leszek Koltunski
      int resourceID= list.getResourceIDs()[sizeIndex];
224
225
      InputStream is = res.openRawResource(resourceID);
226
      DataInputStream dos = new DataInputStream(is);
227
      mMesh = new MeshFile(dos);
228
229
      try
230
        {
231
        is.close();
232
        }
233
      catch(IOException e)
234
        {
235
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
236
        }
237
238
      for(int i=0; i<NUM_CUBITS; i++)
239
        {
240 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
241
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
242 19f0f767 Leszek Koltunski
        }
243 eaee1ddc Leszek Koltunski
244
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
245 19f0f767 Leszek Koltunski
      }
246
    else
247
      {
248
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
249
250
      for(int i=0; i<NUM_CUBITS; i++)
251
        {
252 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
253 a64e07d0 Leszek Koltunski
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
254 19f0f767 Leszek Koltunski
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
255 6b6504fe Leszek Koltunski
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
256 19f0f767 Leszek Koltunski
        }
257
258
      mMesh = new MeshJoined(cubitMesh);
259
      resetAllTextureMaps();
260
      }
261
    }
262
263 c7b00dfb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
  public void setObjectRatio(float sizeChange)
266
    {
267
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
268
269 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
270
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
271 c7b00dfb Leszek Koltunski
272 d99f3a48 Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
273 c7b00dfb Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
274
    }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278
  static float getObjectRatio()
279
    {
280 b30695c6 Leszek Koltunski
    return mObjectScreenRatio*mInitScreenRatio;
281 c7b00dfb Leszek Koltunski
    }
282
283 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285 a97e02b7 Leszek Koltunski
  int computeRow(float x, float y, float z, int rotIndex)
286 e844c116 Leszek Koltunski
    {
287 a97e02b7 Leszek Koltunski
    Static3D axis = ROTATION_AXIS[rotIndex];
288
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
289 e844c116 Leszek Koltunski
290 a97e02b7 Leszek Koltunski
    for(int i=0; i<NUM_CUTS; i++)
291 e844c116 Leszek Koltunski
      {
292 a97e02b7 Leszek Koltunski
      if( tmp<CUTS[i] ) return i;
293 e844c116 Leszek Koltunski
      }
294
295 a97e02b7 Leszek Koltunski
    return NUM_CUTS;
296 e844c116 Leszek Koltunski
    }
297
298 985f3dfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300
  private boolean wasRotateApplied()
301
    {
302
    return mEffects.exists(mRotateEffect.getID());
303
    }
304
305 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307 9224ffd2 Leszek Koltunski
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
308 efef689c Leszek Koltunski
    {
309 6b6504fe Leszek Koltunski
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
310 9224ffd2 Leszek Koltunski
    return ((1<<cubitRow)&rowBitmap)!=0;
311 66cbdd21 Leszek Koltunski
    }
312
313 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
315
// when looking towards the direction where the axis increases in values.
316 aa171dee Leszek Koltunski
317 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
318 aa171dee Leszek Koltunski
    {
319 a31d25de Leszek Koltunski
    Static3D axis = ROTATION_AXIS[axisIndex];
320
321
    while( angleInDegrees<0 ) angleInDegrees += 360;
322
    angleInDegrees %= 360;
323
    
324
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
325
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
326
327
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
328
    }
329
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332 8bbac3c2 Leszek Koltunski
  private synchronized void setupPosition(int[][] moves)
333 a31d25de Leszek Koltunski
    {
334
    if( moves!=null )
335
      {
336
      Static4D quat;
337 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
338 a31d25de Leszek Koltunski
      int corr = (360/getBasicAngle());
339
340
      for(int[] move: moves)
341
        {
342
        axis     = move[0];
343
        rowBitmap= move[1];
344
        angle    = move[2]*corr;
345
        quat     = makeQuaternion(axis,angle);
346
347
        for(int j=0; j<NUM_CUBITS; j++)
348
          if( belongsToRotation(j,axis,rowBitmap) )
349
            {
350 6b6504fe Leszek Koltunski
            index = CUBITS[j].removeRotationNow(quat);
351
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
352 a31d25de Leszek Koltunski
            }
353
        }
354
      }
355 aa171dee Leszek Koltunski
    }
356
357 fa0f7a56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359
  int getCubitFaceColorIndex(int cubit, int face)
360
    {
361 470820a7 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
362 064ccc31 Leszek Koltunski
363
    int x = (int)(texMap.get0()/texMap.get2());
364
    int y = (int)(texMap.get1()/texMap.get3());
365
366
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
367 fa0f7a56 Leszek Koltunski
    }
368
369 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
371
372
  void clampPos(Static3D pos)
373
    {
374
    float currError, minError = Float.MAX_VALUE;
375
    int minErrorIndex= -1;
376
    float x = pos.get0();
377
    float y = pos.get1();
378
    float z = pos.get2();
379
    float xo,yo,zo;
380
381
    for(int i=0; i<NUM_CUBITS; i++)
382
      {
383
      xo = mOrigPos[i].get0();
384
      yo = mOrigPos[i].get1();
385
      zo = mOrigPos[i].get2();
386
387
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
388
389
      if( currError<minError )
390
        {
391
        minError = currError;
392
        minErrorIndex = i;
393
        }
394
      }
395
396
    pos.set( mOrigPos[minErrorIndex] );
397
    }
398
399 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
400 ae755eda Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
401 411c6285 Leszek Koltunski
402
  public void createTexture()
403
    {
404
    Bitmap bitmap;
405
406
    Paint paint = new Paint();
407 ae755eda Leszek Koltunski
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
408 411c6285 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
409
410
    paint.setAntiAlias(true);
411
    paint.setTextAlign(Paint.Align.CENTER);
412
    paint.setStyle(Paint.Style.FILL);
413
414 ee526fe0 Leszek Koltunski
    paint.setColor(COLOR_BLACK);
415 ae755eda Leszek Koltunski
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
416 411c6285 Leszek Koltunski
417 ae755eda Leszek Koltunski
    int tex = 0;
418
419
    for(int row=0; row<mNumTexRows; row++)
420
      for(int col=0; col<mNumTexCols; col++)
421
        {
422
        if( tex>=NUM_TEXTURES ) break;
423
        createFaceTexture(canvas, paint, tex, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT);
424
        tex++;
425
        }
426 411c6285 Leszek Koltunski
427 c7e23561 Leszek Koltunski
    if( !mTexture.setTexture(bitmap) )
428
      {
429
      int max = DistortedLibrary.getMaxTextureSize();
430
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
431
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
432
      }
433 411c6285 Leszek Koltunski
    }
434
435 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
436
437 d99f3a48 Leszek Koltunski
  public int getNumLayers()
438 fdec60a3 Leszek Koltunski
    {
439 d99f3a48 Leszek Koltunski
    return mNumLayers;
440 fdec60a3 Leszek Koltunski
    }
441
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
444 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
445 fdec60a3 Leszek Koltunski
    {
446 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
447 fdec60a3 Leszek Koltunski
    }
448
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450
451 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
452
      {
453 4da7d87a Leszek Koltunski
      return mQuat;
454 27a70eae Leszek Koltunski
      }
455
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
458 f18e8fae Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth)
459 fdec60a3 Leszek Koltunski
    {
460 3717a94e Leszek Koltunski
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
461 fdec60a3 Leszek Koltunski
    }
462 27a70eae Leszek Koltunski
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464
465 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
466
    {
467 6b6504fe Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
468 a10ada2a Leszek Koltunski
    }
469 f16ff19d Leszek Koltunski
470 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
471 27a70eae Leszek Koltunski
472 8bbac3c2 Leszek Koltunski
  public synchronized void restorePreferences(SharedPreferences preferences)
473 a10ada2a Leszek Koltunski
    {
474 fc3c5170 Leszek Koltunski
    boolean error = false;
475
476 2fcad75d Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
477
      {
478 a15078bb Leszek Koltunski
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
479 1d6c1eea Leszek Koltunski
480 fc3c5170 Leszek Koltunski
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
481 1d6c1eea Leszek Koltunski
        {
482 fc3c5170 Leszek Koltunski
        CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
483
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
484
        }
485
      else
486
        {
487
        error = true;
488 1d6c1eea Leszek Koltunski
        }
489 fc3c5170 Leszek Koltunski
      }
490 1d6c1eea Leszek Koltunski
491 fc3c5170 Leszek Koltunski
    if( error )
492
      {
493
      for(int i=0; i<NUM_CUBITS; i++)
494
        {
495
        CUBITS[i].solve();
496
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
497
        }
498
      recordQuatsState("Failed to restorePreferences");
499 a15078bb Leszek Koltunski
      }
500
    }
501
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
504
  public void recordQuatsState(String message)
505
    {
506
    StringBuilder quats = new StringBuilder();
507
508
    for(int j=0; j<NUM_CUBITS; j++)
509
      {
510
      quats.append(mQuatDebug[j]);
511
      quats.append(" ");
512 2fcad75d Leszek Koltunski
      }
513 a15078bb Leszek Koltunski
514 25445dcf Leszek Koltunski
    if( BuildConfig.DEBUG )
515
      {
516 2d9d9d62 Leszek Koltunski
      android.util.Log.e("quats" , quats.toString());
517 25445dcf Leszek Koltunski
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
518
      }
519
    else
520
      {
521
      Exception ex = new Exception(message);
522
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
523
      crashlytics.setCustomKey("quats" , quats.toString());
524
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
525
      crashlytics.recordException(ex);
526
      }
527 a10ada2a Leszek Koltunski
    }
528 27a70eae Leszek Koltunski
529 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
530
531
  public void releaseResources()
532
    {
533
    mTexture.markForDeletion();
534 54342a21 Leszek Koltunski
    mMesh.markForDeletion();
535
    mEffects.markForDeletion();
536
537
    for(int j=0; j<NUM_CUBITS; j++)
538
      {
539
      CUBITS[j].releaseResources();
540
      }
541 a10ada2a Leszek Koltunski
    }
542
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
545
  public void apply(Effect effect, int position)
546
    {
547 8cccfb10 Leszek Koltunski
    mEffects.apply(effect, position);
548 a10ada2a Leszek Koltunski
    }
549
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
552
  public void remove(long effectID)
553
    {
554 8cccfb10 Leszek Koltunski
    mEffects.abortById(effectID);
555 a10ada2a Leszek Koltunski
    }
556 74686c71 Leszek Koltunski
557 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
558
559 8bbac3c2 Leszek Koltunski
  public synchronized void solve()
560 a10ada2a Leszek Koltunski
    {
561 98904e45 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
562
      {
563 6b6504fe Leszek Koltunski
      CUBITS[i].solve();
564
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
565 a10ada2a Leszek Koltunski
      }
566
    }
567
568 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
569
570
  public void resetAllTextureMaps()
571
    {
572 ae755eda Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
573
    final float ratioH = 1.0f/mNumTexRows;
574
    int color, row, col;
575 380162cb Leszek Koltunski
576 ad73edd5 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
577 1f9772f3 Leszek Koltunski
      {
578 8f53e513 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
579 ad73edd5 Leszek Koltunski
580 f6d06256 Leszek Koltunski
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
581 ad73edd5 Leszek Koltunski
        {
582 d99f3a48 Leszek Koltunski
        color = getFaceColor(cubit,cubitface,mNumLayers);
583 ae755eda Leszek Koltunski
        row = (mNumTexRows-1) - color/mNumTexCols;
584
        col = color%mNumTexCols;
585
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
586 ad73edd5 Leszek Koltunski
        }
587
588 8f53e513 Leszek Koltunski
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
589 1f9772f3 Leszek Koltunski
      }
590
    }
591
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593
594
  public void setTextureMap(int cubit, int face, int newColor)
595
    {
596 064ccc31 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
597
    final float ratioH = 1.0f/mNumTexRows;
598 8f53e513 Leszek Koltunski
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
599 064ccc31 Leszek Koltunski
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
600
    int col = newColor%mNumTexCols;
601 1f9772f3 Leszek Koltunski
602 064ccc31 Leszek Koltunski
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
603 8f53e513 Leszek Koltunski
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
604 1f9772f3 Leszek Koltunski
    }
605
606 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
607
608 8bbac3c2 Leszek Koltunski
  public synchronized void beginNewRotation(int axis, int row )
609 a10ada2a Leszek Koltunski
    {
610 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
611
      {
612
      android.util.Log.e("object", "invalid rotation axis: "+axis);
613
      return;
614
      }
615 d99f3a48 Leszek Koltunski
    if( row<0 || row>=mNumLayers )
616 9cd7695f Leszek Koltunski
      {
617
      android.util.Log.e("object", "invalid rotation row: "+row);
618
      return;
619
      }
620
621 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
622
    mRotRowBitmap= (1<<row);
623 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
624 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
625
    mRotationAngle.add(mRotationAngleStatic);
626 9c2f0c91 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
627 27e6c301 Leszek Koltunski
    }
628 a10ada2a Leszek Koltunski
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630
631 8bbac3c2 Leszek Koltunski
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
632 27e6c301 Leszek Koltunski
    {
633 985f3dfa Leszek Koltunski
    if( wasRotateApplied() )
634
      {
635
      mRotAxis     = axis;
636
      mRotRowBitmap= rowBitmap;
637
638
      mRotationAngleStatic.set0(0.0f);
639
      mRotationAxis.set( ROTATION_AXIS[axis] );
640
      mRotationAngle.setDuration(durationMillis);
641
      mRotationAngle.resetToBeginning();
642
      mRotationAngle.add(new Static1D(0));
643
      mRotationAngle.add(new Static1D(angle));
644
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
645
      mRotateEffect.notifyWhenFinished(listener);
646
647
      return mRotateEffect.getID();
648
      }
649 27e6c301 Leszek Koltunski
650 985f3dfa Leszek Koltunski
    return 0;
651 27e6c301 Leszek Koltunski
    }
652 a10ada2a Leszek Koltunski
653 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
654 a10ada2a Leszek Koltunski
655 168b6b56 Leszek Koltunski
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
656 27e6c301 Leszek Koltunski
    {
657 985f3dfa Leszek Koltunski
    if( wasRotateApplied() )
658
      {
659
      float angle = getAngle();
660
      mRotationAngleStatic.set0(angle);
661
      mRotationAngleFinal.set0(nearestAngleInDegrees);
662
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
663
664
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
665
      mRotationAngle.resetToBeginning();
666
      mRotationAngle.removeAll();
667
      mRotationAngle.add(mRotationAngleStatic);
668
      mRotationAngle.add(mRotationAngleMiddle);
669
      mRotationAngle.add(mRotationAngleFinal);
670
      mRotateEffect.notifyWhenFinished(listener);
671
672
      return mRotateEffect.getID();
673
      }
674 27e6c301 Leszek Koltunski
675 985f3dfa Leszek Koltunski
    return 0;
676 27e6c301 Leszek Koltunski
    }
677 001cc0e4 Leszek Koltunski
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679
680 27e6c301 Leszek Koltunski
  private float getAngle()
681 001cc0e4 Leszek Koltunski
    {
682 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
683 001cc0e4 Leszek Koltunski
684 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
685 001cc0e4 Leszek Koltunski
      {
686 27e6c301 Leszek Koltunski
      return mRotationAngle.getPoint(pointNum-1).get0();
687
      }
688
    else
689
      {
690
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
691
      crashlytics.log("points in RotationAngle: "+pointNum);
692
      return 0;
693 001cc0e4 Leszek Koltunski
      }
694
    }
695
696 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
697
698 8bbac3c2 Leszek Koltunski
  public synchronized void removeRotationNow()
699 168b6b56 Leszek Koltunski
    {
700
    float angle = getAngle();
701
    double nearestAngleInRadians = angle*Math.PI/180;
702
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
703
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
704
    float axisX = ROTATION_AXIS[mRotAxis].get0();
705
    float axisY = ROTATION_AXIS[mRotAxis].get1();
706
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
707
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
708
709
    mRotationAngle.removeAll();
710
    mRotationAngleStatic.set0(0);
711
712
    for(int i=0; i<NUM_CUBITS; i++)
713
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
714
        {
715 6b6504fe Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
716
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
717 168b6b56 Leszek Koltunski
        }
718
    }
719 a10ada2a Leszek Koltunski
720 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
721
722 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
723 aa171dee Leszek Koltunski
    {
724
    solve();
725
    setupPosition(moves);
726
    }
727
728 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
729
730
  public int getCubit(float[] point3D)
731
    {
732 418aa554 Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
733 9621255f Leszek Koltunski
    int currentBest=-1;
734
    float multiplier = returnMultiplier();
735
736
    point3D[0] *= multiplier;
737
    point3D[1] *= multiplier;
738
    point3D[2] *= multiplier;
739
740
    for(int i=0; i<NUM_CUBITS; i++)
741
      {
742 6b6504fe Leszek Koltunski
      dist = CUBITS[i].getDistSquared(point3D);
743 9621255f Leszek Koltunski
      if( dist<minDist )
744
        {
745
        minDist = dist;
746
        currentBest = i;
747
        }
748
      }
749
750
    return currentBest;
751
    }
752
753 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
754
755 168b6b56 Leszek Koltunski
  public int computeNearestAngle(float angle, float speed)
756 0e5ad27c Leszek Koltunski
    {
757
    final int NEAREST = 360/getBasicAngle();
758
759 4c864c68 Leszek Koltunski
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
760
    if( angle< -(NEAREST*0.5) ) tmp-=1;
761 168b6b56 Leszek Koltunski
762 4c864c68 Leszek Koltunski
    if( tmp!=0 ) return NEAREST*tmp;
763 168b6b56 Leszek Koltunski
764 c7b00dfb Leszek Koltunski
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
765 0e5ad27c Leszek Koltunski
    }
766
767 30bc2d91 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
768
769
  public float getCameraDist()
770
    {
771
    return mCameraDist;
772
    }
773
774 5b893eee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
775
776
  public int getNodeSize()
777
    {
778
    return mNodeSize;
779
    }
780
781 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
782
783 9c2f0c91 Leszek Koltunski
  public ObjectList getObjectList()
784 aa171dee Leszek Koltunski
    {
785
    return mList;
786
    }
787
788 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
789
790 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
791 ae755eda Leszek Koltunski
  abstract Static3D[] getCubitPositions(int numLayers);
792 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
793 411c6285 Leszek Koltunski
  abstract int getNumFaces();
794 a64e07d0 Leszek Koltunski
  abstract int getNumStickerTypes(int numLayers);
795 8f53e513 Leszek Koltunski
  abstract int getNumCubitFaces();
796 a64e07d0 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit, int numLayers);
797 ae755eda Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
798
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
799 fb377dae Leszek Koltunski
  abstract float returnMultiplier();
800 a64e07d0 Leszek Koltunski
  abstract float[] getRowChances(int numLayers);
801 ae755eda Leszek Koltunski
  abstract float[] getCuts(int numLayers);
802 eaee1ddc Leszek Koltunski
  abstract boolean shouldResetTextureMaps();
803 7c969a6d Leszek Koltunski
804 6b6504fe Leszek Koltunski
  public abstract boolean isSolved();
805 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
806 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
807 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
808 7c969a6d Leszek Koltunski
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
809
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
810 6fd4a72c Leszek Koltunski
  public abstract int getObjectName(int numLayers);
811
  public abstract int getInventor(int numLayers);
812
  public abstract int getComplexity(int numLayers);
813 fdec60a3 Leszek Koltunski
  }