Project

General

Profile

Download (9.45 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ e3a72781

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.examples.rubik;
21

    
22
import android.opengl.GLSurfaceView;
23

    
24
import org.distorted.library.effect.VertexEffectSink;
25
import org.distorted.library.main.Distorted;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.library.message.EffectMessage;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31

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

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
42
    private RubikSurfaceView mView;
43
    private DistortedScreen mScreen;
44
    private Static3D mMove, mScale;
45
    private Static4D mQuatCurrent, mQuatAccumulated;
46
    private Static4D mTempCurrent, mTempAccumulated;
47
    private float mCubeSizeInScreenSpace;
48
    private int mNextCubeSize;
49
    private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
50
    private boolean mCanRotate;
51
    private RubikCube mCube;
52

    
53
    private int mScreenWidth, mScreenHeight;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
61
      mScreen = new DistortedScreen();
62

    
63
      mTempCurrent     = new Static4D(0,0,0,1);
64
      mTempAccumulated = initializeQuat();
65
      mQuatCurrent     = new Static4D(0,0,0,1);
66
      mQuatAccumulated = initializeQuat();
67

    
68
      mScreenWidth = mScreenHeight = 0;
69

    
70
      mMove  = new Static3D(0,0,0);
71
      mScale = new Static3D(1,1,1);
72

    
73
      mFinishRotation        = false;
74
      mRemoveRotation        = false;
75
      mFinishDragCurrent     = false;
76
      mFinishDragAccumulated = false;
77

    
78
      mNextCubeSize= 0;
79

    
80
      mCanRotate = true;
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// various things are done here delayed, 'after the next render' as not to be done mid-render and
85
// cause artifacts.
86

    
87
    public void onDrawFrame(GL10 glUnused) 
88
      {
89
      mScreen.render( System.currentTimeMillis() );
90

    
91
      if( mFinishDragCurrent )
92
        {
93
        mFinishDragCurrent = false;
94
        mQuatCurrent.set(mTempCurrent);
95
        }
96

    
97
      if( mFinishDragAccumulated )
98
        {
99
        mFinishDragAccumulated = false;
100
        mQuatAccumulated.set(mTempAccumulated);
101
        }
102

    
103
      if( mFinishRotation )
104
        {
105
        mCanRotate = false;
106
        mFinishRotation=false;
107
        mCube.finishRotationNow(this);
108
        }
109

    
110
      if( mRemoveRotation )
111
        {
112
        mRemoveRotation=false;
113
        mCube.removeRotationNow(this);
114
        mCanRotate = true;
115
        }
116

    
117
      if( mNextCubeSize!=0 )
118
        {
119
        createCubeNow(mNextCubeSize);
120
        mScreen.detachAll();
121
        mCube.attachToScreen(mScreen);
122
        mNextCubeSize = 0;
123
        }
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// EffectListener. The library sends a message to us when it's time to call 'removeRotation'
128

    
129
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
130
     {
131
     switch(em)
132
        {
133
        case EFFECT_FINISHED: mRemoveRotation = true; break;
134
        }
135
     }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
140
      {
141
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
142
      float fovInDegrees   = computeFOV(cameraDistance,height);
143

    
144
      mScreen.setProjection( fovInDegrees, 0.1f);
145
      mView.setScreenSize(width,height);
146
      mView.setCameraDist(cameraDistance);
147
      mScreen.resize(width, height);
148

    
149
      recomputeScaleFactor(width,height);
150

    
151
      mScreenHeight = height;
152
      mScreenWidth  = width;
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
    
157
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
158
      {
159
      mCube.createTexture();
160
      mScreen.detachAll();
161
      mCube.attachToScreen(mScreen);
162

    
163
      VertexEffectSink.enable();
164

    
165
      try
166
        {
167
        Distorted.onCreate(mView.getContext());
168
        }
169
      catch(Exception ex)
170
        {
171
        android.util.Log.e("Rubik", ex.getMessage() );
172
        }
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
   private float computeFOV(float cameraDistance, int screenHeight)
178
     {
179
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
180
     return (float)(2*halfFOVInRadians*(180/Math.PI));
181
     }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// no this will not race with onDrawFrame
185

    
186
   void finishRotation()
187
     {
188
     mFinishRotation = true;
189
     }
190

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

    
193
   void createCube(int newSize)
194
     {
195
     mNextCubeSize = newSize;
196
     }
197

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

    
200
   void createCubeNow(int newSize)
201
     {
202
     int oldSize = mCube==null ? 0 : mCube.getSize();
203

    
204
     if( oldSize!=newSize )
205
       {
206
       if( mCube!=null ) mCube.releaseResources();
207
       mCube = new RubikCube(newSize, mMove, mScale, mQuatCurrent, mQuatAccumulated);
208
       mCube.createTexture();
209

    
210
       if( mScreenWidth!=0 )
211
         {
212
         recomputeScaleFactor(mScreenWidth,mScreenHeight);
213
         }
214
       }
215
     }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
220
     {
221
     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
222
     float texSize = mCube.getTextureSize();
223
     float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
224

    
225
     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
226
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
227
     }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
   void scrambleCube()
232
     {
233

    
234
     }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
   float returnCubeSizeInScreenSpace()
239
     {
240
     return mCubeSizeInScreenSpace;
241
     }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
   boolean canRotate()
246
     {
247
     return mCanRotate;
248
     }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
   RubikCube getCube()
253
     {
254
     return mCube;
255
     }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
// Initial rotation of the cube. Something semi-random that looks good.
259

    
260
   Static4D initializeQuat()
261
     {
262
     return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
263
     }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
   void setQuatCurrent(Static4D current)
268
     {
269
     mTempCurrent.set(current);
270
     mFinishDragCurrent = true;
271
     }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
   void setQuatAccumulated(Static4D accumulated)
276
     {
277
     mTempAccumulated.set(accumulated);
278
     mFinishDragAccumulated = true;
279
     }
280
}
(3-3/4)