Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 13a3dfa9

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import android.app.Activity;
26
import android.opengl.GLES31;
27
import android.opengl.GLSurfaceView;
28
import android.widget.Toast;
29

    
30
import org.distorted.dialogs.RubikDialogSaveBandaged;
31
import org.distorted.library.effect.PostprocessEffectBorder;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedFramebuffer;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedNode;
36
import org.distorted.library.main.DistortedScreen;
37

    
38
import org.distorted.library.main.InternalOutputSurface;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
import org.distorted.objectlib.json.JsonWriter;
43
import org.distorted.objectlib.main.ShapeHexahedron;
44
import org.distorted.objectlib.main.TwistyObject;
45
import org.distorted.objectlib.objects.TwistyBandagedGeneric;
46
import org.json.JSONException;
47

    
48
import java.io.File;
49
import java.io.FileNotFoundException;
50
import java.io.IOException;
51
import java.nio.ByteBuffer;
52
import java.nio.ByteOrder;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
57
{
58
   public static final float BRIGHTNESS = 0.333f;
59
   private static final int DURATION = 1000;
60

    
61
   static final int COLOR_DEFAULT = 0xffffff55;
62
   static final int COLOR_MARKED  = 0xffff0000;
63

    
64
   static final float SCREEN_RATIO = 0.5f;
65
   static final float OBJECT_SIZE  = 3.0f;
66

    
67
   private final float[][] POSITIONS = new float[][]
68
        {
69
          {-1,  1,  1},
70
          {-1,  1,  0},
71
          {-1,  1, -1},
72
          {-1,  0,  1},
73
          {-1,  0,  0},
74
          {-1,  0, -1},
75
          {-1, -1,  1},
76
          {-1, -1,  0},
77
          {-1, -1, -1},
78
          { 0, -1,  1},
79
          { 0, -1,  0},
80
          { 0,  1,  1},
81
          { 0,  1,  0},
82
          { 0,  1, -1},
83
          { 0,  0,  1},
84
          { 0,  0, -1},
85
          { 1,  1,  1},
86
          { 1,  1,  0},
87
          { 1,  1, -1},
88
          { 1,  0,  1},
89
          { 1,  0,  0},
90
          { 1, -1,  1},
91
          { 1,  0, -1},
92
          { 1, -1, -1},
93
          { 1, -1,  0},
94
          { 0, -1, -1},
95
        };
96

    
97
   private final BandagedCreatorView mView;
98
   private final DistortedScreen mScreen;
99
   private final Static3D mScale;
100
   private final BandagedCubit[] mCubits;
101
   private final Static4D mQuatT, mQuatA;
102

    
103
   private boolean mInitialPhase;
104
   private long mStartTime;
105
   private float mScaleValue;
106
   private float mX, mY, mZ, mW;
107
   private boolean mResetQuats, mSetQuatT, mResettingObject;
108
   private int mSaveIcon;
109
   private DistortedFramebuffer mFramebuffer;
110
   private String mPath;
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
   BandagedCreatorRenderer(BandagedCreatorView v)
115
     {
116
     mQuatT = new Static4D(0,0,0,1);
117
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
118

    
119
     mView = v;
120

    
121
     mResetQuats     = false;
122
     mSetQuatT       = false;
123
     mResettingObject= false;
124

    
125
     mSaveIcon = -1;
126

    
127
     mScreen = new DistortedScreen();
128
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
129
     mScale = new Static3D(1,1,1);
130
     mCubits= createCubits();
131
     }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   private boolean isAdjacent(float[] pos1, float[] pos2)
136
     {
137
     int len1 = pos1.length/3;
138
     int len2 = pos2.length/3;
139

    
140
     for(int i=0; i<len1; i++)
141
       for(int j=0; j<len2; j++)
142
         {
143
         float d0 = pos1[3*i  ] - pos2[3*j  ];
144
         float d1 = pos1[3*i+1] - pos2[3*j+1];
145
         float d2 = pos1[3*i+2] - pos2[3*j+2];
146

    
147
         if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true;
148
         }
149

    
150
     return false;
151
     }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
   private BandagedCubit[] createCubits()
156
     {
157
     int len = POSITIONS.length;
158
     BandagedCubit[] cubits = new BandagedCubit[len];
159

    
160
     for(int c=0; c<len; c++)
161
       {
162
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuatT,mQuatA,mScale,COLOR_DEFAULT);
163
       }
164

    
165
     return cubits;
166
     }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
   private void resetObject()
