Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ be576d14

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
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedTexture;
39 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
40 ccf9fec5 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
41 19f0f767 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
42 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
43 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
44 27e6c301 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
45 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48 4f9f99a2 Leszek Koltunski
49 ccf9fec5 Leszek Koltunski
import java.io.DataInputStream;
50
import java.io.IOException;
51
import java.io.InputStream;
52 7c969a6d Leszek Koltunski
import java.util.Random;
53 ccf9fec5 Leszek Koltunski
54 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 9c2f0c91 Leszek Koltunski
public abstract class TwistyObject extends DistortedNode
57 fdec60a3 Leszek Koltunski
  {
58 ece1b58d Leszek Koltunski
  static final int COLOR_YELLOW = 0xffffff00;
59
  static final int COLOR_WHITE  = 0xffffffff;
60
  static final int COLOR_BLUE   = 0xff0000ff;
61
  static final int COLOR_GREEN  = 0xff00ff00;
62
  static final int COLOR_RED    = 0xffff0000;
63
  static final int COLOR_BROWN  = 0xffb5651d;
64
  static final int COLOR_PINK   = 0xffe134eb;
65
  static final int COLOR_VIOLET = 0xffa534eb;
66
67 c7b00dfb Leszek Koltunski
  private static final float NODE_RATIO = 1.32f;
68
  private static final float MAX_SIZE_CHANGE = 1.3f;
69
  private static final float MIN_SIZE_CHANGE = 0.8f;
70
71 44f95b2a Leszek Koltunski
  private static boolean mCreateFromDMesh = true;
72 19f0f767 Leszek Koltunski
73 8cccfb10 Leszek Koltunski
  private static final Static3D CENTER = new Static3D(0,0,0);
74 14bd7976 Leszek Koltunski
  static final int INTERIOR_COLOR = 0xff000000;
75 27e6c301 Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
76 fb377dae Leszek Koltunski
  private static final int TEXTURE_HEIGHT = 256;
77 27e6c301 Leszek Koltunski
78 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
79 98904e45 Leszek Koltunski
  final Static4D[] QUATS;
80 6b6504fe Leszek Koltunski
  final Cubit[] CUBITS;
81 470820a7 Leszek Koltunski
  final int NUM_FACES;
82 eab9d8f8 Leszek Koltunski
  final int NUM_TEXTURES;
83 8f53e513 Leszek Koltunski
  final int NUM_CUBIT_FACES;
84 1ebc4767 Leszek Koltunski
  final int NUM_AXIS;
85 6b6504fe Leszek Koltunski
  final int NUM_CUBITS;
86 7403cdfa Leszek Koltunski
  final float BASIC_STEP;
87 27a70eae Leszek Koltunski
88 b30695c6 Leszek Koltunski
  private static float mInitScreenRatio;
89
  private static float mObjectScreenRatio = 1.0f;
90 f0fa83ae Leszek Koltunski
91 5b893eee Leszek Koltunski
  private final int mNodeSize;
92 9224ffd2 Leszek Koltunski
  private int mRotRowBitmap;
93 efef689c Leszek Koltunski
  private int mRotAxis;
94 49f67f9b Leszek Koltunski
  private Static3D[] mOrigPos;
95 fb6a40c8 Leszek Koltunski
  private Static3D mNodeScale;
96 4da7d87a Leszek Koltunski
  private Static4D mQuat;
97 49f67f9b Leszek Koltunski
  private int mSize;
98 9c2f0c91 Leszek Koltunski
  private ObjectList mList;
99 470820a7 Leszek Koltunski
  private MeshBase mMesh;
100
  private DistortedEffects mEffects;
101 27e6c301 Leszek Koltunski
  private VertexEffectRotate mRotateEffect;
102
  private Dynamic1D mRotationAngle;
103
  private Static3D mRotationAxis;
104 c7b00dfb Leszek Koltunski
  private Static3D mObjectScale;
105 27a70eae Leszek Koltunski
106 e844c116 Leszek Koltunski
  float mStart, mStep;
107 7c969a6d Leszek Koltunski
  float[] mRowChances;
108 fdec60a3 Leszek Koltunski
109 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
110
  DistortedTexture mTexture;
111
112
  MatrixEffectScale mScaleEffect;
113 4da7d87a Leszek Koltunski
  MatrixEffectQuaternion mQuatEffect;
114 27a70eae Leszek Koltunski
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116 fdec60a3 Leszek Koltunski
117 9c2f0c91 Leszek Koltunski
  TwistyObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
118
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
119 fdec60a3 Leszek Koltunski
    {
120 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
121 fdec60a3 Leszek Koltunski
122 5b893eee Leszek Koltunski
    mNodeSize = screenWidth;
123
124 c7b00dfb Leszek Koltunski
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
125 d41742f7 Leszek Koltunski
126 aa171dee Leszek Koltunski
    mList = list;
127 49f67f9b Leszek Koltunski
    mOrigPos = getCubitPositions(size);
128 10a2e360 Leszek Koltunski
129 98904e45 Leszek Koltunski
    QUATS = getQuats();
130 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
131 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
132 1ebc4767 Leszek Koltunski
    NUM_AXIS = ROTATION_AXIS.length;
133 b30695c6 Leszek Koltunski
    mInitScreenRatio = getScreenRatio();
134 470820a7 Leszek Koltunski
    NUM_FACES = getNumFaces();
135 8f53e513 Leszek Koltunski
    NUM_CUBIT_FACES = getNumCubitFaces();
136 eab9d8f8 Leszek Koltunski
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
137 7403cdfa Leszek Koltunski
    BASIC_STEP = getBasicStep();
138 a10ada2a Leszek Koltunski
139 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
140
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
141
142 27a70eae Leszek Koltunski
    mSize = size;
143 49f67f9b Leszek Koltunski
    computeStartAndStep(mOrigPos);
144 c7b00dfb Leszek Koltunski
    mNodeScale= new Static3D(1,NODE_RATIO,1);
145 4da7d87a Leszek Koltunski
    mQuat = quat;
146 e844c116 Leszek Koltunski
147 7c969a6d Leszek Koltunski
    mRowChances = getRowChances();
148
149 27e6c301 Leszek Koltunski
    mRotationAngle= new Dynamic1D();
150
    mRotationAxis = new Static3D(1,0,0);
151 8cccfb10 Leszek Koltunski
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
152 27e6c301 Leszek Koltunski
153 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
154
    mRotationAngleMiddle = new Static1D(0);
155
    mRotationAngleFinal  = new Static1D(0);
156
157 b30695c6 Leszek Koltunski
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
158 19f0f767 Leszek Koltunski
    mObjectScale = new Static3D(scale,scale,scale);
159 c7b00dfb Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mObjectScale);
160 4da7d87a Leszek Koltunski
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
161 27a70eae Leszek Koltunski
162
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
163 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
164 a10ada2a Leszek Koltunski
165 6b6504fe Leszek Koltunski
    CUBITS = new Cubit[NUM_CUBITS];
166 19f0f767 Leszek Koltunski
    createMeshAndCubits(list,res);
167 7381193e Leszek Koltunski
168 19f0f767 Leszek Koltunski
    mTexture = new DistortedTexture();
169 470820a7 Leszek Koltunski
    mEffects = new DistortedEffects();
170 10585385 Leszek Koltunski
171 98904e45 Leszek Koltunski
    int num_quats = QUATS.length;
172 10585385 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
173
      {
174 98904e45 Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
175 10585385 Leszek Koltunski
      vq.setMeshAssociation(0,q);
176
      mEffects.apply(vq);
177
      }
178
179 27e6c301 Leszek Koltunski
    mEffects.apply(mRotateEffect);
180 4da7d87a Leszek Koltunski
    mEffects.apply(mQuatEffect);
181 470820a7 Leszek Koltunski
    mEffects.apply(mScaleEffect);
182
183 dfbb340a Leszek Koltunski
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
184 b376bfd7 Leszek Koltunski
    // order for the effect to be in front of the object, we need to set the center to be behind it.
185 dfbb340a Leszek Koltunski
    getMesh().setComponentCenter(0,0,0,-0.1f);
186
187 470820a7 Leszek Koltunski
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
188
189 aa171dee Leszek Koltunski
    setupPosition(moves);
190
191 4888e97c Leszek Koltunski
    setProjection(fov, 0.1f);
192 27a70eae Leszek Koltunski
    }
