Project

General

Profile

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

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

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.effect.VertexEffectDeform;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedFramebuffer;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedScreen;
40

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
61
{
62
   public static final float BRIGHTNESS = 0.333f;
63
   private static final int DURATION = 1000;
64

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

    
68
   private final BandagedCreatorView mView;
69
   private final DistortedScreen mScreen;
70
   private final Static3D mScale;
71
   private final Static4D mQuatT, mQuatA;
72

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
93
     mView = v;
94

    
95
     mResetQuats       = false;
96
     mSetQuatT         = false;
97
     mResettingObject  = false;
98
     mConnectingCubits = false;
99
     mCubitsCreated    = false;
100

    
101
     mSaveIcon = -1;
102

    
103
     mScreen = new DistortedScreen();
104
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
105
     mScale = new Static3D(1,1,1);
106
     }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
   private void createCubits()
138
     {
139
     android.util.Log.e("D", "creating cubits : "+mX+" "+mY+" "+mZ);
140

    
141
     mCubits = new BandagedCubit[mNumCubits];
142
     int c=0;
143

    
144
     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

    
154
     mView.setCubits(mCubits);
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
   private void resetObject()
160
     {
161
     mView.resetCubits();
162

    
163
     for(int c=0; c<mNumCubits; c++)
164
       {
165
       if( !mCubits[c].isAttached() )
166
         {
167
         mCubits[c].attach();
168
         mCubits[c].setUnmarked();
169
         mScreen.attach(mCubits[c].getNode());
170
         }
171
       if( mCubits[c].getPosition().length>3 )
172
         {
173
         mCubits[c].reset(mScaleValue);
174
         }
175
       }
176
     }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
   @Override
181
   public void onDrawFrame(GL10 glUnused)
182
     {
183
     long time = System.currentTimeMillis();
184
     mScreen.render(time);
185

    
186
     if( mSetQuatT )
187
       {
188
       mSetQuatT = false;
189
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
190
       }
191

    
192
     if( mResetQuats )
193
       {
194
       mResetQuats = false;
195

    
196
       float qx = mQuatT.get0();
197
       float qy = mQuatT.get1();
198
       float qz = mQuatT.get2();
199
       float qw = mQuatT.get3();
200

    
201
       float rx = mQuatA.get0();
202
       float ry = mQuatA.get1();
203
       float rz = mQuatA.get2();
204
       float rw = mQuatA.get3();
205

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

    
215
     if( mResettingObject )
216
       {
217
       boolean done = continueResetting(time);
218
       if( done ) mResettingObject = false;
219
       }
220

    
221
     if( mSaveIcon>=0 )
222
       {
223
       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
       }
226
     if( mSaveIcon>=2 )
227
       {
228
       saveIcon();
229
       mSaveIcon = -1;
230
       }
231

    
232
     if( mConnectingCubits )
233
       {
234
       mConnectingCubits = false;
235
       tryConnectingCubits(mIndex1,mIndex2);
236
       }
237
     }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
   @Override
242
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
243
      {
244
      final float Q = SCREEN_RATIO/OBJECT_SIZE;
245
      mScaleValue = width<height ? Q*width : Q*height;
246

    
247
      mScreen.detachAll();
248
      int touched = mView.getTouched();
249

    
250
      for(int i=0; i<mNumCubits; i++)
251
        if( mCubits[i].isAttached() )
252
          {
253
          mCubits[i].scaleMove(mScaleValue);
254
          if( touched==i ) mCubits[i].setMarked();
255
          else             mCubits[i].setUnmarked();
256
          DistortedNode node = mCubits[i].getNode();
257
          mScreen.attach(node);
258
          }
259

    
260
      mScale.set( mScaleValue,mScaleValue,mScaleValue );
261
      mView.setScreenSize(width,height);
262
      mScreen.resize(width,height);
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
   @Override
268
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
269
      {
270
      DistortedLibrary.setMax(EffectType.VERTEX,25);
271
      MeshBase.setMaxEffComponents(50);
272

    
273
      VertexEffectDeform.enable();
274
      FragmentEffectBrightness.enable();
275

    
276
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
277
      DistortedLibrary.setCull(true);
278

    
279
      createCubits();
280
      mCubitsCreated = true;
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
   public void distortedException(Exception ex)
286
     {
287
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
288
     }
289

    
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
   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
     {
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
         mCubits[index2].join(pos1,mScaleValue);
325
         mCubits[index1].detach();
326
         mScreen.detach(mCubits[index1].getNode());
327
         }
328
       }
329
     }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
   public Static4D getQuatAccu()
334
     {
335
     return mQuatA;
336
     }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
   public void setQuatTemp(float x, float y, float z, float w)
341
     {
342
     mQuatX = x;
343
     mQuatY = y;
344
     mQuatZ = z;
345
     mQuatW = w;
346

    
347
     mSetQuatT = true;
348
     }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
   public void resetQuats()
353
     {
354
     mResetQuats = true;
355
     }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
   public boolean isBusy()
360
     {
361
     return mResettingObject;
362
     }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
   public void saveObject()
367
     {
368
     int numAttached=0;
369

    
370
     for(int i=0; i<mNumCubits; i++)
371
       if( mCubits[i].isAttached() ) numAttached++;
372

    
373
     float[][] pos = new float[numAttached][];
374
     int attached=0;
375

    
376
     for(int i=0; i<mNumCubits; i++)
377
       if( mCubits[i].isAttached() )
378
         {
379
         pos[attached++] = mCubits[i].getPosition();
380
         }
381

    
382
     TwistyBandagedGeneric.setPositions(pos);
383
     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
     String name = obj.getShortName();
386
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
387

    
388
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
389
       {
390
       setupIconCreation(act);
391
       act.addObject(obj.getShortName());
392
       }
393
     }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
   private boolean createObjectJson(TwistyObject object, Activity act)
398
     {
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
       return true;
409
       }
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

    
421
       return false;
422
       }
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

    
434
       return false;
435
       }
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

    
447
       return false;
