Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 2683f0c4

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 44fec653 Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 b4a6e84d Leszek Koltunski
import java.io.File;
13
import java.io.FileNotFoundException;
14
import java.io.IOException;
15
import java.nio.ByteBuffer;
16
import java.nio.ByteOrder;
17
18 77efd5ad Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
19
import javax.microedition.khronos.opengles.GL10;
20
21 72e386ef Leszek Koltunski
import android.app.Activity;
22 7cb8d4b0 Leszek Koltunski
import android.opengl.GLES31;
23 9530f6b0 Leszek Koltunski
import android.opengl.GLSurfaceView;
24 72e386ef Leszek Koltunski
import android.widget.Toast;
25 9530f6b0 Leszek Koltunski
26 6647b730 Leszek Koltunski
import org.distorted.dialogs.RubikDialogBandagedSave;
27 6dff7924 Leszek Koltunski
import org.distorted.library.effect.EffectType;
28 1a106eb8 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
29 7cb8d4b0 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectBorder;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedFramebuffer;
32 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 da56b12f Leszek Koltunski
import org.distorted.library.main.DistortedNode;
34 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
35 20b60ad9 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
36 6dff7924 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
37 7cb8d4b0 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 77efd5ad Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 72e386ef Leszek Koltunski
import org.distorted.objectlib.json.JsonWriter;
41 a0d89fd1 Leszek Koltunski
import org.distorted.objectlib.main.InitData;
42 7cb8d4b0 Leszek Koltunski
import org.distorted.objectlib.main.ShapeHexahedron;
43 72e386ef Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
44 b4a6e84d Leszek Koltunski
import org.distorted.objectlib.objects.TwistyBandagedCuboid;
45 72e386ef Leszek Koltunski
import org.json.JSONException;
46
47 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
50
{
51 7cb8d4b0 Leszek Koltunski
   public static final float BRIGHTNESS = 0.333f;
52 213c15de Leszek Koltunski
   private static final int RESET_DURATION = 1000;
53 f6bf4e77 Leszek Koltunski
   private static final float MAX_SIZE_CHANGE = 1.70f;
54
   private static final float MIN_SIZE_CHANGE = 0.50f;
55
   private static final float INIT_RATIO = 0.5f;
56 9530f6b0 Leszek Koltunski
57 75173f81 Leszek Koltunski
   private final BandagedCreatorView mView;
58
   private final DistortedScreen mScreen;
59
   private final Static3D mScale;
60 50ec342b Leszek Koltunski
   private final Static4D mQuatT, mQuatA;
61 09b76d08 Leszek Koltunski
   private final int[] mObjSize;
62 75173f81 Leszek Koltunski
63 5d5ed376 Leszek Koltunski
   private BandagedCubit[] mCubits;
64 e0b71e6e Leszek Koltunski
   private boolean mInitialPhase;
65
   private long mStartTime;
66 b9d062cf Leszek Koltunski
   private float mQuatX, mQuatY, mQuatZ, mQuatW;
67 826abd80 Leszek Koltunski
   private int mX, mY, mZ, mNumCubits;
68 f6bf4e77 Leszek Koltunski
   private boolean mResetQuats, mSetQuatT, mResettingObject, mConnectingCubits, mCreatingCubits, mRescaling;
69 1d4592a2 Leszek Koltunski
   private int mIndex1, mIndex2;
70 7cb8d4b0 Leszek Koltunski
   private int mSaveIcon;
71
   private DistortedFramebuffer mFramebuffer;
72
   private String mPath;
73 b9d062cf Leszek Koltunski
   private boolean mCubitsCreated;
74 a8aa49dc Leszek Koltunski
   private int mWidth, mHeight;
75 f6bf4e77 Leszek Koltunski
   private float mScaleValue, mObjectScreenRatio;
76 da56b12f Leszek Koltunski
77 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
   BandagedCreatorRenderer(BandagedCreatorView v)
80
     {
81 50ec342b Leszek Koltunski
     mQuatT = new Static4D(0,0,0,1);
82
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
83 da56b12f Leszek Koltunski
84 9530f6b0 Leszek Koltunski
     mView = v;
85 77efd5ad Leszek Koltunski
86 1d4592a2 Leszek Koltunski
     mResetQuats       = false;
87
     mSetQuatT         = false;
88
     mResettingObject  = false;
89
     mConnectingCubits = false;
90 b9d062cf Leszek Koltunski
     mCubitsCreated    = false;
91 227e8c1f Leszek Koltunski
     mCreatingCubits   = false;
92 f6bf4e77 Leszek Koltunski
     mRescaling        = false;
93 75173f81 Leszek Koltunski
94 f6bf4e77 Leszek Koltunski
     mObjectScreenRatio= INIT_RATIO;
95 7cb8d4b0 Leszek Koltunski
     mSaveIcon = -1;
96 09b76d08 Leszek Koltunski
     mObjSize  = new int[3];
97 7cb8d4b0 Leszek Koltunski
98 9530f6b0 Leszek Koltunski
     mScreen = new DistortedScreen();
99
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
100 550db260 Leszek Koltunski
     mScale = new Static3D(1,1,1);
101 da56b12f Leszek Koltunski
     }
102
103 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
   private boolean isAdjacent(float[] pos1, float[] pos2)
106
     {
107
     int len1 = pos1.length/3;
108
     int len2 = pos2.length/3;
109
110
     for(int i=0; i<len1; i++)
111
       for(int j=0; j<len2; j++)
112
         {
113
         float d0 = pos1[3*i  ] - pos2[3*j  ];
114
         float d1 = pos1[3*i+1] - pos2[3*j+1];
115
         float d2 = pos1[3*i+2] - pos2[3*j+2];
116
117
         if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true;
118
         }
119
120
     return false;
121
     }
122
123 826abd80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
   private int computeNumCubits(int x, int y, int z)
126
     {
127
     return ( x<=1 || y<=1 || z<=1 ) ? x*y*z : x*y*z-(x-2)*(y-2)*(z-2);
128
     }
129
130 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131 a76cc4f4 Leszek Koltunski
132 b9d062cf Leszek Koltunski
   private void createCubits()
133 da56b12f Leszek Koltunski
     {
134 826abd80 Leszek Koltunski
     mCubits = new BandagedCubit[mNumCubits];
135
     int c=0;
136 da56b12f Leszek Koltunski
137 8d10f7b8 Leszek Koltunski
     float begX = 0.5f*(1-mX);
138
     float begY = 0.5f*(1-mY);
139
     float begZ = 0.5f*(1-mZ);
140
141 826abd80 Leszek Koltunski
     for(int x=0; x<mX; x++)
142
       for(int y=0; y<mY; y++)
143
          for(int z=0; z<mZ; z++)
144
            if( x==0 || x==mX-1 || y==0 || y==mY-1 || z==0 || z==mZ-1 )
145
              {
146 8d10f7b8 Leszek Koltunski
              float[] pos = new float[] { begX+x,begY+y,begZ+z };
147 826abd80 Leszek Koltunski
              mCubits[c] = new BandagedCubit(pos,mX,mY,mZ,mQuatT,mQuatA,mScale,false);
148
              c++;
149
              }
150 da56b12f Leszek Koltunski
151 213c15de Leszek Koltunski
     mView.setCubits(mCubits,mX,mY,mZ);
152 9530f6b0 Leszek Koltunski
     }
153 a76cc4f4 Leszek Koltunski
154 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
   private void resetObject()
157
     {
158
     mView.resetCubits();
159
160 826abd80 Leszek Koltunski
     for(int c=0; c<mNumCubits; c++)
161 e0b71e6e Leszek Koltunski
       {
162
       if( !mCubits[c].isAttached() )
163
         {
164
         mCubits[c].attach();
165 1a106eb8 Leszek Koltunski
         mCubits[c].setUnmarked();
166 e0b71e6e Leszek Koltunski
         mScreen.attach(mCubits[c].getNode());
167
         }
168
       if( mCubits[c].getPosition().length>3 )
169
         {
170
         mCubits[c].reset(mScaleValue);
171
         }
172
       }
173
     }
174
175 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
   @Override
178
   public void onDrawFrame(GL10 glUnused)
179
     {
180
     long time = System.currentTimeMillis();
181
     mScreen.render(time);
182 75173f81 Leszek Koltunski
183 50ec342b Leszek Koltunski
     if( mSetQuatT )
184 75173f81 Leszek Koltunski
       {
185 50ec342b Leszek Koltunski
       mSetQuatT = false;
186 b9d062cf Leszek Koltunski
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
187 75173f81 Leszek Koltunski
       }
188
189
     if( mResetQuats )
190
       {
191
       mResetQuats = false;
192
193 50ec342b Leszek Koltunski
       float qx = mQuatT.get0();
194
       float qy = mQuatT.get1();
195
       float qz = mQuatT.get2();
196
       float qw = mQuatT.get3();
197 75173f81 Leszek Koltunski
198 50ec342b Leszek Koltunski
       float rx = mQuatA.get0();
199
       float ry = mQuatA.get1();
200
       float rz = mQuatA.get2();
201
       float rw = mQuatA.get3();
202 75173f81 Leszek Koltunski
203
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
204
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
205
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
206
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
207
208 50ec342b Leszek Koltunski
       mQuatT.set(0f, 0f, 0f, 1f);
209
       mQuatA.set(tx, ty, tz, tw);
210 75173f81 Leszek Koltunski
       }
211 e0b71e6e Leszek Koltunski
212
     if( mResettingObject )
213
       {
214
       boolean done = continueResetting(time);
215
       if( done ) mResettingObject = false;
216
       }
217 7cb8d4b0 Leszek Koltunski
218 eb6bccbd Leszek Koltunski
     if( mSaveIcon>=0 )
219
       {
220 f203ffa0 Leszek Koltunski
       renderIcon(time); // for some reason we need to call render() twice here, otherwise the
221
       mSaveIcon++;      // icon turns out black. Probably some problem with binding the texture.
222 eb6bccbd Leszek Koltunski
       }
223
     if( mSaveIcon>=2 )
224
       {
225
       saveIcon();
226
       mSaveIcon = -1;
227
       }
228 1d4592a2 Leszek Koltunski
229
     if( mConnectingCubits )
230
       {
231
       mConnectingCubits = false;
232
       tryConnectingCubits(mIndex1,mIndex2);
233
       }
234 227e8c1f Leszek Koltunski
235
     if( mCreatingCubits )
236
       {
237
       mCreatingCubits = false;
238
239
       rescaleObject();
240
241
       if( mCubitsCreated )
242
         {
243
         createCubits();
244
245
         mScreen.detachAll();
246
         mView.resetCubits();
247
248
         for(int i=0; i<mNumCubits; i++)
249
           {
250
           mCubits[i].scaleMove(mScaleValue);
251
           DistortedNode node = mCubits[i].getNode();
252
           mScreen.attach(node);
253
           }
254
         }
255
       }
256 f6bf4e77 Leszek Koltunski
257
     if( mRescaling )
258
       {
259
       mRescaling = false;
260
       rescaleObject();
261
       for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(mScaleValue);
262
       BandagedCreatorTouchControl control = mView.getTouchControl();
263
       control.setObjectRatio(mObjectScreenRatio);
264
       }
265 9530f6b0 Leszek Koltunski
     }
266
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269
   @Override
270
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
271
      {
272 3b8f5220 Leszek Koltunski
      if( width!=mWidth || height!=mHeight )
273
        {
274
        mWidth = width;
275
        mHeight= height;
276
        rescaleObject();
277
278
        mScreen.detachAll();
279
        int touched = mView.getTouched();
280
281
        for(int i=0; i<mNumCubits; i++)
282
          if( mCubits[i].isAttached() )
283
            {
284
            mCubits[i].scaleMove(mScaleValue);
285
            if( touched==i ) mCubits[i].setMarked();
286
            else             mCubits[i].setUnmarked();
287
            DistortedNode node = mCubits[i].getNode();
288
            mScreen.attach(node);
289
            }
290
291
        mView.setScreenSize(width,height);
292
        mScreen.resize(width,height);
293
        }
294 9530f6b0 Leszek Koltunski
      }
295
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
   @Override
299
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
300
      {
301 7d8ae8db Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,0);
302 213c15de Leszek Koltunski
      MeshBase.setMaxEffComponents(120);
303 6dff7924 Leszek Koltunski
304 1a106eb8 Leszek Koltunski
      FragmentEffectBrightness.enable();
305 b3d6aff5 Leszek Koltunski
306 9530f6b0 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
307 6dff7924 Leszek Koltunski
      DistortedLibrary.setCull(true);
308 5d5ed376 Leszek Koltunski
309 3b8f5220 Leszek Koltunski
      if( mCubits==null )
310
        {
311
        createCubits();
312
        }
313
      else
314
        {
315
        for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap();
316
        }
317
318 b9d062cf Leszek Koltunski
      mCubitsCreated = true;
319 3b8f5220 Leszek Koltunski
      mWidth = 0;
320
      mHeight= 0;
321 9530f6b0 Leszek Koltunski
      }
