Project

General

Profile

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

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

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 boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
49
    private boolean mCanRotate;
50
    private RubikCube mCube;
51

    
52
    private int mScreenWidth, mScreenHeight;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
60
      mScreen = new DistortedScreen();
61

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

    
67
      mScreenWidth = mScreenHeight = 0;
68

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

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

    
77
      mCanRotate = true;
78
      }
79

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

    
84
    public void onDrawFrame(GL10 glUnused) 
85
      {
86
      mScreen.render( System.currentTimeMillis() );
87

    
88
      if( mFinishDragCurrent )
89
        {
90
        mFinishDragCurrent = false;
91
        mQuatCurrent.set(mTempCurrent);
92
        }
93

    
94
      if( mFinishDragAccumulated )
95
        {
96
        mFinishDragAccumulated = false;
97
        mQuatAccumulated.set(mTempAccumulated);
98
        }
99

    
100
      if( mFinishRotation )
101
        {
102
        mCanRotate = false;
103
        mFinishRotation=false;
104
        mCube.finishRotationCalledOnNextRender(this);
105
        }
106

    
107
      if( mRemoveRotation )
108
        {
109
        mRemoveRotation=false;
110
        mCube.removeRotationCalledOnNextRender(this);
111
        mCanRotate = true;
112
        }
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// EffectListener. The library sends a message to us when it's time to call 'removeRotation'
117

    
118
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
119
     {
120
     switch(em)
121
        {
122
        case EFFECT_FINISHED: mRemoveRotation = true; break;
123
        }
124
     }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
129
      {
130
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
131
      float fovInDegrees   = computeFOV(cameraDistance,height);
132

    
133
      mScreen.setProjection( fovInDegrees, 0.1f);
134
      mView.setScreenSize(width,height);
135
      mView.setCameraDist(cameraDistance);
136
      mScreen.resize(width, height);
137

    
138
      recomputeScaleFactor(width,height);
139

    
140
      mScreenHeight = height;
141
      mScreenWidth  = width;
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
147
      {
148
      mCube.createTexture();
149
      mScreen.detachAll();
150
      mCube.attachToScreen(mScreen);
151

    
152
      VertexEffectSink.enable();
153

    
154
      try
155
        {
156
        Distorted.onCreate(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Rubik", ex.getMessage() );
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   private float computeFOV(float cameraDistance, int screenHeight)
167
     {
168
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
169
     return (float)(2*halfFOVInRadians*(180/Math.PI));
170
     }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// no this will not race with onDrawFrame
174

    
175
   void finishRotation()
176
     {
177
     mFinishRotation = true;
178
     }
179

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

    
182
   void createCube(int newSize)
183
     {
184
     int oldSize = mCube==null ? 0 : mCube.getSize();
185

    
186
     if( oldSize!=newSize )
187
       {
188
       mCube = new RubikCube(newSize, mMove, mScale, mQuatCurrent, mQuatAccumulated);
189
       mCube.createTexture();
190

    
191
       if( mScreenWidth!=0 )
192
         {
193
         recomputeScaleFactor(mScreenWidth,mScreenHeight);
194
         }
195

    
196
       mScreen.detachAll();
197
       mCube.attachToScreen(mScreen);
198
       }
199
     }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
   void recomputeScaleFactor(int screenWidth, int screenHeight)
204
     {
205
     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
206
     float texSize = mCube.getTextureSize();
207
     float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
208

    
209
     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
210
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
211
     }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
   void scrambleCube()
216
     {
217

    
218
     }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
   float returnCubeSizeInScreenSpace()
223
     {
224
     return mCubeSizeInScreenSpace;
225
     }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
   boolean canRotate()
230
     {
231
     return mCanRotate;
232
     }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
   RubikCube getCube()
237
     {
238
     return mCube;
239
     }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// Initial rotation of the cube. Something semi-random that looks good.
243

    
244
   Static4D initializeQuat()
245
     {
246
     return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
247
     }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
   void setQuatCurrent(Static4D current)
252
     {
253
     mTempCurrent.set(current);
254
     mFinishDragCurrent = true;
255
     }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
   void setQuatAccumulated(Static4D accumulated)
260
     {
261
     mTempAccumulated.set(accumulated);
262
     mFinishDragAccumulated = true;
263
     }
264
}
(3-3/4)