Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 34bc9f31

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