322
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
325
   public void distortedException(Exception ex)
326
     {
327
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
328
     }
329 550db260 Leszek Koltunski
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332
   public BandagedCubit[] getCubits()
333
     {
334
     return mCubits;
335
     }
336
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
339
   public DistortedScreen getScreen()
340
     {
341
     return mScreen;
342
     }
343
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
346 1d4592a2 Leszek Koltunski
   void setConnecting(int index1, int index2)
347
     {
348
     mIndex1 = index1;
349
     mIndex2 = index2;
350
     mConnectingCubits = true;
351
     }
352
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355
   private void tryConnectingCubits(int index1, int index2)
356 550db260 Leszek Koltunski
     {
357
     if( index1!=index2 )
358
       {
359
       float[] pos1 = mCubits[index1].getPosition();
360
       float[] pos2 = mCubits[index2].getPosition();
361
362
       if( isAdjacent(pos1,pos2) )
363
         {
364 28cb1607 Leszek Koltunski
         mCubits[index2].join(pos1,mScaleValue);
365 550db260 Leszek Koltunski
         mCubits[index1].detach();
366 28cb1607 Leszek Koltunski
         mScreen.detach(mCubits[index1].getNode());
367 550db260 Leszek Koltunski
         }
368
       }
369
     }
370 75173f81 Leszek Koltunski
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373 50ec342b Leszek Koltunski
   public Static4D getQuatAccu()
