Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ d876c3bc

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.RubikDialogBandagedSave;
31
import org.distorted.library.effect.EffectType;
32
import org.distorted.library.effect.FragmentEffectBrightness;
33
import org.distorted.library.effect.PostprocessEffectBorder;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedFramebuffer;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedScreen;
39

    
40
import org.distorted.library.main.InternalOutputSurface;
41
import org.distorted.library.mesh.MeshBase;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
import org.distorted.objectlib.json.JsonWriter;
46
import org.distorted.objectlib.main.ShapeHexahedron;
47
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
import java.nio.ByteBuffer;
55
import java.nio.ByteOrder;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
60
{
61
   public static final float BRIGHTNESS = 0.333f;
62
   private static final int RESET_DURATION = 1000;
63
   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

    
67
   private final BandagedCreatorView mView;
68
   private final DistortedScreen mScreen;
69
   private final Static3D mScale;
70
   private final Static4D mQuatT, mQuatA;
71
   private final int[] mObjSize;
72

    
73
   private BandagedCubit[] mCubits;
74
   private boolean mInitialPhase;
75
   private long mStartTime;
76
   private float mQuatX, mQuatY, mQuatZ, mQuatW;
77
   private int mX, mY, mZ, mNumCubits;
78
   private boolean mResetQuats, mSetQuatT, mResettingObject, mConnectingCubits, mCreatingCubits, mRescaling;
79
   private int mIndex1, mIndex2;
80
   private int mSaveIcon;
81
   private DistortedFramebuffer mFramebuffer;
82
   private String mPath;
83
   private boolean mCubitsCreated;
84
   private int mWidth, mHeight;
85
   private float mScaleValue, mObjectScreenRatio;
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   BandagedCreatorRenderer(BandagedCreatorView v)
90
     {
91
     mQuatT = new Static4D(0,0,0,1);
92
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
93

    
94
     mView = v;
95

    
96
     mResetQuats       = false;
97
     mSetQuatT         = false;
98
     mResettingObject  = false;
99
     mConnectingCubits = false;
100
     mCubitsCreated    = false;
101
     mCreatingCubits   = false;
102
     mRescaling        = false;
103

    
104
     mObjectScreenRatio= INIT_RATIO;
105
     mSaveIcon = -1;
106
     mObjSize  = new int[3];
107

    
108
     mScreen = new DistortedScreen();
109
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
110
     mScale = new Static3D(1,1,1);
111
     }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
   private void createCubits()
143
     {
144
     mCubits = new BandagedCubit[mNumCubits];
145
     int c=0;
146

    
147
     float begX = 0.5f*(1-mX);
148
     float begY = 0.5f*(1-mY);
149
     float begZ = 0.5f*(1-mZ);
150

    
151
     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
              float[] pos = new float[] { begX+x,begY+y,begZ+z };
157
              mCubits[c] = new BandagedCubit(pos,mX,mY,mZ,mQuatT,mQuatA,mScale,false);
158
              c++;
159
              }
160

    
161
     mView.setCubits(mCubits,mX,mY,mZ);
162
     }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   private void resetObject()
