Project

General

Profile

« Previous | Next » 

Revision 483ae94e

Added by Leszek Koltunski about 5 years ago

RubikApp: properly compute cameraDistance and FOV.

View differences:

src/main/java/org/distorted/examples/rubik/RubikCube.java
254 254
    private void modifyCurrentPosition(int x, int y, int z, Static4D quat)
255 255
      {
256 256
      Static3D current = mCurrentPosition[x][y][z];
257
      float beforeX = current.get1();
258
      float beforeY = current.get2();
259
      float beforeZ = current.get3();
260

  
261 257
      float diff = 0.5f*(mSize-1);
262

  
263
      float cubitCenterX = beforeX - diff;
264
      float cubitCenterY = beforeY - diff;
265
      float cubitCenterZ = beforeZ - diff;
258
      float cubitCenterX = current.get1() - diff;
259
      float cubitCenterY = current.get2() - diff;
260
      float cubitCenterZ = current.get3() - diff;
266 261

  
267 262
      Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
268 263
      Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
......
326 321

  
327 322
///////////////////////////////////////////////////////////////////////////////////////////////////
328 323

  
329
    float getWidth()
330
      {
331
      return mTexture.getWidth();
332
      }
333

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

  
336
    float getHeight()
337
      {
338
      return mTexture.getHeight();
339
      }
340

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

  
343
    float getDepth()
324
    float getTextureSize()
344 325
      {
345
      return mTexture.getDepth(mCubes[0][0][0]);
326
      return TEXTURE_SIZE;
346 327
      }
347 328

  
348 329
///////////////////////////////////////////////////////////////////////////////////////////////////
349 330

  
350
    float getSizeInModelSpace()
331
    float getSize()
351 332
      {
352
      return mSize*TEXTURE_SIZE;
333
      return mSize;
353 334
      }
354 335
}
src/main/java/org/distorted/examples/rubik/RubikRenderer.java
36 36
{
37 37
    private static final int NUM_CUBES = 4;
38 38
    private static final float CUBE_SCREEN_RATIO = 0.5f;
39
    private static final float CAMERA_DISTANCE = 0.5f;     // 0.5 of the length of max(scrHeight,scrWidth)
39 40

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

  
......
98 99
    
99 100
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
100 101
      {
101
      float fovInDegrees = (width>height ? 60.0f : 90.0f);
102
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
103
      float fovInDegrees   = computeFOV(cameraDistance,height);
102 104

  
103 105
      mScreen.setProjection( fovInDegrees, 0.1f);
104

  
105 106
      mView.setScreenSize(width,height);
106
      mView.recomputeCameraDistance(fovInDegrees*Math.PI/180);
107

  
108
      float w = mCube.getWidth();
109
      float h = mCube.getHeight();
110
      float d = mCube.getDepth();
107
      mView.setCameraDist(cameraDistance);
111 108

  
112
      mScaleFactor = CUBE_SCREEN_RATIO*(width>height ? height:width)/mCube.getSizeInModelSpace();
109
      mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(width>height ? height:width);
110
      float texSize = mCube.getTextureSize();
111
      float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
113 112

  
114
      mMove.set( (width-mScaleFactor*w)/2 , (height-mScaleFactor*h)/2 , -mScaleFactor*d/2 );
115
      mScale.set(mScaleFactor,mScaleFactor,mScaleFactor);
113
      mMove.set( (width-scaleFactor*texSize)/2 , (height-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
114
      mScale.set(scaleFactor,scaleFactor,scaleFactor);
116 115

  
117 116
      mScreen.resize(width, height);
118 117
      }
......
137 136
        }
138 137
      }
139 138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
    private float computeFOV(float cameraDistance, int screenHeight)
142
      {
143
      double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
144
      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
145

  
146
      return fovInDegrees;
147
      }
148

  
140 149
///////////////////////////////////////////////////////////////////////////////////////////////////
141 150
// no this will not race with onDrawFrame
142 151

  
......
149 158

  
150 159
    float returnCubeSizeInScreenSpace()
151 160
      {
152
      return mScaleFactor*mCube.getSizeInModelSpace();
161
      return mCubeSizeInScreenSpace;
153 162
      }
154 163

  
155 164
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
160 160

  
161 161
///////////////////////////////////////////////////////////////////////////////////////////////////
162 162

  
163
    void recomputeCameraDistance(double fovInRadians)
163
    void setCameraDist(float distance)
164 164
      {
165
      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
165
      mCameraDistance = distance;
166 166
      }
167 167

  
168 168
///////////////////////////////////////////////////////////////////////////////////////////////////
......
224 224
      }
225 225

  
226 226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
228
// given face by 240/2 = 120 degrees.
227 229

  
228 230
    private void continueRotation(int x, int y)
229 231
      {

Also available in: Unified diff