374
     {
375
     return mQuatA;
376
     }
377 75173f81 Leszek Koltunski
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380 50ec342b Leszek Koltunski
   public void setQuatTemp(float x, float y, float z, float w)
381
     {
382 2683f0c4 Leszek Koltunski
     mSetQuatT = false;
383
384 b9d062cf Leszek Koltunski
     mQuatX = x;
385
     mQuatY = y;
386
     mQuatZ = z;
387
     mQuatW = w;
388 75173f81 Leszek Koltunski
389 50ec342b Leszek Koltunski
     mSetQuatT = true;
390
     }
391 75173f81 Leszek Koltunski
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
394 50ec342b Leszek Koltunski
   public void resetQuats()
395
     {
396
     mResetQuats = true;
397
     }
398
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
401 e0b71e6e Leszek Koltunski
   public boolean isBusy()
402 50ec342b Leszek Koltunski
     {
403 e0b71e6e Leszek Koltunski
     return mResettingObject;
404
     }
405
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408
   public void saveObject()
409 72e386ef Leszek Koltunski
     {
410
     int numAttached=0;
411
412 826abd80 Leszek Koltunski
     for(int i=0; i<mNumCubits; i++)
413 72e386ef Leszek Koltunski
       if( mCubits[i].isAttached() ) numAttached++;
414
415
     float[][] pos = new float[numAttached][];
416
     int attached=0;
417
418 826abd80 Leszek Koltunski
     for(int i=0; i<mNumCubits; i++)
419 72e386ef Leszek Koltunski
       if( mCubits[i].isAttached() )
420
         {
421
         pos[attached++] = mCubits[i].getPosition();
422
         }
423
424 a0d89fd1 Leszek Koltunski
     InitData data = new InitData(mObjSize,pos);
425 b4a6e84d Leszek Koltunski
     TwistyObject obj = new TwistyBandagedCuboid( data, TwistyObject.MESH_NICE, TwistyObject.MODE_NORM,
426
                                                  new Static4D(0,0,0,1), new Static3D(0,0,0), 1.0f, null );
427 bfb59352 Leszek Koltunski
     String name = obj.getShortName();
428 72e386ef Leszek Koltunski
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
429
430 bfb59352 Leszek Koltunski
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
431 e48ad1af Leszek Koltunski
       {
432 a0d89fd1 Leszek Koltunski
       setupIconCreation(act,data);
433 e48ad1af Leszek Koltunski
       act.addObject(obj.getShortName());
434
       }
435 72e386ef Leszek Koltunski
     }
