Revision 6e18bd32
Added by Leszek Koltunski about 5 years ago
src/main/java/org/distorted/examples/rubik/RubikRenderer.java | ||
---|---|---|
38 | 38 |
{ |
39 | 39 |
private static final float CUBE_SCREEN_RATIO = 0.5f; |
40 | 40 |
private static final float CAMERA_DISTANCE = 0.6f; // 0.6 of the length of max(scrHeight,scrWidth) |
41 |
private static final int MIN_CUBE_SIZE = 1; |
|
42 |
private static final int MAX_CUBE_SIZE = 6; |
|
41 | 43 |
|
42 | 44 |
private RubikSurfaceView mView; |
43 | 45 |
private DistortedScreen mScreen; |
44 | 46 |
private Static3D mMove, mScale; |
45 |
private Static4D mQuatCurrent, mQuatAccumulated; |
|
46 |
private Static4D mTempCurrent, mTempAccumulated; |
|
47 |
private float mCubeSizeInScreenSpace; |
|
48 | 47 |
private int mNextCubeSize; |
49 |
private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
|
|
50 |
private boolean mCanRotate;
|
|
48 |
private boolean mChangeCubeSizeNow;
|
|
49 |
private int mNumCube;
|
|
51 | 50 |
private RubikCube mCube; |
52 | 51 |
|
53 | 52 |
private int mScreenWidth, mScreenHeight; |
... | ... | |
60 | 59 |
|
61 | 60 |
mScreen = new DistortedScreen(); |
62 | 61 |
|
63 |
mTempCurrent = new Static4D(0,0,0,1); |
|
64 |
mTempAccumulated = initializeQuat(); |
|
65 |
mQuatCurrent = new Static4D(0,0,0,1); |
|
66 |
mQuatAccumulated = initializeQuat(); |
|
67 |
|
|
68 | 62 |
mScreenWidth = mScreenHeight = 0; |
69 | 63 |
|
70 | 64 |
mMove = new Static3D(0,0,0); |
71 | 65 |
mScale = new Static3D(1,1,1); |
72 | 66 |
|
73 |
mFinishRotation = false; |
|
74 |
mRemoveRotation = false; |
|
75 |
mFinishDragCurrent = false; |
|
76 |
mFinishDragAccumulated = false; |
|
77 |
|
|
78 |
mNextCubeSize= 0; |
|
67 |
mNextCubeSize= MIN_CUBE_SIZE; |
|
68 |
mChangeCubeSizeNow = false; |
|
79 | 69 |
|
80 |
mCanRotate = true;
|
|
70 |
mNumCube = 0;
|
|
81 | 71 |
} |
82 | 72 |
|
83 | 73 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
84 |
// various things are done here delayed, 'after the next render' as not to be done mid-render and |
|
85 |
// cause artifacts. |
|
74 |
// change the cube size here, 'after the next render' as not to cause artifacts. |
|
86 | 75 |
|
87 | 76 |
public void onDrawFrame(GL10 glUnused) |
88 | 77 |
{ |
89 | 78 |
mScreen.render( System.currentTimeMillis() ); |
90 | 79 |
|
91 |
if( mFinishDragCurrent ) |
|
92 |
{ |
|
93 |
mFinishDragCurrent = false; |
|
94 |
mQuatCurrent.set(mTempCurrent); |
|
95 |
} |
|
96 |
|
|
97 |
if( mFinishDragAccumulated ) |
|
98 |
{ |
|
99 |
mFinishDragAccumulated = false; |
|
100 |
mQuatAccumulated.set(mTempAccumulated); |
|
101 |
} |
|
102 |
|
|
103 |
if( mFinishRotation ) |
|
80 |
if( mChangeCubeSizeNow ) |
|
104 | 81 |
{ |
105 |
mCanRotate = false; |
|
106 |
mFinishRotation=false; |
|
107 |
mCube.finishRotationNow(this); |
|
108 |
} |
|
109 |
|
|
110 |
if( mRemoveRotation ) |
|
111 |
{ |
|
112 |
mRemoveRotation=false; |
|
113 |
mCube.removeRotationNow(this); |
|
114 |
mCanRotate = true; |
|
115 |
} |
|
116 |
|
|
117 |
if( mNextCubeSize!=0 ) |
|
118 |
{ |
|
119 |
createCubeNow(mNextCubeSize); |
|
120 |
mScreen.detachAll(); |
|
121 |
mCube.attachToScreen(mScreen); |
|
122 |
mNextCubeSize = 0; |
|
123 |
mCanRotate = true; // it can happen that we have just changed the size of the cube while |
|
124 |
// finishing rotation and never removed it! |
|
82 |
mChangeCubeSizeNow = false; |
|
83 |
createNextCube(); |
|
125 | 84 |
} |
126 | 85 |
} |
127 | 86 |
|
... | ... | |
132 | 91 |
{ |
133 | 92 |
switch(em) |
134 | 93 |
{ |
135 |
case EFFECT_FINISHED: mRemoveRotation = true; break; |
|
94 |
case EFFECT_FINISHED: mNextCubeSize++; |
|
95 |
if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE; |
|
96 |
mChangeCubeSizeNow = true; |
|
97 |
break; |
|
136 | 98 |
} |
137 | 99 |
} |
138 | 100 |
|
... | ... | |
144 | 106 |
float fovInDegrees = computeFOV(cameraDistance,height); |
145 | 107 |
|
146 | 108 |
mScreen.setProjection( fovInDegrees, 0.1f); |
147 |
mView.setScreenSize(width,height); |
|
148 |
mView.setCameraDist(cameraDistance); |
|
149 | 109 |
mScreen.resize(width, height); |
150 | 110 |
|
151 | 111 |
recomputeScaleFactor(width,height); |
... | ... | |
158 | 118 |
|
159 | 119 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
160 | 120 |
{ |
161 |
mCube.createTexture(); |
|
162 |
mScreen.detachAll(); |
|
163 |
mCube.attachToScreen(mScreen); |
|
121 |
createNextCube(); |
|
164 | 122 |
|
165 | 123 |
VertexEffectSink.enable(); |
166 | 124 |
|
... | ... | |
176 | 134 |
|
177 | 135 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
178 | 136 |
|
179 |
private float computeFOV(float cameraDistance, int screenHeight) |
|
180 |
{ |
|
181 |
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) ); |
|
182 |
return (float)(2*halfFOVInRadians*(180/Math.PI)); |
|
183 |
} |
|
184 |
|
|
185 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
186 |
// no this will not race with onDrawFrame |
|
187 |
|
|
188 |
void finishRotation() |
|
137 |
private void createNextCube() |
|
189 | 138 |
{ |
190 |
mFinishRotation = true; |
|
191 |
} |
|
139 |
if( mCube!=null ) mCube.releaseResources(); |
|
140 |
mCube = new RubikCube(mNextCubeSize, mMove, mScale); |
|
141 |
mCube.createTexture(); |
|
192 | 142 |
|
193 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
143 |
if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
|
|
194 | 144 |
|
195 |
void createCube(int newSize) |
|
196 |
{ |
|
197 |
mNextCubeSize = newSize; |
|
145 |
mScreen.detachAll(); |
|
146 |
mCube.attachToScreen(mScreen); |
|
147 |
mCube.addRotation(this); |
|
148 |
RubikActivity act = mView.getRubikActivity(); |
|
149 |
act.setText(++mNumCube); |
|
198 | 150 |
} |
199 | 151 |
|
200 | 152 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
201 | 153 |
|
202 |
void createCubeNow(int newSize)
|
|
154 |
private float computeFOV(float cameraDistance, int screenHeight)
|
|
203 | 155 |
{ |
204 |
int oldSize = mCube==null ? 0 : mCube.getSize(); |
|
205 |
|
|
206 |
if( oldSize!=newSize ) |
|
207 |
{ |
|
208 |
if( mCube!=null ) mCube.releaseResources(); |
|
209 |
mCube = new RubikCube(newSize, mMove, mScale, mQuatCurrent, mQuatAccumulated); |
|
210 |
mCube.createTexture(); |
|
211 |
|
|
212 |
if( mScreenWidth!=0 ) |
|
213 |
{ |
|
214 |
recomputeScaleFactor(mScreenWidth,mScreenHeight); |
|
215 |
} |
|
216 |
} |
|
156 |
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) ); |
|
157 |
return (float)(2*halfFOVInRadians*(180/Math.PI)); |
|
217 | 158 |
} |
218 | 159 |
|
219 | 160 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
220 | 161 |
|
221 | 162 |
private void recomputeScaleFactor(int screenWidth, int screenHeight) |
222 | 163 |
{ |
223 |
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
|
|
164 |
float cubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
|
|
224 | 165 |
float texSize = mCube.getTextureSize(); |
225 |
float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
|
|
166 |
float scaleFactor = cubeSizeInScreenSpace/(texSize*mCube.getSize());
|
|
226 | 167 |
|
227 | 168 |
mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 ); |
228 | 169 |
mScale.set(scaleFactor,scaleFactor,scaleFactor); |
229 | 170 |
} |
230 |
|
|
231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
232 |
|
|
233 |
void scrambleCube() |
|
234 |
{ |
|
235 |
|
|
236 |
} |
|
237 |
|
|
238 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
239 |
|
|
240 |
float returnCubeSizeInScreenSpace() |
|
241 |
{ |
|
242 |
return mCubeSizeInScreenSpace; |
|
243 |
} |
|
244 |
|
|
245 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
246 |
|
|
247 |
boolean canRotate() |
|
248 |
{ |
|
249 |
return mCanRotate; |
|
250 |
} |
|
251 |
|
|
252 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
253 |
|
|
254 |
RubikCube getCube() |
|
255 |
{ |
|
256 |
return mCube; |
|
257 |
} |
|
258 |
|
|
259 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
260 |
// Initial rotation of the cube. Something semi-random that looks good. |
|
261 |
|
|
262 |
Static4D initializeQuat() |
|
263 |
{ |
|
264 |
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); |
|
265 |
} |
|
266 |
|
|
267 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
268 |
|
|
269 |
void setQuatCurrent(Static4D current) |
|
270 |
{ |
|
271 |
mTempCurrent.set(current); |
|
272 |
mFinishDragCurrent = true; |
|
273 |
} |
|
274 |
|
|
275 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
276 |
|
|
277 |
void setQuatAccumulated(Static4D accumulated) |
|
278 |
{ |
|
279 |
mTempAccumulated.set(accumulated); |
|
280 |
mFinishDragAccumulated = true; |
|
281 |
} |
|
282 | 171 |
} |
Also available in: Unified diff
Turn the Rubik app into a MemoryTest.