171
     {
172
     mView.resetCubits();
173

    
174
     int len = POSITIONS.length;
175

    
176
     for(int c=0; c<len; c++)
177
       {
178
       if( !mCubits[c].isAttached() )
179
         {
180
         mCubits[c].attach();
181
         mScreen.attach(mCubits[c].getNode());
182
         }
183
       if( mCubits[c].getPosition().length>3 )
184
         {
185
         mCubits[c].reset(mScaleValue);
186
         }
187
       }
188
     }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
   @Override
193
   public void onDrawFrame(GL10 glUnused)
194
     {
195
     long time = System.currentTimeMillis();
196
     mScreen.render(time);
197

    
198
     if( mSetQuatT )
199
       {
200
       mSetQuatT = false;
201
       mQuatT.set(mX,mY,mZ,mW);
202
       }
203

    
204
     if( mResetQuats )
205
       {
206
       mResetQuats = false;
207

    
208
       float qx = mQuatT.get0();
209
       float qy = mQuatT.get1();
210
       float qz = mQuatT.get2();
211
       float qw = mQuatT.get3();
212

    
213
       float rx = mQuatA.get0();
214
       float ry = mQuatA.get1();
215
       float rz = mQuatA.get2();
216
       float rw = mQuatA.get3();
217

    
218
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
219
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
220
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
221
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
222

    
223
       mQuatT.set(0f, 0f, 0f, 1f);
224
       mQuatA.set(tx, ty, tz, tw);
225
       }
226

    
227
     if( mResettingObject )
228
       {
229
       boolean done = continueResetting(time);
230
       if( done ) mResettingObject = false;
231
       }
232

    
233
     if( mSaveIcon>=0 )
234
       {
235
       renderIcon(time); // for some reason we need to call render() twice here, otherwise the
236
       mSaveIcon++;      // icon turns out black. Probably some problem with binding the texture.
237
       }
238
     if( mSaveIcon>=2 )
239
       {
240
       saveIcon();
241
       mSaveIcon = -1;
242
       }
243
     }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
   @Override
