Project

General

Profile

« Previous | Next » 

Revision 18709ae0

Added by Leszek Koltunski over 5 years ago

Improve the architecture of the Rubik App.

View differences:

src/main/java/org/distorted/examples/rubik/RubikRenderer.java
37 37
    static final int NUM_CUBES = 4;
38 38
    private static final float CUBE_SCREEN_RATIO = 0.5f;
39 39

  
40
    private GLSurfaceView mView;
40
    private RubikSurfaceView mView;
41 41
    private DistortedScreen mScreen;
42 42
    private Static3D mMove, mScale;
43 43
    private Static4D mQuatCurrent, mQuatAccumulated;
44 44
    private Static4D mTempCurrent, mTempAccumulated;
45 45
    private float mScaleFactor;
46
    private int mScreenWidth, mScreenHeight;
47 46
    private boolean mFinishRotation, mFinishDragCurrent, mFinishDragAccumulated;
48 47
    private RubikCube mCube;
49 48

  
50 49
///////////////////////////////////////////////////////////////////////////////////////////////////
51 50

  
52
    RubikRenderer(GLSurfaceView v)
51
    RubikRenderer(RubikSurfaceView v)
53 52
      {
54 53
      mView = v;
55 54

  
......
101 100
      {
102 101
      mScreen.setProjection( width>height ? 60.0f : 90.0f, 0.1f);
103 102

  
104
      mScreenWidth = width;
105
      mScreenHeight= height;
103
      mView.setScreenSize(width,height);
104
      mView.recomputeCameraDistance();
106 105

  
107 106
      float w = mCube.getWidth();
108 107
      float h = mCube.getHeight();
......
146 145

  
147 146
///////////////////////////////////////////////////////////////////////////////////////////////////
148 147

  
149
    float returnCameraDistance()
148
    float getFOVInRadians()
150 149
      {
151
      float fov = mScreen.getFOV();
152
      double fovInRadians = (fov*Math.PI) / 180.0;
153
      double distance = (mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5);
154

  
155
      return (float)distance;
150
      return (float)((mScreen.getFOV()*Math.PI) / 180.0);
156 151
      }
157 152

  
158 153
///////////////////////////////////////////////////////////////////////////////////////////////////
......
163 158
      return mScaleFactor*NUM_CUBES*RubikCube.TEXTURE_SIZE;
164 159
      }
165 160

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
    float getScreenWidth()
169
      {
170
      return mScreenWidth;
171
      }
172

  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

  
175
    float getScreenHeight()
176
      {
177
      return mScreenHeight;
178
      }
179

  
180 161
///////////////////////////////////////////////////////////////////////////////////////////////////
181 162

  
182 163
    RubikCube getCube()
......
184 165
      return mCube;
185 166
      }
186 167

  
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

  
189
    int getScreenMin()
190
      {
191
      return mScreenWidth<mScreenHeight ? mScreenWidth:mScreenHeight;
192
      }
193

  
194 168
///////////////////////////////////////////////////////////////////////////////////////////////////
195 169
// Initial rotation of the cube. Something semi-random that looks good.
196 170

  
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
59 59
    private float mPoiX, mPoiY, mPoiZ, mCamX, mCamY, mCamZ;
60 60
    private float mStartX, mStartY, mStartZ;
61 61
    private int mLastTouchedFace;
62
    private int mScreenWidth, mScreenHeight, mScreenMin;
63
    private float mCameraDistance;
62 64

  
63 65
///////////////////////////////////////////////////////////////////////////////////////////////////
64 66

  
......
70 72
      mRotating = false;
71 73
      mRotationVect = VECTN;
72 74

  
75
      mScreenWidth = mScreenHeight = mScreenMin = 0;
76

  
73 77
      mRenderer = new RubikRenderer(this);
74 78
      mCube = mRenderer.getCube();
75 79

  
......
115 119
                                         }
116 120
                                       else if( mRotating )
117 121
                                         {
118
                                         int screenMin = mRenderer.getScreenMin();
119
                                         int minimumToRotate = (screenMin*screenMin)/64;
122
                                         int minimumToRotate = (mScreenMin*mScreenMin)/64;
120 123

  
121 124
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
122 125
                                           {
......
244 247
      float dY = mY-y;
245 248
      float calibration = (float)Math.sqrt(dX*dX+dY*dY);
246 249

  
247
      mCube.continueRotation(calibration*sign, mRenderer.getScreenMin());
250
      mCube.continueRotation(calibration*sign, mScreenMin);
248 251
      }
249 252

  
250 253
///////////////////////////////////////////////////////////////////////////////////////////////////
......
341 344
        axisY /= axisL;
342 345
        axisZ /= axisL;
343 346

  
344
        float cosA = (float)Math.cos(axisL*Math.PI/mRenderer.getScreenMin());
347
        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
345 348
        float sinA = (float)Math.sqrt(1-cosA*cosA);
346 349

  
347 350
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
......
354 357

  
355 358
    private boolean faceIsVisible(int face)
356 359
      {
357
      float cameraDistance = mRenderer.returnCameraDistance();
358 360
      float cubeHalfSize   = mRenderer.returnCubeSize()*0.5f;
359 361

  
360
      Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,cameraDistance,0), mQuatAccumulated);
362
      Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,mCameraDistance,0), mQuatAccumulated);
361 363

  
362 364
      switch(face)
363 365
        {
......
406 408

  
407 409
    void fillTouchPoint(int x, int y)
408 410
      {
409
      float halfScrWidth  = mRenderer.getScreenWidth() *0.5f;
410
      float halfScrHeight = mRenderer.getScreenHeight()*0.5f;
411
      float halfScrWidth  = mScreenWidth *0.5f;
412
      float halfScrHeight = mScreenHeight*0.5f;
411 413
      Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
412 414
      Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
413 415

  
......
420 422

  
421 423
    void fillCamera()
422 424
      {
423
      float cameraDistance = mRenderer.returnCameraDistance();
424

  
425
      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0);
425
      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
426 426
      Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
427 427

  
428 428
      mCamX = rotatedCamera.get1();
......
463 463

  
464 464
      return false;
465 465
      }
466

  
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

  
469
    void setScreenSize(int width, int height)
470
      {
471
      mScreenWidth = width;
472
      mScreenHeight= height;
473

  
474
      mScreenMin = width<height ? width:height;
475
      }
476

  
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

  
479
    void recomputeCameraDistance()
480
      {
481
      double fovInRadians = mRenderer.getFOVInRadians();
482
      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
483
      }
466 484
}
467 485

  

Also available in: Unified diff