167
     {
168
     mView.resetCubits();
169

    
170
     for(int c=0; c<mNumCubits; c++)
171
       {
172
       if( !mCubits[c].isAttached() )
173
         {
174
         mCubits[c].attach();
175
         mCubits[c].setUnmarked();
176
         mScreen.attach(mCubits[c].getNode());
177
         }
178
       if( mCubits[c].getPosition().length>3 )
179
         {
180
         mCubits[c].reset(mScaleValue);
181
         }
182
       }
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
   @Override
188
   public void onDrawFrame(GL10 glUnused)
189
     {
190
     long time = System.currentTimeMillis();
191
     mScreen.render(time);
192

    
193
     if( mSetQuatT )
194
       {
195
       mSetQuatT = false;
196
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
197
       }
198

    
199
     if( mResetQuats )
200
       {
201
       mResetQuats = false;
202

    
203
       float qx = mQuatT.get0();
204
       float qy = mQuatT.get1();
205
       float qz = mQuatT.get2();
206
       float qw = mQuatT.get3();
207

    
208
       float rx = mQuatA.get0();
209
       float ry = mQuatA.get1();
210
       float rz = mQuatA.get2();
211
       float rw = mQuatA.get3();
212

    
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
       mQuatT.set(0f, 0f, 0f, 1f);
219
       mQuatA.set(tx, ty, tz, tw);
220
       }
221

    
222
     if( mResettingObject )
223
       {
224
       boolean done = continueResetting(time);
225
       if( done ) mResettingObject = false;
226
       }
227

    
228
     if( mSaveIcon>=0 )
229
       {
230
       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
       }
233
     if( mSaveIcon>=2 )
234
       {
235
       saveIcon();
236
       mSaveIcon = -1;
237
       }
238

    
239
     if( mConnectingCubits )
240
       {
241
       mConnectingCubits = false;
242
       tryConnectingCubits(mIndex1,mIndex2);
243
       }
244

    
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

    
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
     }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
   @Override
280
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
281
      {
282
      mWidth = width;
283
      mHeight= height;
284
      rescaleObject();
285

    
286
      mScreen.detachAll();
287
      int touched = mView.getTouched();
288

    
289
      for(int i=0; i<mNumCubits; i++)
290
        if( mCubits[i].isAttached() )
291
          {
292
          mCubits[i].scaleMove(mScaleValue);
293
          if( touched==i ) mCubits[i].setMarked();
294
          else             mCubits[i].setUnmarked();
295
          DistortedNode node = mCubits[i].getNode();
296
          mScreen.attach(node);
297
          }
298

    
299
      mView.setScreenSize(width,height);
300
      mScreen.resize(width,height);
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
   @Override
306
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
307
      {
308
      DistortedLibrary.setMax(EffectType.VERTEX,0);
309
      MeshBase.setMaxEffComponents(120);
310

    
311
      FragmentEffectBrightness.enable();
312

    
313
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
314
      DistortedLibrary.setCull(true);
315

    
316
      createCubits();
317
      mCubitsCreated = true;
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
   public void distortedException(Exception ex)
323
     {
324
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
325
     }
326

    
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
   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
     {
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
         mCubits[index2].join(pos1,mScaleValue);
362
         mCubits[index1].detach();
363
         mScreen.detach(mCubits[index1].getNode());
364
         }
365
       }
366
     }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
   public Static4D getQuatAccu()
371
     {
372
     return mQuatA;
373
     }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
   public void setQuatTemp(float x, float y, float z, float w)
378
     {
379
     mQuatX = x;
380
     mQuatY = y;
381
     mQuatZ = z;
382
     mQuatW = w;
383

    
384
     mSetQuatT = true;
385
     }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
   public void resetQuats()
390
     {
391
     mResetQuats = true;
392
     }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
   public boolean isBusy()
397
     {
398
     return mResettingObject;
399
     }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
   public void saveObject()
404
     {
405
     int numAttached=0;
406

    
407
     for(int i=0; i<mNumCubits; i++)
408
       if( mCubits[i].isAttached() ) numAttached++;
409

    
410
     float[][] pos = new float[numAttached][];
411
     int attached=0;
412

    
413
     for(int i=0; i<mNumCubits; i++)
414
       if( mCubits[i].isAttached() )
415
         {
416
         pos[attached++] = mCubits[i].getPosition();
417
         }
418

    
419
     TwistyBandagedGeneric.setPositions(pos);
420
     TwistyObject obj = new TwistyBandagedGeneric( mObjSize, TwistyObject.MESH_NICE, TwistyObject.MODE_NORM,
421
                                                   new Static4D(0,0,0,1), new Static3D(0,0,0), 1.0f, null );
422
     String name = obj.getShortName();
423
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
424

    
425
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
426
       {
427
       setupIconCreation(act);
428
       act.addObject(obj.getShortName());
429
       }
430
     }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
   private boolean createObjectJson(TwistyObject object, Activity act)
435
     {
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
       return true;
446
       }
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

    
458
       return false;
459
       }
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

    
471
       return false;
472
       }
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

    
484
       return false;
485
       }
486
     }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
   private void setupIconCreation(Activity act)
