Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ 4235de9b

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
         }
246
       else
247
         {
248
         mCanRotate = true;
249
         mCanUI     = true;
250
         }
251

    
252
       mIsSolved = solved;
253
       }
254

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

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

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

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

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

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

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

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

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

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

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

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