Project

General

Profile

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

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

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.json.JsonWriter;
43
import org.distorted.objectlib.main.TwistyObject;
44

    
45
import org.json.JSONException;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
57
   private final BandagedCreatorView mView;
58
   private final Resources mResources;
59
   private final DistortedScreen mScreen;
60
   private final Static3D mScale;
61
   private final Static4D mQuatT, mQuatA;
62
   private final BandagedObject mObject;
63

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
   BandagedCreatorRenderer(BandagedCreatorView v)
79
     {
80
     mView = v;
81
     mResources = v.getResources();
82

    
83
     mQuatT = new Static4D(0,0,0,1);
84
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
85

    
86
     mResetQuats       = false;
87
     mSetQuatT         = false;
88
     mResettingObject  = false;
89
     mConnectingCubits = false;
90
     mCubitsCreated    = false;
91
     mCreatingCubits   = false;
92
     mRescaling        = false;
93

    
94
     mObjectScreenRatio= INIT_RATIO;
95
     mSaveIcon = -1;
96

    
97
     mScreen = new DistortedScreen();
98
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
99
     mScale = new Static3D(1,1,1);
100
     mObject = new BandagedObjectCuboid(mScreen);
101
     }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
   @Override
106
   public void onDrawFrame(GL10 glUnused)
107
     {
108
     long time = System.currentTimeMillis();
109
     mScreen.render(time);
110

    
111
     if( mSetQuatT )
112
       {
113
       mSetQuatT = false;
114
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
115
       }
116

    
117
     if( mResetQuats )
118
       {
119
       mResetQuats = false;
120

    
121
       float qx = mQuatT.get0();
122
       float qy = mQuatT.get1();
123
       float qz = mQuatT.get2();
124
       float qw = mQuatT.get3();
125

    
126
       float rx = mQuatA.get0();
127
       float ry = mQuatA.get1();
128
       float rz = mQuatA.get2();
129
       float rw = mQuatA.get3();
130

    
131
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
132
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
133
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
134
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
135

    
136
       mQuatT.set(0f, 0f, 0f, 1f);
137
       mQuatA.set(tx, ty, tz, tw);
138
       }
139

    
140
     if( mResettingObject )
141
       {
142
       boolean done = continueResetting(time);
143
       if( done ) mResettingObject = false;
144
       }
145

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

    
157
     if( mConnectingCubits )
158
       {
159
       mObject.tryConnectingCubits(mIndex1,mIndex2,mScaleValue);
160
       mConnectingCubits = false;
161
       }
162

    
163
     if( mCreatingCubits )
164
       {
165
       rescaleObject();
166

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

    
178
       mCreatingCubits = false;
179
       }
180

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
   public DistortedScreen getScreen()
228
     {
229
     return mScreen;
230
     }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
   void setConnecting(int index1, int index2)
235
     {
236
     mIndex1 = index1;
237
     mIndex2 = index2;
238
     mConnectingCubits = true;
239
     }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
   public Static4D getQuatAccu()
244
     {
245
     return mQuatA;
246
     }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
   public void setQuatTemp(float x, float y, float z, float w)
251
     {
252
     mSetQuatT = false;
253

    
254
     mQuatX = x;
255
     mQuatY = y;
256
     mQuatZ = z;
257
     mQuatW = w;
258

    
259
     mSetQuatT = true;
260
     }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
   public void resetQuats()
265
     {
266
     mResetQuats = true;
267
     }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
   public boolean isBusy()
272
     {
273
     return (mResettingObject || mCreatingCubits || mConnectingCubits);
274
     }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

    
284
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
285
       {
286
       setupIconCreation(act);
287
       act.addObject(obj.getShortName());
288
       }
289
     }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

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

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

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

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

    
343
       return false;
344
       }
345
     }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

    
356
     TwistyObject obj = mObject.createObject(TwistyObject.MODE_ICON, OBJECT_SIZE );
357
     DistortedEffects effects = obj.getObjectEffects();
358
     DistortedNode node = obj.getNode();
359

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

    
366
     mFramebuffer.setProjection( mObject.computeProjectionAngle() ,0.1f);
367
     mFramebuffer.detachAll();
368
     mFramebuffer.attach(node);
369

    
370
     Static1D halo = new Static1D(5);
371
     Static4D color = new Static4D(0,0,0,1);
372
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
373
     border.setHaloDepth(false);
374
     effects.apply(border);
375

    
376
     final String name = obj.getShortName()+".png";
377
     File file = new File(act.getFilesDir(), name);
378
     String filename = file.getAbsolutePath();
379

    
380
     mSaveIcon = 0;
381
     mPath = filename;
382
     }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
   private void renderIcon(long time)
387
     {
388
     mFramebuffer.render(time);
389
     }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
   private void saveIcon()
394
     {
395
     int fW = mFramebuffer.getWidth();
396
     int fH = mFramebuffer.getHeight();
397

    
398
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
399
     buf.order(ByteOrder.LITTLE_ENDIAN);
400

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

    
407
     mSaveIcon = -1;
408
     }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
   void mulObjectRatio(float ratio)
413
     {
414
     mObjectScreenRatio *= ratio;
415

    
416
     if( mObjectScreenRatio>MAX_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MAX_SIZE_CHANGE*INIT_RATIO;
417
     if( mObjectScreenRatio<MIN_SIZE_CHANGE*INIT_RATIO) mObjectScreenRatio = MIN_SIZE_CHANGE*INIT_RATIO;
418

    
419
     mRescaling = true;
420
     }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
   float getObjectRatio()
425
     {
426
     return mObjectScreenRatio;
427
     }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

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

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

    
441
   public void changeObject(int x, int y, int z)
442
     {
443
     if( mObject.tryChangeObject(x,y,z) ) mCreatingCubits = true;
444
     }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

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

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
   public void setupReset()
458
     {
459
     mResettingObject = true;
460
     mInitialPhase    = true;
461
     mStartTime       = System.currentTimeMillis();
462
     }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
   public boolean continueResetting(long time)
467
     {
468
     long diff = time-mStartTime;
469
     float quotient = ((float)diff)/RESET_DURATION;
470

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

    
478
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
479

    
480
     float sinA = (float)Math.sin(angle);
481
     float cosA = (float)Math.cos(angle);
482

    
483
     mQuatT.set(0, -sinA, 0, cosA);
484

    
485
     return quotient>1.0f;
486
     }
487

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

    
490
  public void touchCubit(int index)
491
    {
492
    mObject.touchCubit(index);
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  public void untouchCubit(int index)
498
    {
499
    mObject.untouchCubit(index);
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  public BandagedCreatorTouchControl createTouchControl()
505
    {
506
    return new BandagedCreatorTouchControl( getObjectRatio() , mScreen.getFOV(), mObject );
507
    }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public InputStream localFile(int fileID)
519
    {
520
    return mResources.openRawResource(fileID);
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public void logMessage(String message)
526
    {
527
    android.util.Log.e("BandagedCreator", message );
528
    }
529
}
(3-3/16)