Project

General

Profile

« Previous | Next » 

Revision af8b42cc

Added by Leszek Koltunski about 5 years ago

Port changes from the 'distorted-cube' target.

View differences:

src/main/java/org/distorted/examples/rubik/RubikRenderer.java
49 49
    private boolean mCanRotate;
50 50
    private RubikCube mCube;
51 51

  
52
    private int mScreenWidth, mScreenHeight;
53

  
52 54
///////////////////////////////////////////////////////////////////////////////////////////////////
53 55

  
54 56
    RubikRenderer(RubikSurfaceView v)
......
62 64
      mQuatCurrent     = new Static4D(0,0,0,1);
63 65
      mQuatAccumulated = initializeQuat();
64 66

  
67
      mScreenWidth = mScreenHeight = 0;
68

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

  
......
71 75
      mFinishDragAccumulated = false;
72 76

  
73 77
      mCanRotate = true;
74

  
75
      mCube = new RubikCube( RubikActivity.DEFAULT_SIZE, mMove, mScale, mQuatCurrent, mQuatAccumulated);
76 78
      }
77 79

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

  
135
      mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(width>height ? height:width);
136
      float texSize = mCube.getTextureSize();
137
      float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
138

  
139
      mMove.set( (width-scaleFactor*texSize)/2 , (height-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
140
      mScale.set(scaleFactor,scaleFactor,scaleFactor);
138
      recomputeScaleFactor(width,height);
141 139

  
142
      mScreen.resize(width, height);
140
      mScreenHeight = height;
141
      mScreenWidth  = width;
143 142
      }
144 143

  
145 144
///////////////////////////////////////////////////////////////////////////////////////////////////
......
164 163

  
165 164
///////////////////////////////////////////////////////////////////////////////////////////////////
166 165

  
167
    private float computeFOV(float cameraDistance, int screenHeight)
168
      {
169
      double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
170
      return (float)(2*halfFOVInRadians*(180/Math.PI));
171
      }
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
     }
172 171

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

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

  
181 180
///////////////////////////////////////////////////////////////////////////////////////////////////
182 181

  
183
    float returnCubeSizeInScreenSpace()
184
      {
185
      return mCubeSizeInScreenSpace;
186
      }
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
     }
187 200

  
188 201
///////////////////////////////////////////////////////////////////////////////////////////////////
189 202

  
190
    boolean canRotate()
191
      {
192
      return mCanRotate;
193
      }
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
     }
194 212

  
195 213
///////////////////////////////////////////////////////////////////////////////////////////////////
196 214

  
197
    RubikCube getCube()
198
      {
199
      return mCube;
200
      }
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
     }
201 240

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

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

  
210 249
///////////////////////////////////////////////////////////////////////////////////////////////////
211 250

  
212
    void setQuatCurrent(Static4D current)
213
      {
214
      mTempCurrent.set(current);
215
      mFinishDragCurrent = true;
216
      }
251
   void setQuatCurrent(Static4D current)
252
     {
253
     mTempCurrent.set(current);
254
     mFinishDragCurrent = true;
255
     }
217 256

  
218 257
///////////////////////////////////////////////////////////////////////////////////////////////////
219 258

  
220
    void setQuatAccumulated(Static4D accumulated)
221
      {
222
      mTempAccumulated.set(accumulated);
223
      mFinishDragAccumulated = true;
224
      }
259
   void setQuatAccumulated(Static4D accumulated)
260
     {
261
     mTempAccumulated.set(accumulated);
262
     mFinishDragAccumulated = true;
263
     }
225 264
}

Also available in: Unified diff