Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ fdec60a3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.magic;
21

    
22
import android.opengl.GLSurfaceView;
23

    
24
import org.distorted.effect.BaseEffect;
25
import org.distorted.library.effect.VertexEffectSink;
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.library.main.DistortedScreen;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshFlat;
31
import org.distorted.library.message.EffectListener;
32

    
33
import javax.microedition.khronos.egl.EGLConfig;
34
import javax.microedition.khronos.opengles.GL10;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
39
{
40
    private static final float CUBE_SCREEN_RATIO = 0.5f;
41
    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
42
    public  static final int TEXTURE_SIZE = 600;
43

    
44
    private RubikSurfaceView mView;
45
    private DistortedScreen mScreen;
46
    private float mCubeSizeInScreenSpace;
47
    private int mNextCubeSize, mScrambleCubeNum;
48
    private long mRotationFinishedID;
49
    private long[] mEffectID;
50
    private boolean mFinishRotation, mRemoveRotation, mSetQuatCurrent, mSetQuatAccumulated;
51
    private boolean mSizeChangeCube, mSolveCube, mScrambleCube;
52
    private boolean mCanRotate, mCanDrag, mCanUI;
53
    private boolean mIsSolved;
54
    private RubikCube mOldCube, mNewCube;
55
    private int mScreenWidth, mScreenHeight;
56
    private MeshFlat mMesh;
57

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

    
60
    RubikRenderer(RubikSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      mScreen = new DistortedScreen();
65

    
66
      mOldCube = null;
67
      mNewCube = null;
68

    
69
      mScreenWidth = mScreenHeight = 0;
70
      mScrambleCubeNum = 0;
71

    
72
      mFinishRotation     = false;
73
      mRemoveRotation     = false;
74
      mSetQuatCurrent     = false;
75
      mSetQuatAccumulated = false;
76
      mSizeChangeCube     = true;
77
      mSolveCube          = false;
78
      mScrambleCube       = false;
79

    
80
      mCanRotate   = true;
81
      mCanDrag     = true;
82
      mCanUI       = true;
83

    
84
      mEffectID = new long[BaseEffect.Type.LENGTH];
85

    
86
      mMesh= new MeshFlat(20,20);
87
      mNextCubeSize = RubikActivity.getSize();
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
   private float computeFOV(float cameraDistance, int screenHeight)
93
     {
94
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
95
     return (float)(2*halfFOVInRadians*(180/Math.PI));
96
     }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
101
     {
102
     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
103

    
104
     if( mNewCube!=null )
105
       {
106
       mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace);
107
       }
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   private void createCubeNow(int newSize)
113
     {
114
     if( mOldCube!=null ) mOldCube.releaseResources();
115
     mOldCube = mNewCube;
116

    
117
     DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
118
     DistortedEffects effects = new DistortedEffects();
119

    
120
     mNewCube = new RubikCube(newSize, mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects);
121
     mNewCube.createTexture();
122

    
123
     if( mScreenWidth!=0 )
124
       {
125
       recomputeScaleFactor(mScreenWidth,mScreenHeight);
126
       }
127

    
128
     mIsSolved = true;
129
     }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
133

    
134
   private void doEffectNow(BaseEffect.Type type)
135
     {
136
     int index = type.ordinal();
137

    
138
     mEffectID[index] = type.startEffect(this);
139

    
140
     if( mEffectID[index] < 0 )
141
       {
142
       mCanUI     = true;
143
       mCanRotate = true;
144
       mCanDrag   = true;
145
       }
146
     }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// no this will not race with onDrawFrame
150

    
151
   void finishRotation()
152
     {
153
     mFinishRotation = true;
154
     }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
   boolean createCube(int newSize)
159
     {
160
     if( mCanDrag && mCanRotate && (mNewCube==null || newSize != mNewCube.getSize()) )
161
       {
162
       mSizeChangeCube = true;
163
       mNextCubeSize = newSize;
164
       return true;
165
       }
166

    
167
     return false;
168
     }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
   void scrambleCube(int num)
173
     {
174
     if( mCanUI )
175
       {
176
       mScrambleCube = true;
177
       mScrambleCubeNum = num;
178
       }
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   void solveCube()
184
     {
185
     if( mCanUI )
186
       {
187
       mSolveCube = true;
188
       }
189
     }
190

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

    
193
   float returnCubeSizeInScreenSpace()
194
     {
195
     return mCubeSizeInScreenSpace;
196
     }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
   boolean canRotate()
201
     {
202
     return mCanRotate;
203
     }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   boolean canDrag()
208
     {
209
     return mCanDrag;
210
     }
211

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

    
214
   void setQuatCurrentOnNextRender()
215
     {
216
     mSetQuatCurrent = true;
217
     }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
   void setQuatAccumulatedOnNextRender()
222
     {
223
     mSetQuatAccumulated = true;
224
     }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// PUBLIC API
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
// various things are done here delayed, 'after the next render' as not to be done mid-render and
230
// cause artifacts.
231

    
232
   @Override
233
   public void onDrawFrame(GL10 glUnused)
234
     {
235
     mScreen.render( System.currentTimeMillis() );
236

    
237
     if( mSetQuatCurrent )
238
       {
239
       mSetQuatCurrent = false;
240
       mView.setQuatCurrent();
241
       }
242

    
243
     if( mSetQuatAccumulated )
244
       {
245
       mSetQuatAccumulated = false;
246
       mView.setQuatAccumulated();
247
       }
248

    
249
     if( mFinishRotation )
250
       {
251
       mFinishRotation = false;
252
       mCanRotate      = false;
253
       mCanUI          = false;
254
       mRotationFinishedID = mNewCube.finishRotationNow(this);
255
       }
256

    
257
     if( mRemoveRotation )
258
       {
259
       mRemoveRotation=false;
260
       mNewCube.removeRotationNow();
261

    
262
       boolean solved = mNewCube.isSolved();
263

    
264
       if( solved && !mIsSolved )
265
         {
266
         mCanDrag        = false;
267
         mCanRotate      = false;
268
         mCanUI          = false;
269
         doEffectNow( BaseEffect.Type.WIN );
270
         }
271
       else
272
         {
273
         mCanRotate = true;
274
         mCanUI     = true;
275
         }
276

    
277
       mIsSolved = solved;
278
       }
279

    
280
     if( mSizeChangeCube )
281
       {
282
       mSizeChangeCube = false;
283
       mCanDrag        = false;
284
       mCanRotate      = false;
285
       mCanUI          = false;
286
       createCubeNow(mNextCubeSize);
287
       doEffectNow( BaseEffect.Type.SIZECHANGE );
288
       }
289

    
290
     if( mSolveCube )
291
       {
292
       mSolveCube      = false;
293
       mCanDrag        = false;
294
       mCanRotate      = false;
295
       mCanUI          = false;
296
       doEffectNow( BaseEffect.Type.SOLVE );
297
       }
298

    
299
     if( mScrambleCube )
300
       {
301
       mScrambleCube = false;
302
       mCanDrag      = false;
303
       mCanRotate    = false;
304
       mCanUI        = false;
305
       doEffectNow( BaseEffect.Type.SCRAMBLE );
306
       }
307
     }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
   @Override
312
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
313
      {
314
      if( mNewCube!=null ) mNewCube.createTexture();
315

    
316
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
317
      float fovInDegrees   = computeFOV(cameraDistance,height);
318

    
319
      mScreen.setProjection( fovInDegrees, 0.1f);
320
      mView.setScreenSize(width,height);
321
      mView.setCameraDist(cameraDistance);
322
      mScreen.resize(width, height);
323

    
324
      recomputeScaleFactor(width,height);
325

    
326
      mScreenHeight = height;
327
      mScreenWidth  = width;
328
      }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
   @Override
333
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
334
      {
335
      VertexEffectSink.enable();
336
      BaseEffect.Type.enableEffects();
337

    
338
      try
339
        {
340
        DistortedLibrary.onCreate(mView.getContext());
341
        }
342
      catch(Exception ex)
343
        {
344
        android.util.Log.e("Rubik", ex.getMessage() );
345
        }
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
   public void effectFinished(final long effectID)
351
     {
352
     if( effectID == mRotationFinishedID )
353
       {
354
       mRemoveRotation = true;
355
       }
356
     else
357
       {
358
       for(int i=0; i<BaseEffect.Type.LENGTH; i++)
359
         {
360
         if( effectID == mEffectID[i] )
361
           {
362
           mCanRotate   = true;
363
           mCanDrag     = true;
364
           mCanUI       = true;
365
           break;
366
           }
367
         }
368
       }
369
     }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
   public RubikCube getCube()
374
     {
375
     return mNewCube;
376
     }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
   public RubikCube getOldCube()
381
     {
382
     return mOldCube;
383
     }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
   public DistortedScreen getScreen()
388
     {
389
     return mScreen;
390
     }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
   public int getNumScrambles()
395
     {
396
     return mScrambleCubeNum;
397
     }
398
}
(4-4/10)