491
     {
492
     final float R=1.0f;
493
     final int FBO_WIDTH  = (int)(R*720);
494
     final int FBO_HEIGHT = (int)(R*1280);
495
     final float OBJECT_SIZE = R*0.35f;
496

    
497
     TwistyObject obj = new TwistyBandagedGeneric( mObjSize, TwistyObject.MESH_NICE, TwistyObject.MODE_ICON,
498
                                                   ShapeHexahedron.DEFAULT_ROT, new Static3D(0,0,0), OBJECT_SIZE, null );
499

    
500
     DistortedEffects effects = obj.getObjectEffects();
501
     DistortedNode node = obj.getNode();
502

    
503
     if( mFramebuffer==null )
504
       {
505
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
506
       mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
507
       }
508

    
509
     mFramebuffer.detachAll();
510
     mFramebuffer.attach(node);
511

    
512
     Static1D halo = new Static1D(5);
513
     Static4D color = new Static4D(0,0,0,1);
514
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
515
     border.setHaloDepth(false);
516
     effects.apply(border);
517

    
518
     final String name = obj.getShortName()+".png";
519
     File file = new File(act.getFilesDir(), name);
520
     String filename = file.getAbsolutePath();
521

    
522
     mSaveIcon = 0;
523
     mPath = filename;
524
     }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
   private void renderIcon(long time)
529
     {
530
     mFramebuffer.render(time);
531
     }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
   private void saveIcon()
536
     {
537
     int fW = mFramebuffer.getWidth();
538
     int fH = mFramebuffer.getHeight();
539

    
540
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
541
     buf.order(ByteOrder.LITTLE_ENDIAN);
542

    
543
     mFramebuffer.setAsReadFramebuffer(0);
544
     GLES31.glReadBuffer(GLES31.GL_COLOR_ATTACHMENT0);
545
     GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
546
     BandagedCreatorWorkerThread.newBuffer(buf,fW,fH,6,mPath);
547
     GLES31.glBindFramebuffer(GLES31.GL_READ_FRAMEBUFFER, 0);
548

    
549
     mSaveIcon = -1;
550
     }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
   void mulObjectRatio(float ratio)
555
     {
556
     mObjectScreenRatio *= ratio;
557

    
558
     if( mObjectScreenRatio>MAX_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MAX_SIZE_CHANGE*INIT_RATIO;
559
     if( mObjectScreenRatio<MIN_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MIN_SIZE_CHANGE*INIT_RATIO;
560

    
561
     mRescaling = true;
562
     }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
   float getObjectRatio()
567
     {
568
     return mObjectScreenRatio;
569
     }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
   private void rescaleObject()
574
     {
575
     final int size = mX>mY ? Math.max(mX, mZ) : Math.max(mY, mZ);
576
     final float Q = mObjectScreenRatio/size;
577
     mScaleValue = mWidth<mHeight ? Q*mWidth : Q*mHeight;
578
     mScale.set( mScaleValue,mScaleValue,mScaleValue );
579
     }
580

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

    
583
   public void changeObject(int x, int y, int z)
584
     {
585
     if( mX!=x || mY!=y || mZ!=z )
586
       {
587
       mX = x;
588
       mY = y;
589
       mZ = z;
590

    
591
       mObjSize[0] = mX;
592
       mObjSize[1] = mY;
593
       mObjSize[2] = mZ;
594

    
595
       mNumCubits = computeNumCubits(mX,mY,mZ);
596

    
597
       mCreatingCubits = true;
598
       }
599
     }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
   public void displaySavingDialog()
604
     {
605
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
606
     RubikDialogBandagedSave saveDiag = new RubikDialogBandagedSave();
607
     saveDiag.show(act.getSupportFragmentManager(), null);
608
     }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
   public void setupReset()
613
     {
614
     mResettingObject = true;
615
     mInitialPhase    = true;
616
     mStartTime       = System.currentTimeMillis();
617
     }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
   public boolean continueResetting(long time)
622
     {
623
     long diff = time-mStartTime;
624
     float quotient = ((float)diff)/RESET_DURATION;
625

    
626
     if( mInitialPhase && quotient>0.5f )
627
       {
628
       mInitialPhase=false;
629
       resetObject();
630
       }
631

    
632
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
633

    
634
     float sinA = (float)Math.sin(angle);
635
     float cosA = (float)Math.cos(angle);
636

    
637
     mQuatT.set(0, -sinA, 0, cosA);
638

    
639
     return quotient>1.0f;
640
     }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
  public void touchCubit(int index)
645
    {
646
    mCubits[index].setMarked();
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
  public void untouchCubit(int index)
652
    {
653
    mCubits[index].setUnmarked();
654
    }
655
}
(3-3/13)