248
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
249
      {
250
      final float Q = SCREEN_RATIO/OBJECT_SIZE;
251
      mScaleValue = width<height ? Q*width : Q*height;
252

    
253
      mScreen.detachAll();
254
      int len = POSITIONS.length;
255
      int touched = mView.getTouched();
256

    
257
      for(int i=0; i<len; i++)
258
        if( mCubits[i].isAttached() )
259
          {
260
          mCubits[i].scaleMove(mScaleValue);
261
          mCubits[i].setTexture( touched==i ? COLOR_MARKED : COLOR_DEFAULT);
262
          DistortedNode node = mCubits[i].getNode();
263
          mScreen.attach(node);
264
          }
265

    
266
      mScale.set( mScaleValue,mScaleValue,mScaleValue );
267
      mView.setScreenSize(width,height);
268
      mScreen.resize(width,height);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
   @Override
274
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
275
      {
276
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
   public void distortedException(Exception ex)
282
     {
283
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
284
     }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
   public BandagedCubit[] getCubits()
289
     {
290
     return mCubits;
291
     }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
   public DistortedScreen getScreen()
296
     {
297
     return mScreen;
298
     }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
   public void tryConnectingCubits(int index1, int index2)
303
     {
304
     if( index1!=index2 )
305
       {
306
       float[] pos1 = mCubits[index1].getPosition();
307
       float[] pos2 = mCubits[index2].getPosition();
308

    
309
       if( isAdjacent(pos1,pos2) )
310
         {
311
         mCubits[index2].join(pos1,mScaleValue);
312
         mCubits[index1].detach();
313
         mScreen.detach(mCubits[index1].getNode());
314
         }
315
       }
316
     }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
   public Static4D getQuatAccu()
321
     {
322
     return mQuatA;
323
     }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
   public void setQuatTemp(float x, float y, float z, float w)
328
     {
329
     mX = x;
330
     mY = y;
331
     mZ = z;
332
     mW = w;
333

    
334
     mSetQuatT = true;
335
     }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
   public void resetQuats()
340
     {
341
     mResetQuats = true;
342
     }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
   public boolean isBusy()
347
     {
348
     return mResettingObject;
349
     }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
   public void saveObject()
354
     {
355
     int len = POSITIONS.length;
356
     int numAttached=0;
357

    
358
     for(int i=0; i<len; i++)
359
       if( mCubits[i].isAttached() ) numAttached++;
360

    
361
     float[][] pos = new float[numAttached][];
362
     int attached=0;
363

    
364
     for(int i=0; i<len; i++)
365
       if( mCubits[i].isAttached() )
366
         {
367
         pos[attached++] = mCubits[i].getPosition();
368
         }
369

    
370
     TwistyBandagedGeneric.setPositions(pos);
371
     TwistyObject obj = new TwistyBandagedGeneric( new Static4D(0,0,0,1), 1.0f, TwistyObject.MODE_NORM);
372
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
373

    
374
     boolean success = createObjectJson(obj,act);
375
     setupIconCreation(act);
376

    
377
     if( success )
378
       {
379
       act.addObject(obj.getShortName());
380
       }
381
     }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
   private boolean createObjectJson(TwistyObject object, Activity act)
386
     {
387
     final String name = object.getShortName()+"_object.json";
388
     File file = new File(act.getFilesDir(), name);
389
     String filename = file.getAbsolutePath();
390

    
391
     try
392
       {
393
       JsonWriter writer = JsonWriter.getInstance();
394
       String json = writer.createObjectString(object,24);
395
       writer.write(filename,json);
396
       return true;
397
       }
398
     catch(JSONException ex)
399
       {
400
       act.runOnUiThread(new Runnable()
401
         {
402
         public void run()
403
           {
404
           String message = "JSON Exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
405
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
406
           }
407
         });
408

    
409
       return false;
410
       }
411
     catch(FileNotFoundException ex)
412
       {
413
       act.runOnUiThread(new Runnable()
414
         {
415
         public void run()
416
           {
417
           String message = "FileNotFound exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
418
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
419
           }
420
         });
421

    
422
       return false;
423
       }
424
     catch(IOException ex)
425
       {
426
       act.runOnUiThread(new Runnable()
427
         {
428
         public void run()
429
           {
430
           String message = "IO exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
431
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
432
           }
433
         });
434

    
435
       return false;
436
       }
437
     }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
   private void setupIconCreation(Activity act)
442
     {
443
     final float R=1.0f;
444
     final int FBO_WIDTH  = (int)(R*240);
445
     final int FBO_HEIGHT = (int)(R*360);
446
     final float OBJECT_SIZE = R*0.38f;
447

    
448
     TwistyObject object = new TwistyBandagedGeneric(ShapeHexahedron.DEFAULT_ROT, OBJECT_SIZE, TwistyObject.MODE_ICON);
449
     DistortedEffects effects = object.getObjectEffects();
450
     DistortedNode node = object.getNode();
451

    
452
     if( mFramebuffer==null )
453
       {
454
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
455
       mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
456
       }
457

    
458
     mFramebuffer.detachAll();
459
     mFramebuffer.attach(node);
460

    
461
     Static1D halo = new Static1D(5);
462
     Static4D color = new Static4D(0,0,0,1);
463
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
464
     border.setHaloDepth(false);
465
     effects.apply(border);
466

    
467
     final String name = object.getShortName()+".png";
468
     File file = new File(act.getFilesDir(), name);
469
     String filename = file.getAbsolutePath();
470

    
471
     mSaveIcon = 0;
472
     mPath = filename;
473
     }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
   private void renderIcon(long time)
478
     {
479
     mFramebuffer.render(time);
480
     }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
   private void saveIcon()
485
     {
486
     int fW = mFramebuffer.getWidth();
487
     int fH = mFramebuffer.getHeight();
488

    
489
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
490
     buf.order(ByteOrder.LITTLE_ENDIAN);
491

    
492
     mFramebuffer.setAsReadFramebuffer(0);
493
     GLES31.glReadBuffer(GLES31.GL_COLOR_ATTACHMENT0);
494
     GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
495
     BandagedCreatorWorkerThread.newBuffer(buf,fW,fH,6,mPath);
496
     GLES31.glBindFramebuffer(GLES31.GL_READ_FRAMEBUFFER, 0);
497

    
498
     mSaveIcon = -1;
499
     }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
   public void displaySavingDialog()
504
     {
505
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
506
     RubikDialogSaveBandaged saveDiag = new RubikDialogSaveBandaged();
507
     saveDiag.show(act.getSupportFragmentManager(), null);
508
     }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
   public void setupReset()
513
     {
514
     mResettingObject = true;
515
     mInitialPhase    = true;
516
     mStartTime       = System.currentTimeMillis();
517
     }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
   public boolean continueResetting(long time)
522
     {
523
     long diff = time-mStartTime;
524
     float quotient = ((float)diff)/DURATION;
525

    
526
     if( mInitialPhase && quotient>0.5f )
527
       {
528
       mInitialPhase=false;
529
       resetObject();
530
       }
531

    
532
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
533

    
534
     float sinA = (float)Math.sin(angle);
535
     float cosA = (float)Math.cos(angle);
536

    
537
     mQuatT.set(0, -sinA, 0, cosA);
538

    
539
     return quotient>1.0f;
540
     }
541
}
(3-3/13)