193
194 19f0f767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 9c2f0c91 Leszek Koltunski
  private void createMeshAndCubits(ObjectList list, Resources res)
197 19f0f767 Leszek Koltunski
    {
198
    if( mCreateFromDMesh )
199
      {
200 9c2f0c91 Leszek Koltunski
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mSize);
201 19f0f767 Leszek Koltunski
      int resourceID= list.getResourceIDs()[sizeIndex];
202
203
      InputStream is = res.openRawResource(resourceID);
204
      DataInputStream dos = new DataInputStream(is);
205
      mMesh = new MeshFile(dos);
206
207
      try
208
        {
209
        is.close();
210
        }
211
      catch(IOException e)
212
        {
213
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
214
        }
215
216
      for(int i=0; i<NUM_CUBITS; i++)
217
        {
218 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
219
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
220 19f0f767 Leszek Koltunski
        }
221 eaee1ddc Leszek Koltunski
222
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
223 19f0f767 Leszek Koltunski
      }
224
    else
225
      {
226
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
227
228
      for(int i=0; i<NUM_CUBITS; i++)
229
        {
230 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
231 19f0f767 Leszek Koltunski
        cubitMesh[i] = createCubitMesh(i);
232
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
233 6b6504fe Leszek Koltunski
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
234 19f0f767 Leszek Koltunski
        }
235
236
      mMesh = new MeshJoined(cubitMesh);
237
      resetAllTextureMaps();
238
      }
