Revision 47ba5ddc
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikRenderer.java | ||
---|---|---|
50 | 50 |
private boolean mFinishRotation, mRemoveRotation, mSetQuatCurrent, mSetQuatAccumulated; |
51 | 51 |
private boolean mSizeChangeCube, mSolveCube, mScrambleCube; |
52 | 52 |
private boolean mCanRotate, mCanDrag, mCanUI; |
53 |
private boolean mIsSolved; |
|
53 | 54 |
private RubikCube mOldCube, mNewCube; |
54 | 55 |
private int mScreenWidth, mScreenHeight; |
55 | 56 |
private MeshFlat mMesh; |
... | ... | |
86 | 87 |
mNextCubeSize = RubikActivity.getSize(); |
87 | 88 |
} |
88 | 89 |
|
90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
91 |
|
|
92 |
private float computeFOV(float cameraDistance, int screenHeight) |
|
93 |
{ |
|
94 |
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) ); |
|
95 |
return (float)(2*halfFOVInRadians*(180/Math.PI)); |
|
96 |
} |
|
97 |
|
|
98 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
99 |
|
|
100 |
private void recomputeScaleFactor(int screenWidth, int screenHeight) |
|
101 |
{ |
|
102 |
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth); |
|
103 |
|
|
104 |
if( mNewCube!=null ) |
|
105 |
{ |
|
106 |
mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
111 |
|
|
112 |
private void createCubeNow(int newSize) |
|
113 |
{ |
|
114 |
if( mOldCube!=null ) mOldCube.releaseResources(); |
|
115 |
mOldCube = mNewCube; |
|
116 |
|
|
117 |
DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
118 |
DistortedEffects effects = new DistortedEffects(); |
|
119 |
|
|
120 |
mNewCube = new RubikCube(newSize, mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects); |
|
121 |
mNewCube.createTexture(); |
|
122 |
|
|
123 |
if( mScreenWidth!=0 ) |
|
124 |
{ |
|
125 |
recomputeScaleFactor(mScreenWidth,mScreenHeight); |
|
126 |
} |
|
127 |
|
|
128 |
mIsSolved = true; |
|
129 |
} |
|
130 |
|
|
131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
132 |
// do all 'adjustable' effects (SizeChange, Solve, Scramble) |
|
133 |
|
|
134 |
private void doEffectNow(BaseEffect.Type type) |
|
135 |
{ |
|
136 |
int index = type.ordinal(); |
|
137 |
|
|
138 |
mEffectID[index] = type.startEffect(this); |
|
139 |
|
|
140 |
if( mEffectID[index] < 0 ) |
|
141 |
{ |
|
142 |
mCanUI = true; |
|
143 |
mCanRotate = true; |
|
144 |
mCanDrag = true; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
149 |
// no this will not race with onDrawFrame |
|
150 |
|
|
151 |
void finishRotation() |
|
152 |
{ |
|
153 |
mFinishRotation = true; |
|
154 |
} |
|
155 |
|
|
156 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
157 |
|
|
158 |
boolean createCube(int newSize) |
|
159 |
{ |
|
160 |
if( mCanDrag && mCanRotate && (mNewCube==null || newSize != mNewCube.getSize()) ) |
|
161 |
{ |
|
162 |
mSizeChangeCube = true; |
|
163 |
mNextCubeSize = newSize; |
|
164 |
return true; |
|
165 |
} |
|
166 |
|
|
167 |
return false; |
|
168 |
} |
|
169 |
|
|
170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 |
|
|
172 |
void scrambleCube(int num) |
|
173 |
{ |
|
174 |
if( mCanUI ) |
|
175 |
{ |
|
176 |
mScrambleCube = true; |
|
177 |
mScrambleCubeNum = num; |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
182 |
|
|
183 |
void solveCube() |
|
184 |
{ |
|
185 |
if( mCanUI ) |
|
186 |
{ |
|
187 |
mSolveCube = true; |
|
188 |
} |
|
189 |
} |
|
190 |
|
|
191 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
192 |
|
|
193 |
float returnCubeSizeInScreenSpace() |
|
194 |
{ |
|
195 |
return mCubeSizeInScreenSpace; |
|
196 |
} |
|
197 |
|
|
198 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
199 |
|
|
200 |
boolean canRotate() |
|
201 |
{ |
|
202 |
return mCanRotate; |
|
203 |
} |
|
204 |
|
|
205 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
206 |
|
|
207 |
boolean canDrag() |
|
208 |
{ |
|
209 |
return mCanDrag; |
|
210 |
} |
|
211 |
|
|
212 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
213 |
|
|
214 |
void setQuatCurrentOnNextRender() |
|
215 |
{ |
|
216 |
mSetQuatCurrent = true; |
|
217 |
} |
|
218 |
|
|
219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
220 |
|
|
221 |
void setQuatAccumulatedOnNextRender() |
|
222 |
{ |
|
223 |
mSetQuatAccumulated = true; |
|
224 |
} |
|
225 |
|
|
226 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
227 |
// PUBLIC API |
|
89 | 228 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
90 | 229 |
// various things are done here delayed, 'after the next render' as not to be done mid-render and |
91 | 230 |
// cause artifacts. |
... | ... | |
120 | 259 |
mRemoveRotation=false; |
121 | 260 |
mNewCube.removeRotationNow(); |
122 | 261 |
|
123 |
if( mNewCube.isSolved() ) |
|
262 |
boolean solved = mNewCube.isSolved(); |
|
263 |
|
|
264 |
if( solved && !mIsSolved ) |
|
124 | 265 |
{ |
125 | 266 |
mCanDrag = false; |
126 | 267 |
mCanRotate = false; |
... | ... | |
132 | 273 |
mCanRotate = true; |
133 | 274 |
mCanUI = true; |
134 | 275 |
} |
276 |
|
|
277 |
mIsSolved = solved; |
|
135 | 278 |
} |
136 | 279 |
|
137 | 280 |
if( mSizeChangeCube ) |
... | ... | |
225 | 368 |
} |
226 | 369 |
} |
227 | 370 |
|
228 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
229 |
|
|
230 |
private float computeFOV(float cameraDistance, int screenHeight) |
|
231 |
{ |
|
232 |
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) ); |
|
233 |
return (float)(2*halfFOVInRadians*(180/Math.PI)); |
|
234 |
} |
|
235 |
|
|
236 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
237 |
// no this will not race with onDrawFrame |
|
238 |
|
|
239 |
void finishRotation() |
|
240 |
{ |
|
241 |
mFinishRotation = true; |
|
242 |
} |
|
243 |
|
|
244 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
245 |
|
|
246 |
boolean createCube(int newSize) |
|
247 |
{ |
|
248 |
if( mCanDrag && mCanRotate && (mNewCube==null || newSize != mNewCube.getSize()) ) |
|
249 |
{ |
|
250 |
mSizeChangeCube = true; |
|
251 |
mNextCubeSize = newSize; |
|
252 |
return true; |
|
253 |
} |
|
254 |
|
|
255 |
return false; |
|
256 |
} |
|
257 |
|
|
258 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
259 |
|
|
260 |
private void recomputeScaleFactor(int screenWidth, int screenHeight) |
|
261 |
{ |
|
262 |
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth); |
|
263 |
|
|
264 |
if( mNewCube!=null ) |
|
265 |
{ |
|
266 |
mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace); |
|
267 |
} |
|
268 |
} |
|
269 |
|
|
270 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
271 |
|
|
272 |
void scrambleCube(int num) |
|
273 |
{ |
|
274 |
if( mCanUI ) |
|
275 |
{ |
|
276 |
mScrambleCube = true; |
|
277 |
mScrambleCubeNum = num; |
|
278 |
} |
|
279 |
} |
|
280 |
|
|
281 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
282 |
|
|
283 |
void solveCube() |
|
284 |
{ |
|
285 |
if( mCanUI ) |
|
286 |
{ |
|
287 |
mSolveCube = true; |
|
288 |
} |
|
289 |
} |
|
290 |
|
|
291 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
292 |
|
|
293 |
private void createCubeNow(int newSize) |
|
294 |
{ |
|
295 |
if( mOldCube!=null ) mOldCube.releaseResources(); |
|
296 |
mOldCube = mNewCube; |
|
297 |
|
|
298 |
DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
299 |
DistortedEffects effects = new DistortedEffects(); |
|
300 |
|
|
301 |
mNewCube = new RubikCube(newSize, mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects); |
|
302 |
mNewCube.createTexture(); |
|
303 |
|
|
304 |
if( mScreenWidth!=0 ) |
|
305 |
{ |
|
306 |
recomputeScaleFactor(mScreenWidth,mScreenHeight); |
|
307 |
} |
|
308 |
} |
|
309 |
|
|
310 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
311 |
// do all 'adjustable' effects (SizeChange, Solve, Scramble) |
|
312 |
|
|
313 |
private void doEffectNow(BaseEffect.Type type) |
|
314 |
{ |
|
315 |
int index = type.ordinal(); |
|
316 |
|
|
317 |
mEffectID[index] = type.startEffect(this); |
|
318 |
|
|
319 |
if( mEffectID[index] < 0 ) |
|
320 |
{ |
|
321 |
mCanUI = true; |
|
322 |
mCanRotate = true; |
|
323 |
mCanDrag = true; |
|
324 |
} |
|
325 |
} |
|
326 |
|
|
327 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
328 |
|
|
329 |
float returnCubeSizeInScreenSpace() |
|
330 |
{ |
|
331 |
return mCubeSizeInScreenSpace; |
|
332 |
} |
|
333 |
|
|
334 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
335 |
|
|
336 |
boolean canRotate() |
|
337 |
{ |
|
338 |
return mCanRotate; |
|
339 |
} |
|
340 |
|
|
341 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
342 |
|
|
343 |
boolean canDrag() |
|
344 |
{ |
|
345 |
return mCanDrag; |
|
346 |
} |
|
347 |
|
|
348 | 371 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
349 | 372 |
|
350 | 373 |
public RubikCube getCube() |
... | ... | |
372 | 395 |
{ |
373 | 396 |
return mScrambleCubeNum; |
374 | 397 |
} |
375 |
|
|
376 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
377 |
|
|
378 |
void setQuatCurrentOnNextRender() |
|
379 |
{ |
|
380 |
mSetQuatCurrent = true; |
|
381 |
} |
|
382 |
|
|
383 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
384 |
|
|
385 |
void setQuatAccumulatedOnNextRender() |
|
386 |
{ |
|
387 |
mSetQuatAccumulated = true; |
|
388 |
} |
|
389 | 398 |
} |
Also available in: Unified diff
RubikCube: further fixes