Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 661cdbac

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