Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 826abd80

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