Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ 0fd3ac1f

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
import org.distorted.object.RubikCube;
33

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

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

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

    
59
    RubikRenderer(RubikSurfaceView v)
60
      {
61
      mView = v;
62
      mScreen = new DistortedScreen();
63

    
64
      mOldCube = null;
65
      mNewCube = null;
66

    
67
      mScreenWidth = mScreenHeight = 0;
68
      mScrambleCubeNum = 0;
69

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

    
78
      mCanRotate   = true;
79
      mCanDrag     = true;
80
      mCanUI       = true;
81

    
82
      mEffectID = new long[BaseEffect.Type.LENGTH];
83

    
84
      mMesh= new MeshFlat(20,20);
85
      mNextCubeSize = RubikSize.getSize(RubikSurfaceView.getRedButton()).getCubeSize();
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
   private void createCubeNow(int newSize)
91
     {
92
     if( mOldCube!=null ) mOldCube.releaseResources();
93
     mOldCube = mNewCube;
94

    
95
     DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
96
     DistortedEffects effects = new DistortedEffects();
97

    
98
     mNewCube = new RubikCube(newSize, mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects);
99
     mNewCube.createTexture();
100

    
101
     if( mScreenWidth!=0 )
102
       {
103
       mNewCube.recomputeScaleFactor(mScreenWidth, mScreenHeight);
104
       }
105

    
106
     mIsSolved = true;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
111

    
112
   private void doEffectNow(BaseEffect.Type type)
113
     {
114
     int index = type.ordinal();
115

    
116
     try
117
       {
118
       mEffectID[index] = type.startEffect(this);
119
       }
120
     catch( Exception ex )
121
       {
122
       android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
123

    
124
       mCanUI     = true;
125
       mCanRotate = true;
126
       mCanDrag   = true;
127
       }
128
     }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
// no this will not race with onDrawFrame
132

    
133
   void finishRotation()
134
     {
135
     mFinishRotation = true;
136
     }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
   boolean createCube(int newSize)
141
     {
142
     if( mCanDrag && mCanRotate && (mNewCube==null || newSize != mNewCube.getSize()) )
143
       {
144
       mSizeChangeCube = true;
145
       mNextCubeSize = newSize;
146
       return true;
147
       }
148

    
149
     return false;
150
     }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
   void scrambleCube(int num)
155
     {
156
     if( mCanUI )
157
       {
158
       mScrambleCube = true;
159
       mScrambleCubeNum = num;
160
       }
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
   void solveCube()
166
     {
167
     if( mCanUI )
168
       {
169
       mSolveCube = true;
170
       }
171
     }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
   boolean canRotate()
176
     {
177
     return mCanRotate;
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
   boolean canDrag()
183
     {
184
     return mCanDrag;
185
     }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
   void setQuatCurrentOnNextRender()
190
     {
191
     mSetQuatCurrent = true;
192
     }
193

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

    
196
   void setQuatAccumulatedOnNextRender()
197
     {
198
     mSetQuatAccumulated = true;
199
     }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// PUBLIC API
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// various things are done here delayed, 'after the next render' as not to be done mid-render and
205
// cause artifacts.
206

    
207
   @Override
208
   public void onDrawFrame(GL10 glUnused)
209
     {
210
     mScreen.render( System.currentTimeMillis() );
211

    
212
     if( mSetQuatCurrent )
213
       {
214
       mSetQuatCurrent = false;
215
       mView.setQuatCurrent();
216
       }
217

    
218
     if( mSetQuatAccumulated )
219
       {
220
       mSetQuatAccumulated = false;
221
       mView.setQuatAccumulated();
222
       }
223

    
224
     if( mFinishRotation )
225
       {
226
       mFinishRotation = false;
227
       mCanRotate      = false;
228
       mCanUI          = false;
229
       mRotationFinishedID = mNewCube.finishRotationNow(this);
230
       }
231

    
232
     if( mRemoveRotation )
233
       {
234
       mRemoveRotation=false;
235
       mNewCube.removeRotationNow();
236

    
237
       boolean solved = mNewCube.isSolved();
238

    
239
       if( solved && !mIsSolved )
240
         {
241
         mCanDrag        = true;
242
         mCanRotate      = false;
243
         mCanUI          = false;
244
         doEffectNow( BaseEffect.Type.WIN );
245
         mView.leaveScrambleModeNonUI();
246
         }
247
       else
248
         {
249
         mCanRotate = true;
250
         mCanUI     = true;
251
         }
252

    
253
       mIsSolved = solved;
254
       }
255

    
256
     if( mSizeChangeCube )
257
       {
258
       mSizeChangeCube = false;
259
       mCanDrag        = false;
260
       mCanRotate      = false;
261
       mCanUI          = false;
262
       createCubeNow(mNextCubeSize);
263
       doEffectNow( BaseEffect.Type.SIZECHANGE );
264
       }
265

    
266
     if( mSolveCube )
267
       {
268
       mSolveCube      = false;
269
       mCanDrag        = false;
270
       mCanRotate      = false;
271
       mCanUI          = false;
272
       doEffectNow( BaseEffect.Type.SOLVE );
273
       }
274

    
275
     if( mScrambleCube )
276
       {
277
       mScrambleCube = false;
278
       mCanDrag      = false;
279
       mCanRotate    = false;
280
       mCanUI        = false;
281
       doEffectNow( BaseEffect.Type.SCRAMBLE );
282
       }
283
     }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
   @Override
288
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
289
      {
290
      if( mNewCube!=null ) mNewCube.createTexture();
291

    
292
      double halfFOVInRadians = Math.atan( 1.0f/(2*CAMERA_DISTANCE) );
293
      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
294

    
295
      mScreen.setProjection( fovInDegrees, 0.1f);
296
      mScreen.resize(width, height);
297
      mView.setScreenSize(width,height);
298

    
299
      if( mNewCube!=null )
300
        {
301
        mNewCube.recomputeScaleFactor(width,height);
302
        }
303

    
304
      mScreenHeight = height;
305
      mScreenWidth  = width;
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
   @Override
311
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
312
      {
313
      VertexEffectSink.enable();
314
      BaseEffect.Type.enableEffects();
315

    
316
      try
317
        {
318
        DistortedLibrary.onCreate(mView.getContext());
319
        }
320
      catch(Exception ex)
321
        {
322
        android.util.Log.e("Rubik", ex.getMessage() );
323
        }
324
      }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
   public void effectFinished(final long effectID)
329
     {
330
     if( effectID == mRotationFinishedID )
331
       {
332
       mRemoveRotation = true;
333
       }
334
     else
335
       {
336
       for(int i=0; i<BaseEffect.Type.LENGTH; i++)
337
         {
338
         if( effectID == mEffectID[i] )
339
           {
340
           mCanRotate   = true;
341
           mCanDrag     = true;
342
           mCanUI       = true;
343
           break;
344
           }
345
         }
346
       }
347
     }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
   public RubikCube getCube()
352
     {
353
     return mNewCube;
354
     }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
   public RubikCube getOldCube()
359
     {
360
     return mOldCube;
361
     }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
   public DistortedScreen getScreen()
366
     {
367
     return mScreen;
368
     }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
   public int getNumScrambles()
373
     {
374
     return mScrambleCubeNum;
375
     }
376
}
(8-8/11)