436
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438
439 e48ad1af Leszek Koltunski
   private boolean createObjectJson(TwistyObject object, Activity act)
440 72e386ef Leszek Koltunski
     {
441
     final String name = object.getShortName()+"_object.json";
442
     File file = new File(act.getFilesDir(), name);
443
     String filename = file.getAbsolutePath();
444
445
     try
446
       {
447
       JsonWriter writer = JsonWriter.getInstance();
448
       String json = writer.createObjectString(object,24);
449
       writer.write(filename,json);
450 e48ad1af Leszek Koltunski
       return true;
451 72e386ef Leszek Koltunski
       }
452
     catch(JSONException ex)
453
       {
454
       act.runOnUiThread(new Runnable()
455
         {
456
         public void run()
457
           {
458
           String message = "JSON Exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
459
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
460
           }
461
         });
462 e48ad1af Leszek Koltunski
463
       return false;
464 72e386ef Leszek Koltunski
       }
465
     catch(FileNotFoundException ex)
466
       {
467
       act.runOnUiThread(new Runnable()
468
         {
469
         public void run()
470
           {
471
           String message = "FileNotFound exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
472
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
473
           }
474
         });
475 e48ad1af Leszek Koltunski
476
       return false;
477 72e386ef Leszek Koltunski
       }
478
     catch(IOException ex)
479
       {
480
       act.runOnUiThread(new Runnable()
481
         {
482
         public void run()
483
           {
484
           String message = "IO exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
485
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
486
           }
487
         });
488 e48ad1af Leszek Koltunski
489
       return false;
490 72e386ef Leszek Koltunski
       }
491
     }
