Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ 45686da2

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  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, mFinishDragCurrent, mFinishDragAccumulated, mSolveCube, mScrambleCube;
51
    private boolean mCanRotate, mCanDrag, mCanUI;
52
    private RubikCube mOldCube, mNewCube;
53
    private int mScreenWidth, mScreenHeight;
54
    private MeshFlat mMesh;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
    RubikRenderer(RubikSurfaceView v)
59
      {
60
      mView = v;
61

    
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
      mFinishDragCurrent     = false;
73
      mFinishDragAccumulated = false;
74
      mSolveCube             = false;
75
      mScrambleCube          = false;
76

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

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

    
83
      mMesh= new MeshFlat(20,20);
84
      mNextCubeSize =RubikActivity.getSize();
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// various things are done here delayed, 'after the next render' as not to be done mid-render and
89
// cause artifacts.
90

    
91
    @Override
92
    public void onDrawFrame(GL10 glUnused) 
93
      {
94
      mScreen.render( System.currentTimeMillis() );
95

    
96
      if( mFinishDragCurrent )
97
        {
98
        mFinishDragCurrent = false;
99
        mView.setQuatCurrent();
100
        }
101

    
102
      if( mFinishDragAccumulated )
103
        {
104
        mFinishDragAccumulated = false;
105
        mView.setQuatAccumulated();
106
        }
107

    
108
      if( mFinishRotation )
109
        {
110
        mCanRotate = false;
111
        mFinishRotation=false;
112
        mRotationFinishedID = mNewCube.finishRotationNow(this);
113
        }
114

    
115
      if( mRemoveRotation )
116
        {
117
        mRemoveRotation=false;
118
        mNewCube.removeRotationNow();
119
        mCanRotate = true;
120
        }
121

    
122
      if( mNextCubeSize!=0 )
123
        {
124
        createCubeNow(mNextCubeSize);
125
        mCanDrag   = false;
126
        mCanRotate = false;
127
        mNextCubeSize = 0;
128
        doEffectNow(0);
129
        }
130

    
131
      if( mSolveCube )
132
        {
133
        mSolveCube   = false;
134
        mCanDrag     = false;
135
        mCanRotate   = false;
136
        mCanUI       = false;
137
        doEffectNow(1);
138
        }
139

    
140
      if( mScrambleCube )
141
        {
142
        mScrambleCube = false;
143
        mCanDrag      = false;
144
        mCanRotate    = false;
145
        mCanUI        = false;
146
        doEffectNow(2);
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
   @Override
153
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
154
      {
155
      if( mNewCube!=null ) mNewCube.createTexture();
156

    
157
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
158
      float fovInDegrees   = computeFOV(cameraDistance,height);
159

    
160
      mScreen.setProjection( fovInDegrees, 0.1f);
161
      mView.setScreenSize(width,height);
162
      mView.setCameraDist(cameraDistance);
163
      mScreen.resize(width, height);
164

    
165
      recomputeScaleFactor(width,height);
166

    
167
      mScreenHeight = height;
168
      mScreenWidth  = width;
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
   @Override
174
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
175
      {
176
      VertexEffectSink.enable();
177
      BaseEffect.Type.enableEffects();
178

    
179
      try
180
        {
181
        DistortedLibrary.onCreate(mView.getContext());
182
        }
183
      catch(Exception ex)
184
        {
185
        android.util.Log.e("Rubik", ex.getMessage() );
186
        }
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
   public void effectFinished(final long effectID)
192
     {
193
     if( effectID == mRotationFinishedID )
194
       {
195
       mRemoveRotation = true;
196
       }
197
     else
198
       {
199
       for(int i=0; i<BaseEffect.Type.LENGTH; i++)
200
         {
201
         if( effectID == mEffectID[i] )
202
           {
203
           mCanRotate   = true;
204
           mCanDrag     = true;
205
           mCanUI       = true;
206
           break;
207
           }
208
         }
209
       }
210
     }
211

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

    
214
   private float computeFOV(float cameraDistance, int screenHeight)
215
     {
216
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
217
     return (float)(2*halfFOVInRadians*(180/Math.PI));
218
     }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
// no this will not race with onDrawFrame
222

    
223
   void finishRotation()
224
     {
225
     mFinishRotation = true;
226
     }
227

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

    
230
   boolean createCube(int newSize)
231
     {
232
     if( mCanDrag && mCanRotate && (mNewCube==null || newSize != mNewCube.getSize()) )
233
       {
234
       mNextCubeSize = newSize;
235
       return true;
236
       }
237

    
238
     return false;
239
     }
240

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

    
243
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
244
     {
245
     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
246

    
247
     if( mNewCube!=null )
248
       {
249
       mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace);
250
       }
251
     }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
   void scrambleCube(int num)
256
     {
257
     if( mCanUI )
258
       {
259
       mScrambleCube = true;
260
       mScrambleCubeNum = num;
261
       }
262
     }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
   void solveCube()
267
     {
268
     if( mCanUI )
269
       {
270
       mSolveCube = true;
271
       }
272
     }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
   private void createCubeNow(int newSize)
277
     {
278
     if( mOldCube!=null ) mOldCube.releaseResources();
279
     mOldCube = mNewCube;
280

    
281
     DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
282
     DistortedEffects effects = new DistortedEffects();
283

    
284
     mNewCube = new RubikCube(newSize, mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects);
285
     mNewCube.createTexture();
286

    
287
     if( mScreenWidth!=0 )
288
       {
289
       recomputeScaleFactor(mScreenWidth,mScreenHeight);
290
       }
291
     }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
   private void doEffectNow(int index)
296
     {
297
     mEffectID[index] = BaseEffect.Type.getType(index).startEffect(this);
298

    
299
     if( mEffectID[index] == -1 )
300
       {
301
       mCanRotate = true;
302
       mCanDrag   = true;
303
       }
304
     }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
   float returnCubeSizeInScreenSpace()
309
     {
310
     return mCubeSizeInScreenSpace;
311
     }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
   boolean canRotate()
316
     {
317
     return mCanRotate;
318
     }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
   boolean canDrag()
323
     {
324
     return mCanDrag;
325
     }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
   public RubikCube getCube()
330
     {
331
     return mNewCube;
332
     }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
   public RubikCube getOldCube()
337
     {
338
     return mOldCube;
339
     }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
   public DistortedScreen getScreen()
344
     {
345
     return mScreen;
346
     }
347

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

    
350
   public int getNumScrambles()
351
     {
352
     return mScrambleCubeNum;
353
     }
354

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

    
357
   void setQuatCurrentOnNextRender()
358
     {
359
     mFinishDragCurrent = true;
360
     }
361

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

    
364
   void setQuatAccumulatedOnNextRender()
365
     {
366
     mFinishDragAccumulated = true;
367
     }
368
}
(4-4/6)