Revision 483ae94e
Added by Leszek Koltunski about 5 years ago
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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
RubikApp: properly compute cameraDistance and FOV.