492
493 2d88fcc3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
494
495
   private int computeProjectionAngle()
496
     {
497
     float quot1 = mObjSize[2]/ (float)mObjSize[0];
498
     float quot2 = mObjSize[2]/ (float)mObjSize[1];
499
     float quot3 = mObjSize[0]/ (float)mObjSize[2];
500
     float quot4 = mObjSize[0]/ (float)mObjSize[1];
501
502
     float quot5 = Math.max(quot1,quot2);
503
     float quot6 = Math.max(quot3,quot4);
504
     float quot7 = Math.max(quot5,quot6);
505
506
          if( quot7<=1.0f ) return 120;
507
     else if( quot7<=1.5f ) return 90;
508
     else if( quot7<=2.0f ) return 60;
509
     else                   return 30;
510
     }
511
512 72e386ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
513 7cb8d4b0 Leszek Koltunski
514 a0d89fd1 Leszek Koltunski
   private void setupIconCreation(Activity act, InitData data)
515 72e386ef Leszek Koltunski
     {
516 f203ffa0 Leszek Koltunski
     final float R=1.0f;
517 ba52835a Leszek Koltunski
     final int FBO_WIDTH  = (int)(R*720);
518
     final int FBO_HEIGHT = (int)(R*1280);
519
     final float OBJECT_SIZE = R*0.35f;
520 f203ffa0 Leszek Koltunski
521 b4a6e84d Leszek Koltunski
     TwistyObject obj = new TwistyBandagedCuboid( data, TwistyObject.MESH_NICE, TwistyObject.MODE_ICON,
522
                                                  ShapeHexahedron.DEFAULT_ROT, new Static3D(0,0,0), OBJECT_SIZE, null );
523 16663e27 Leszek Koltunski
524
     DistortedEffects effects = obj.getObjectEffects();
525
     DistortedNode node = obj.getNode();
526 eb6bccbd Leszek Koltunski
527 20b60ad9 Leszek Koltunski
     if( mFramebuffer==null )
528
       {
529 f203ffa0 Leszek Koltunski
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
530
       mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
531 20b60ad9 Leszek Koltunski
       }
532 7cb8d4b0 Leszek Koltunski
533 2d88fcc3 Leszek Koltunski
     mFramebuffer.setProjection( computeProjectionAngle() ,0.1f);
534 20b60ad9 Leszek Koltunski
     mFramebuffer.detachAll();
535
     mFramebuffer.attach(node);
536 7cb8d4b0 Leszek Koltunski
537
     Static1D halo = new Static1D(5);
538
     Static4D color = new Static4D(0,0,0,1);
539
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
540
     border.setHaloDepth(false);
541
     effects.apply(border);
542
543 16663e27 Leszek Koltunski
     final String name = obj.getShortName()+".png";
544 7cb8d4b0 Leszek Koltunski
     File file = new File(act.getFilesDir(), name);
545
     String filename = file.getAbsolutePath();
546
547
     mSaveIcon = 0;
548
     mPath = filename;
549 72e386ef Leszek Koltunski
     }
550 7cb8d4b0 Leszek Koltunski
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
553 eb6bccbd Leszek Koltunski
   private void renderIcon(long time)
554
     {
555
     mFramebuffer.render(time);
556
     }
557 7cb8d4b0 Leszek Koltunski
558 eb6bccbd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
559 7cb8d4b0 Leszek Koltunski
560 eb6bccbd Leszek Koltunski
   private void saveIcon()
