Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 41cd5230

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import java.io.File;
13
import java.io.FileNotFoundException;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.nio.ByteBuffer;
17
import java.nio.ByteOrder;
18

    
19
import javax.microedition.khronos.egl.EGLConfig;
20
import javax.microedition.khronos.opengles.GL10;
21

    
22
import android.app.Activity;
23
import android.content.res.Resources;
24
import android.opengl.GLES31;
25
import android.opengl.GLSurfaceView;
26
import android.widget.Toast;
27

    
28
import org.distorted.dialogs.RubikDialogBandagedSave;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.FragmentEffectBrightness;
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
import org.distorted.library.main.InternalOutputSurface;
38
import org.distorted.library.mesh.MeshBase;
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.bandaged.BandagedObject;
43
//import org.distorted.objectlib.bandaged.BandagedObjectCuboid;
44
import org.distorted.objectlib.bandaged.BandagedObjectPyraminx;
45
import org.distorted.objectlib.json.JsonWriter;
46
import org.distorted.objectlib.main.TwistyObject;
47

    
48
import org.json.JSONException;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53
{
54
   public static final float BRIGHTNESS = 0.333f;
55
   private static final int RESET_DURATION = 1000;
56
   private static final float MAX_SIZE_CHANGE = 1.70f;
57
   private static final float MIN_SIZE_CHANGE = 0.50f;
58
   private static final float INIT_RATIO = 0.5f;
59

    
60
   private final BandagedCreatorView mView;
61
   private final Resources mResources;
62
   private final DistortedScreen mScreen;
63
   private final Static3D mScale;
64
   private final Static4D mQuatT, mQuatA;
65
   private final BandagedObject mObject;
66

    
67
   private boolean mInitialPhase;
68
   private long mStartTime;
69
   private float mQuatX, mQuatY, mQuatZ, mQuatW;
70
   private boolean mResetQuats, mSetQuatT, mResettingObject, mConnectingCubits, mCreatingCubits, mRescaling;
71
   private int mIndex1, mIndex2;
72
   private int mSaveIcon;
73
   private DistortedFramebuffer mFramebuffer;
74
   private String mPath;
75
   private boolean mCubitsCreated;
76
   private int mWidth, mHeight;
77
   private float mScaleValue, mObjectScreenRatio;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
   BandagedCreatorRenderer(BandagedCreatorView v)
82
     {
83
     mView = v;
84
     mResources = v.getResources();
85

    
86
     mQuatT = new Static4D(0,0,0,1);
87
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
88

    
89
     mResetQuats       = false;
90
     mSetQuatT         = false;
91
     mResettingObject  = false;
92
     mConnectingCubits = false;
93
     mCubitsCreated    = false;
94
     mCreatingCubits   = false;
95
     mRescaling        = false;
96

    
97
     mObjectScreenRatio= INIT_RATIO;
98
     mSaveIcon = -1;
99

    
100
     mScreen = new DistortedScreen();
101
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
102
     mScale = new Static3D(1,1,1);
103
     mObject = new BandagedObjectPyraminx(mScreen);
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   @Override
109
   public void onDrawFrame(GL10 glUnused)
110
     {
111
     long time = System.currentTimeMillis();
112
     mScreen.render(time);
113

    
114
     if( mSetQuatT )
115
       {
116
       mSetQuatT = false;
117
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
118
       }
119

    
120
     if( mResetQuats )
121
       {
122
       mResetQuats = false;
123

    
124
       float qx = mQuatT.get0();
125
       float qy = mQuatT.get1();
126
       float qz = mQuatT.get2();
127
       float qw = mQuatT.get3();
128

    
129
       float rx = mQuatA.get0();
130
       float ry = mQuatA.get1();
131
       float rz = mQuatA.get2();
132
       float rw = mQuatA.get3();
133

    
134
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
135
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
136
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
137
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
138

    
139
       mQuatT.set(0f, 0f, 0f, 1f);
140
       mQuatA.set(tx, ty, tz, tw);
141
       }
142

    
143
     if( mResettingObject )
144
       {
145
       boolean done = continueResetting(time);
146
       if( done ) mResettingObject = false;
147
       }
148

    
149
     if( mSaveIcon>=0 )
150
       {
151
       renderIcon(time); // for some reason we need to call render() twice here, otherwise the
152
       mSaveIcon++;      // icon turns out black. Probably some problem with binding the texture.
153
       }
154
     if( mSaveIcon>=2 )
155
       {
156
       saveIcon();
157
       mSaveIcon = -1;
158
       }
159

    
160
     if( mConnectingCubits )
161
       {
162
       mObject.tryConnectingCubits(mIndex1,mIndex2,mScaleValue);
163
       mConnectingCubits = false;
164
       }
165

    
166
     if( mCreatingCubits )
167
       {
168
       rescaleObject();
169

    
170
       if( mCubitsCreated )
171
         {
172
         mObject.createCubits(mQuatT,mQuatA,mScale);
173
         float[] dist = mObject.getDist3D();
174
         BandagedCreatorTouchControl control = mView.getTouchControl();
175
         control.setDist3D(dist);
176
         mScreen.detachAll();
177
         mView.resetCubits();
178
         mObject.attachCubits(mScaleValue);
179
         }
180

    
181
       mCreatingCubits = false;
182
       }
183

    
184
     if( mRescaling )
185
       {
186
       rescaleObject();
187
       mObject.scaleCubits(mScaleValue);
188
       BandagedCreatorTouchControl control = mView.getTouchControl();
189
       control.setObjectRatio(mObjectScreenRatio);
190
       mRescaling = false;
191
       }
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
   @Override
197
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
198
      {
199
      if( width!=mWidth || height!=mHeight )
200
        {
201
        mWidth = width;
202
        mHeight= height;
203
        rescaleObject();
204
        mScreen.detachAll();
205
        int touched = mView.getTouched();
206
        mObject.attachAndMarkCubits(mScaleValue,touched);
207
        mView.setScreenSize(width,height);
208
        mScreen.resize(width,height);
209
        }
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
   @Override
215
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
216
      {
217
      DistortedLibrary.setMax(EffectType.VERTEX,0);
218
      MeshBase.setMaxEffComponents(120); // what?
219
      FragmentEffectBrightness.enable();
220
      DistortedLibrary.onSurfaceCreated(this,1);
221
      DistortedLibrary.setCull(true);
222
      mObject.recreateCubits(mQuatT,mQuatA,mScale);
223
      mCubitsCreated = true;
224
      mWidth = 0;
225
      mHeight= 0;
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
   public DistortedScreen getScreen()
231
     {
232
     return mScreen;
233
     }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
   void setConnecting(int index1, int index2)
238
     {
239
     mIndex1 = index1;
240
     mIndex2 = index2;
241
     mConnectingCubits = true;
242
     }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
   public Static4D getQuatAccu()
247
     {
248
     return mQuatA;
249
     }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
   public void setQuatTemp(float x, float y, float z, float w)
254
     {
255
     mSetQuatT = false;
256

    
257
     mQuatX = x;
258
     mQuatY = y;
259
     mQuatZ = z;
260
     mQuatW = w;
261

    
262
     mSetQuatT = true;
263
     }
264

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

    
267
   public void resetQuats()
268
     {
269
     mResetQuats = true;
270
     }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
   public boolean isBusy()
275
     {
276
     return (mResettingObject || mCreatingCubits || mConnectingCubits);
277
     }
278

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

    
281
   public void saveObject()
282
     {
283
     TwistyObject obj = mObject.createObject(TwistyObject.MODE_NORM, 1.0f );
284
     String name = obj.getShortName();
285
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
286

    
287
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
288
       {
289
       setupIconCreation(act);
290
       act.addObject(obj.getShortName());
291
       }
292
     }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
   private boolean createObjectJson(TwistyObject object, Activity act)
297
     {
298
     final String name = object.getShortName()+"_object.json";
299
     File file = new File(act.getFilesDir(), name);
300
     String filename = file.getAbsolutePath();
301

    
302
     try
303
       {
304
       JsonWriter writer = JsonWriter.getInstance();
305
       String json = writer.createObjectString(object,24,0);
306
       writer.write(filename,json);
307
       return true;
308
       }
309
     catch(JSONException ex)
310
       {
311
       act.runOnUiThread(new Runnable()
312
         {
313
         public void run()
314
           {
315
           String message = "JSON Exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
316
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
317
           }
318
         });
319

    
320
       return false;
321
       }
322
     catch(FileNotFoundException ex)
323
       {
324
       act.runOnUiThread(new Runnable()
325
         {
326
         public void run()
327
           {
328
           String message = "FileNotFound exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
329
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
330
           }
331
         });
332

    
333
       return false;
334
       }
335
     catch(IOException ex)
336
       {
337
       act.runOnUiThread(new Runnable()
338
         {
339
         public void run()
340
           {
341
           String message = "IO exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
342
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
343
           }
344
         });
345

    
346
       return false;
347
       }
348
     }
349

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

    
352
   private void setupIconCreation(Activity act)
353
     {
354
     final float R=1.0f;
355
     final int FBO_WIDTH  = (int)(R*720);
356
     final int FBO_HEIGHT = (int)(R*1280);
357
     final float OBJECT_SIZE = R*0.35f;
358

    
359
     TwistyObject obj = mObject.createObject(TwistyObject.MODE_ICON, OBJECT_SIZE );
360
     DistortedEffects effects = obj.getObjectEffects();
361
     DistortedNode node = obj.getNode();
362

    
363
     if( mFramebuffer==null )
364
       {
365
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
366
       mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
367
       }
368

    
369
     mFramebuffer.setProjection( mObject.computeProjectionAngle() ,0.1f);
370
     mFramebuffer.detachAll();
371
     mFramebuffer.attach(node);
372

    
373
     Static1D halo = new Static1D(5);
374
     Static4D color = new Static4D(0,0,0,1);
375
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
376
     border.setHaloDepth(false);
377
     effects.apply(border);
378

    
379
     final String name = obj.getShortName()+".png";
380
     File file = new File(act.getFilesDir(), name);
381
     String filename = file.getAbsolutePath();
382

    
383
     mSaveIcon = 0;
384
     mPath = filename;
385
     }
386

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

    
389
   private void renderIcon(long time)
390
     {
391
     mFramebuffer.render(time);
392
     }
393

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

    
396
   private void saveIcon()
397
     {
398
     int fW = mFramebuffer.getWidth();
399
     int fH = mFramebuffer.getHeight();
400

    
401
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
402
     buf.order(ByteOrder.LITTLE_ENDIAN);
403

    
404
     mFramebuffer.setAsReadFramebuffer(0);
405
     GLES31.glReadBuffer(GLES31.GL_COLOR_ATTACHMENT0);
406
     GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
407
     BandagedCreatorWorkerThread.newBuffer(buf,fW,fH,6,mPath);
408
     GLES31.glBindFramebuffer(GLES31.GL_READ_FRAMEBUFFER, 0);
409

    
410
     mSaveIcon = -1;
411
     }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
   void mulObjectRatio(float ratio)
416
     {
417
     mObjectScreenRatio *= ratio;
418

    
419
     if( mObjectScreenRatio>MAX_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MAX_SIZE_CHANGE*INIT_RATIO;
420
     if( mObjectScreenRatio<MIN_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MIN_SIZE_CHANGE*INIT_RATIO;
421

    
422
     mRescaling = true;
423
     }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
   float getObjectRatio()
428
     {
429
     return mObjectScreenRatio;
430
     }
431

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

    
434
   private void rescaleObject()
435
     {
436
     float size = mObject.getMaxSize();
437
     final float Q = mObjectScreenRatio/size;
438
     mScaleValue = mWidth<mHeight ? Q*mWidth : Q*mHeight;
439
     mScale.set( mScaleValue,mScaleValue,mScaleValue );
440
     }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
   public void changeObject(int x, int y, int z)
445
     {
446
     if( mObject.tryChangeObject(x,y,z) ) mCreatingCubits = true;
447
     }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
   public void displaySavingDialog()
452
     {
453
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
454
     RubikDialogBandagedSave saveDiag = new RubikDialogBandagedSave();
455
     saveDiag.show(act.getSupportFragmentManager(), null);
456
     }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
   public void setupReset()
461
     {
462
     mResettingObject = true;
463
     mInitialPhase    = true;
464
     mStartTime       = System.currentTimeMillis();
465
     }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
   public boolean continueResetting(long time)
470
     {
471
     long diff = time-mStartTime;
472
     float quotient = ((float)diff)/RESET_DURATION;
473

    
474
     if( mInitialPhase && quotient>0.5f )
475
       {
476
       mInitialPhase=false;
477
       mView.resetCubits();
478
       mObject.resetObject(mScaleValue);
479
       }
480

    
481
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
482

    
483
     float sinA = (float)Math.sin(angle);
484
     float cosA = (float)Math.cos(angle);
485

    
486
     mQuatT.set(0, -sinA, 0, cosA);
487

    
488
     return quotient>1.0f;
489
     }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  public void touchCubit(int index)
494
    {
495
    mObject.touchCubit(index);
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  public void untouchCubit(int index)
501
    {
502
    mObject.untouchCubit(index);
503
    }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
  public BandagedCreatorTouchControl createTouchControl()
508
    {
509
    return new BandagedCreatorTouchControl( getObjectRatio() , mScreen.getFOV(), mObject );
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
  public void distortedException(Exception ex)
515
    {
516
    android.util.Log.e("BandagedCreator", "unexpected exception: "+ex.getMessage() );
517
    }
518

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

    
521
  public InputStream localFile(int fileID)
522
    {
523
    return mResources.openRawResource(fileID);
524
    }
525

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

    
528
  public void logMessage(String message)
529
    {
530
    android.util.Log.e("BandagedCreator", message );
531
    }
532
}
(3-3/12)