239
    }
240
241 c7b00dfb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243
  public void setObjectRatio(float sizeChange)
244
    {
245
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
246
247 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
248
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
249 c7b00dfb Leszek Koltunski
250 b30695c6 Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
251 c7b00dfb Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
252
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  static float getObjectRatio()
257
    {
258 b30695c6 Leszek Koltunski
    return mObjectScreenRatio*mInitScreenRatio;
259 c7b00dfb Leszek Koltunski
    }
260
261 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262 97d2f701 Leszek Koltunski
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
263
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
264
// consecutive).
265
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
266
// the Cube and the Pyraminx.
267
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
268
// given Cubit belongs to.
269 e844c116 Leszek Koltunski
270
  private void computeStartAndStep(Static3D[] pos)
271
    {
272
    float min = Float.MAX_VALUE;
273
    float max = Float.MIN_VALUE;
274
    float axisX = ROTATION_AXIS[0].get0();
275
    float axisY = ROTATION_AXIS[0].get1();
276
    float axisZ = ROTATION_AXIS[0].get2();
277
    float tmp;
278
279
    for(int i=0; i<NUM_CUBITS; i++)
280
      {
281
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
282
      if( tmp<min ) min=tmp;
283
      if( tmp>max ) max=tmp;
284
      }
285
286
    mStart = min;
287 7403cdfa Leszek Koltunski
    mStep  = (max-min+BASIC_STEP)/mSize;
288 e844c116 Leszek Koltunski
    }
289
290 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292 9224ffd2 Leszek Koltunski
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
293 efef689c Leszek Koltunski
    {
294 6b6504fe Leszek Koltunski
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
295 9224ffd2 Leszek Koltunski
    return ((1<<cubitRow)&rowBitmap)!=0;
296 66cbdd21 Leszek Koltunski
    }
297
298 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
299 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
300
// when looking towards the direction where the axis increases in values.
301 aa171dee Leszek Koltunski
302 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
303 aa171dee Leszek Koltunski
    {
304 a31d25de Leszek Koltunski
    Static3D axis = ROTATION_AXIS[axisIndex];
305
306
    while( angleInDegrees<0 ) angleInDegrees += 360;
307
    angleInDegrees %= 360;
308
    
309
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
310
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
311
312
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
313
    }
314
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317 8bbac3c2 Leszek Koltunski
  private synchronized void setupPosition(int[][] moves)
318 a31d25de Leszek Koltunski
    {
319
    if( moves!=null )
320
      {
321
      Static4D quat;
322 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
323 a31d25de Leszek Koltunski
      int corr = (360/getBasicAngle());
324
325
      for(int[] move: moves)
326
        {
327
        axis     = move[0];
328
        rowBitmap= move[1];
329
        angle    = move[2]*corr;
330
        quat     = makeQuaternion(axis,angle);
331
332
        for(int j=0; j<NUM_CUBITS; j++)
333
          if( belongsToRotation(j,axis,rowBitmap) )
334
            {
335 6b6504fe Leszek Koltunski
            index = CUBITS[j].removeRotationNow(quat);
336
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
337 a31d25de Leszek Koltunski
            }
338
        }
339
      }
340 aa171dee Leszek Koltunski
    }
341
342 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
344
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
345
// away from the face of the Pyraminx shouldn't be textured.
346
347
  boolean isOnFace( int cubit, int axis, int row)
348
    {
349
    final float MAX_ERROR = 0.0001f;
350 6b6504fe Leszek Koltunski
    float diff = CUBITS[cubit].mRotationRow[axis] - row;
351 f6d06256 Leszek Koltunski
    return diff*diff < MAX_ERROR;
352
    }
353
354 fa0f7a56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
  int getCubitFaceColorIndex(int cubit, int face)
357
    {
358 470820a7 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
359
    return (int)(texMap.get0() / texMap.get2());
360 fa0f7a56 Leszek Koltunski
    }
361
362 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
363
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
364
365
  void clampPos(Static3D pos)
366
    {
367
    float currError, minError = Float.MAX_VALUE;
368
    int minErrorIndex= -1;
369
    float x = pos.get0();
370
    float y = pos.get1();
371
    float z = pos.get2();
372
    float xo,yo,zo;
373
374
    for(int i=0; i<NUM_CUBITS; i++)
375
      {
376
      xo = mOrigPos[i].get0();
377
      yo = mOrigPos[i].get1();
378
      zo = mOrigPos[i].get2();
379
380
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
381
382
      if( currError<minError )
383
        {
384
        minError = currError;
385
        minErrorIndex = i;
386
        }
387
      }
388
389
    pos.set( mOrigPos[minErrorIndex] );
390
    }
391
392 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
393
// the getFaceColors + final black in a horizontal strip.
394
395
  public void createTexture()
396
    {
397
    Bitmap bitmap;
398
399
    Paint paint = new Paint();
400 eab9d8f8 Leszek Koltunski
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
401 411c6285 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
402
403
    paint.setAntiAlias(true);
404
    paint.setTextAlign(Paint.Align.CENTER);
405
    paint.setStyle(Paint.Style.FILL);
406
407 14bd7976 Leszek Koltunski
    paint.setColor(INTERIOR_COLOR);
408 eab9d8f8 Leszek Koltunski
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
409 411c6285 Leszek Koltunski
410 eab9d8f8 Leszek Koltunski
    for(int i=0; i<NUM_TEXTURES; i++)
411 411c6285 Leszek Koltunski
      {
412 ca292407 Leszek Koltunski
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
413 411c6285 Leszek Koltunski
      }
414
415
    mTexture.setTexture(bitmap);
416
    }
417
418 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
419
420 27a70eae Leszek Koltunski
  public int getSize()
421 fdec60a3 Leszek Koltunski
    {
422 27a70eae Leszek Koltunski
    return mSize;
423 fdec60a3 Leszek Koltunski
    }
424
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
427 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
428 fdec60a3 Leszek Koltunski
    {
429 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
430 fdec60a3 Leszek Koltunski
    }
431
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
434 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
435
      {
436 4da7d87a Leszek Koltunski
      return mQuat;
437 27a70eae Leszek Koltunski
      }
438
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440
441 5ba13c05 Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
442 fdec60a3 Leszek Koltunski
    {
443 3717a94e Leszek Koltunski
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
444 fdec60a3 Leszek Koltunski
    }
445 27a70eae Leszek Koltunski
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
448 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
449
    {
450 6b6504fe Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
451 a10ada2a Leszek Koltunski
    }
452 f16ff19d Leszek Koltunski
453 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
454 27a70eae Leszek Koltunski
455 8bbac3c2 Leszek Koltunski
  public synchronized void restorePreferences(SharedPreferences preferences)
456 a10ada2a Leszek Koltunski
    {
457 2fcad75d Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
458
      {
459 6b6504fe Leszek Koltunski
      int index = CUBITS[i].restorePreferences(preferences);
460
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
461 2fcad75d Leszek Koltunski
      }
462 a10ada2a Leszek Koltunski
    }
463 27a70eae Leszek Koltunski
464 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
465
466
  public void releaseResources()
467
    {
468
    mTexture.markForDeletion();
469
    }
470
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
473
  public void apply(Effect effect, int position)
474
    {
475 8cccfb10 Leszek Koltunski
    mEffects.apply(effect, position);
476 a10ada2a Leszek Koltunski
    }
477
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
480
  public void remove(long effectID)
481
    {
482 8cccfb10 Leszek Koltunski
    mEffects.abortById(effectID);
483 a10ada2a Leszek Koltunski
    }
484 74686c71 Leszek Koltunski
485 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
486
487 8bbac3c2 Leszek Koltunski
  public synchronized void solve()
488 a10ada2a Leszek Koltunski
    {
489 98904e45 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
490
      {
491 6b6504fe Leszek Koltunski
      CUBITS[i].solve();
492
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
493 a10ada2a Leszek Koltunski
      }
494
    }
495
496 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
497
498
  public void resetAllTextureMaps()
499
    {
500 eab9d8f8 Leszek Koltunski
    final float ratio = 1.0f/(NUM_TEXTURES+1);
501 f6d06256 Leszek Koltunski
    int color;
502 380162cb Leszek Koltunski
503 ad73edd5 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
504 1f9772f3 Leszek Koltunski
      {
505 8f53e513 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
506 ad73edd5 Leszek Koltunski
507 f6d06256 Leszek Koltunski
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
508 ad73edd5 Leszek Koltunski
        {
509 f6d06256 Leszek Koltunski
        color = getFaceColor(cubit,cubitface,mSize);
510
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
511 ad73edd5 Leszek Koltunski
        }
512
513 8f53e513 Leszek Koltunski
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
514 1f9772f3 Leszek Koltunski
      }
515
    }
516
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
519
  public void setTextureMap(int cubit, int face, int newColor)
520
    {
521 eab9d8f8 Leszek Koltunski
    final float ratio = 1.0f/(NUM_TEXTURES+1);
522 8f53e513 Leszek Koltunski
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
523 1f9772f3 Leszek Koltunski
524 e03e0352 Leszek Koltunski
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
525 8f53e513 Leszek Koltunski
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
526 1f9772f3 Leszek Koltunski
    }
527
528 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
529
530 8bbac3c2 Leszek Koltunski
  public synchronized void beginNewRotation(int axis, int row )
531 a10ada2a Leszek Koltunski
    {
532 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
533
      {
534
      android.util.Log.e("object", "invalid rotation axis: "+axis);
535
      return;
536
      }
537
    if( row<0 || row>=mSize )
538
      {
539
      android.util.Log.e("object", "invalid rotation row: "+row);
540
      return;
541
      }
542
543 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
544
    mRotRowBitmap= (1<<row);
545 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
546 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
547
    mRotationAngle.add(mRotationAngleStatic);
548 9c2f0c91 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
549 27e6c301 Leszek Koltunski
    }
550 a10ada2a Leszek Koltunski
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
553 8bbac3c2 Leszek Koltunski
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
554 27e6c301 Leszek Koltunski
    {
555
    mRotAxis     = axis;
556
    mRotRowBitmap= rowBitmap;
557 a10ada2a Leszek Koltunski
558 27e6c301 Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
559
    mRotationAxis.set( ROTATION_AXIS[axis] );
560
    mRotationAngle.setDuration(durationMillis);
561
    mRotationAngle.resetToBeginning();
562
    mRotationAngle.add(new Static1D(0));
563
    mRotationAngle.add(new Static1D(angle));
564 9c2f0c91 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
565 27e6c301 Leszek Koltunski
    mRotateEffect.notifyWhenFinished(listener);
566
567
    return mRotateEffect.getID();
568
    }
569 a10ada2a Leszek Koltunski
570 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
571 a10ada2a Leszek Koltunski
572 168b6b56 Leszek Koltunski
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
573 27e6c301 Leszek Koltunski
    {
574
    float angle = getAngle();
575
    mRotationAngleStatic.set0(angle);
576
    mRotationAngleFinal.set0(nearestAngleInDegrees);
577
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
578
579
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
580
    mRotationAngle.resetToBeginning();
581
    mRotationAngle.removeAll();
582
    mRotationAngle.add(mRotationAngleStatic);
583
    mRotationAngle.add(mRotationAngleMiddle);
584
    mRotationAngle.add(mRotationAngleFinal);
585
    mRotateEffect.notifyWhenFinished(listener);
586
587
    return mRotateEffect.getID();
588
    }
589 001cc0e4 Leszek Koltunski
590 a10ada2a Leszek Koltunski
591 001cc0e4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
592
593 27e6c301 Leszek Koltunski
  private float getAngle()
594 001cc0e4 Leszek Koltunski
    {
595 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
596 001cc0e4 Leszek Koltunski
597 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
598 001cc0e4 Leszek Koltunski
      {
599 27e6c301 Leszek Koltunski
      return mRotationAngle.getPoint(pointNum-1).get0();
600
      }
601
    else
602
      {
603
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
604
      crashlytics.log("points in RotationAngle: "+pointNum);
605
      return 0;
606 001cc0e4 Leszek Koltunski
      }
607
    }
608
609 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
610
611 8bbac3c2 Leszek Koltunski
  public synchronized void removeRotationNow()
612 168b6b56 Leszek Koltunski
    {
613
    float angle = getAngle();
614
    double nearestAngleInRadians = angle*Math.PI/180;
615
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
616
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
617
    float axisX = ROTATION_AXIS[mRotAxis].get0();
618
    float axisY = ROTATION_AXIS[mRotAxis].get1();
619
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
620
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
621
622
    mRotationAngle.removeAll();
623
    mRotationAngleStatic.set0(0);
624
625
    for(int i=0; i<NUM_CUBITS; i++)
626
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
627
        {
628 6b6504fe Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
629
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
630 168b6b56 Leszek Koltunski
        }
631
    }
632 a10ada2a Leszek Koltunski
633 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
634
635 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
636 aa171dee Leszek Koltunski
    {
637
    solve();
638
    setupPosition(moves);
639
    }
640
641 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
642
643
  public int getCubit(float[] point3D)
644
    {
645 418aa554 Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
646 9621255f Leszek Koltunski
    int currentBest=-1;
647
    float multiplier = returnMultiplier();
648
649
    point3D[0] *= multiplier;
650
    point3D[1] *= multiplier;
651
    point3D[2] *= multiplier;
652
653
    for(int i=0; i<NUM_CUBITS; i++)
654
      {
655 6b6504fe Leszek Koltunski
      dist = CUBITS[i].getDistSquared(point3D);
656 9621255f Leszek Koltunski
      if( dist<minDist )
657
        {
658
        minDist = dist;
659
        currentBest = i;
660
        }
661
      }
662
663
    return currentBest;
664
    }
665
666 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
667
668 168b6b56 Leszek Koltunski
  public int computeNearestAngle(float angle, float speed)
669 0e5ad27c Leszek Koltunski
    {
670
    final int NEAREST = 360/getBasicAngle();
671
672 4c864c68 Leszek Koltunski
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
673
    if( angle< -(NEAREST*0.5) ) tmp-=1;
674 168b6b56 Leszek Koltunski
675 4c864c68 Leszek Koltunski
    if( tmp!=0 ) return NEAREST*tmp;
676 168b6b56 Leszek Koltunski
677 c7b00dfb Leszek Koltunski
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
678 0e5ad27c Leszek Koltunski
    }
679
680 5b893eee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
681
682
  public int getNodeSize()
683
    {
684
    return mNodeSize;
685
    }
686
687 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
688
689 9c2f0c91 Leszek Koltunski
  public ObjectList getObjectList()
690 aa171dee Leszek Koltunski
    {
691
    return mList;
692
    }
693
694 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
695
696 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
697 10a2e360 Leszek Koltunski
  abstract Static3D[] getCubitPositions(int size);
698 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
699 411c6285 Leszek Koltunski
  abstract int getNumFaces();
700 eab9d8f8 Leszek Koltunski
  abstract int getNumStickerTypes();
701 8f53e513 Leszek Koltunski
  abstract int getNumCubitFaces();
702 14bd7976 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit);
703 7289fd6c Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
704 f6d06256 Leszek Koltunski
  abstract int getFaceColor(int cubit, int cubitface, int size);
705 fb377dae Leszek Koltunski
  abstract float returnMultiplier();
706 7c969a6d Leszek Koltunski
  abstract float[] getRowChances();
707 7403cdfa Leszek Koltunski
  abstract float getBasicStep();
708 eaee1ddc Leszek Koltunski
  abstract boolean shouldResetTextureMaps();
709 7c969a6d Leszek Koltunski
710 6b6504fe Leszek Koltunski
  public abstract boolean isSolved();
711 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
712 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
713 fb377dae Leszek Koltunski
  public abstract int computeRowFromOffset(float offset);
714 e46e17fb Leszek Koltunski
  public abstract float returnRotationFactor(float offset);
715 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
716 7c969a6d Leszek Koltunski
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
717
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
718 fdec60a3 Leszek Koltunski
  }