448
       }
449
     }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
   private void setupIconCreation(Activity act)
454
     {
455
     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
     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

    
466
     if( mFramebuffer==null )
467
       {
468
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
469
       mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
470
       }
471

    
472
     mFramebuffer.detachAll();
473
     mFramebuffer.attach(node);
474

    
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
     final String name = obj.getShortName()+".png";
482
     File file = new File(act.getFilesDir(), name);
483
     String filename = file.getAbsolutePath();
484

    
485
     mSaveIcon = 0;
486
     mPath = filename;
487
     }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
   private void renderIcon(long time)
492
     {
493
     mFramebuffer.render(time);
494
     }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
   private void saveIcon()
499
     {
500
     int fW = mFramebuffer.getWidth();
501
     int fH = mFramebuffer.getHeight();
502

    
503
     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

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
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
       mNumCubits = computeNumCubits(mX,mY,mZ);
527

    
528
       if( mCubitsCreated )
529
         {
530
         createCubits();
531

    
532
         mScreen.detachAll();
533
         mView.resetCubits();
534

    
535
         for(int i=0; i<mNumCubits; i++)
536
           {
537
           mCubits[i].scaleMove(mScaleValue);
538
           DistortedNode node = mCubits[i].getNode();
539
           mScreen.attach(node);
540
           }
541
         }
542
       }
543
     }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
   public void displaySavingDialog()
548
     {
549
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
550
     RubikDialogBandagedSave saveDiag = new RubikDialogBandagedSave();
551
     saveDiag.show(act.getSupportFragmentManager(), null);
552
     }
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

    
583
     return quotient>1.0f;
584
     }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  public void touchCubit(int index)
589
    {
590
    mCubits[index].setMarked();
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
  public void untouchCubit(int index)
596
    {
597
    mCubits[index].setUnmarked();
598
    }
599
}
(3-3/13)