561
     {
562
     int fW = mFramebuffer.getWidth();
563
     int fH = mFramebuffer.getHeight();
564 7cb8d4b0 Leszek Koltunski
565 eb6bccbd Leszek Koltunski
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
566
     buf.order(ByteOrder.LITTLE_ENDIAN);
567
568
     mFramebuffer.setAsReadFramebuffer(0);
569
     GLES31.glReadBuffer(GLES31.GL_COLOR_ATTACHMENT0);
570
     GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
571
     BandagedCreatorWorkerThread.newBuffer(buf,fW,fH,6,mPath);
572
     GLES31.glBindFramebuffer(GLES31.GL_READ_FRAMEBUFFER, 0);
573
574
     mSaveIcon = -1;
575
     }
576 7cb8d4b0 Leszek Koltunski
577 f6bf4e77 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
578
579
   void mulObjectRatio(float ratio)
580
     {
581
     mObjectScreenRatio *= ratio;
582
583
     if( mObjectScreenRatio>MAX_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MAX_SIZE_CHANGE*INIT_RATIO;
584
     if( mObjectScreenRatio<MIN_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MIN_SIZE_CHANGE*INIT_RATIO;
585
586
     mRescaling = true;
587
     }
588
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590
591
   float getObjectRatio()
592
     {
593
     return mObjectScreenRatio;
594
     }
595
596 a8aa49dc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
597
598
   private void rescaleObject()
599
     {
600
     final int size = mX>mY ? Math.max(mX, mZ) : Math.max(mY, mZ);
601 f6bf4e77 Leszek Koltunski
     final float Q = mObjectScreenRatio/size;
602 a8aa49dc Leszek Koltunski
     mScaleValue = mWidth<mHeight ? Q*mWidth : Q*mHeight;
603
     mScale.set( mScaleValue,mScaleValue,mScaleValue );
604
     }
605
606 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
607
608
   public void changeObject(int x, int y, int z)
609
     {
610
     if( mX!=x || mY!=y || mZ!=z )
611
       {
612
       mX = x;
613
       mY = y;
614
       mZ = z;
615 a8aa49dc Leszek Koltunski
616 09b76d08 Leszek Koltunski
       mObjSize[0] = mX;
617
       mObjSize[1] = mY;
618
       mObjSize[2] = mZ;
619
620 826abd80 Leszek Koltunski
       mNumCubits = computeNumCubits(mX,mY,mZ);
621 b9d062cf Leszek Koltunski
622 227e8c1f Leszek Koltunski
       mCreatingCubits = true;
623 b9d062cf Leszek Koltunski
       }
624
     }
625
626 72e386ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
627
628
   public void displaySavingDialog()
629 e0b71e6e Leszek Koltunski
     {
630 61a7b812 Leszek Koltunski
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
631 6647b730 Leszek Koltunski
     RubikDialogBandagedSave saveDiag = new RubikDialogBandagedSave();
632 61a7b812 Leszek Koltunski
     saveDiag.show(act.getSupportFragmentManager(), null);
633 e0b71e6e Leszek Koltunski
     }
634
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636
637
   public void setupReset()
638
     {
639
     mResettingObject = true;
640
     mInitialPhase    = true;
641
     mStartTime       = System.currentTimeMillis();
642
     }
643
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645
646
   public boolean continueResetting(long time)
647
     {
648
     long diff = time-mStartTime;
649 213c15de Leszek Koltunski
     float quotient = ((float)diff)/RESET_DURATION;
650 e0b71e6e Leszek Koltunski
651
     if( mInitialPhase && quotient>0.5f )
652
       {
653
       mInitialPhase=false;
654
       resetObject();
655
       }
656
657
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
658
659
     float sinA = (float)Math.sin(angle);
660
     float cosA = (float)Math.cos(angle);
661
662
     mQuatT.set(0, -sinA, 0, cosA);
663 50ec342b Leszek Koltunski
664 e0b71e6e Leszek Koltunski
     return quotient>1.0f;
665 50ec342b Leszek Koltunski
     }
666 6f3af598 Leszek Koltunski
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
669
  public void touchCubit(int index)
670
    {
671 c6c09ab5 Leszek Koltunski
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
672 6f3af598 Leszek Koltunski
    }
673
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
676
  public void untouchCubit(int index)
677
    {
678 c6c09ab5 Leszek Koltunski
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked();
679 6f3af598 Leszek Koltunski
    }
680 9530f6b0 Leszek Koltunski
}