Revision f404152d
Added by Leszek Koltunski 12 months ago
src/main/java/org/distorted/bandaged/BandagedCreatorRenderer.java | ||
---|---|---|
20 | 20 |
import javax.microedition.khronos.opengles.GL10; |
21 | 21 |
|
22 | 22 |
import android.app.Activity; |
23 |
import android.app.ActivityManager; |
|
24 |
import android.content.Context; |
|
25 |
import android.content.pm.ConfigurationInfo; |
|
26 | 23 |
import android.content.res.Resources; |
27 | 24 |
import android.opengl.GLES31; |
28 | 25 |
import android.opengl.GLSurfaceView; |
... | ... | |
43 | 40 |
import org.distorted.library.type.Static3D; |
44 | 41 |
import org.distorted.library.type.Static4D; |
45 | 42 |
import org.distorted.objectlib.json.JsonWriter; |
46 |
import org.distorted.objectlib.main.InitData; |
|
47 |
import org.distorted.objectlib.shape.ShapeHexahedron; |
|
48 | 43 |
import org.distorted.objectlib.main.TwistyObject; |
49 |
import org.distorted.objectlib.objects.TwistyBandagedCuboid; |
|
44 |
|
|
50 | 45 |
import org.json.JSONException; |
51 | 46 |
|
52 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
64 | 59 |
private final DistortedScreen mScreen; |
65 | 60 |
private final Static3D mScale; |
66 | 61 |
private final Static4D mQuatT, mQuatA; |
67 |
private final int[] mObjSize;
|
|
62 |
private final BandagedObject mObject;
|
|
68 | 63 |
|
69 |
private BandagedCubit[] mCubits; |
|
70 | 64 |
private boolean mInitialPhase; |
71 | 65 |
private long mStartTime; |
72 | 66 |
private float mQuatX, mQuatY, mQuatZ, mQuatW; |
73 |
private int mX, mY, mZ, mNumCubits; |
|
74 | 67 |
private boolean mResetQuats, mSetQuatT, mResettingObject, mConnectingCubits, mCreatingCubits, mRescaling; |
75 | 68 |
private int mIndex1, mIndex2; |
76 | 69 |
private int mSaveIcon; |
... | ... | |
100 | 93 |
|
101 | 94 |
mObjectScreenRatio= INIT_RATIO; |
102 | 95 |
mSaveIcon = -1; |
103 |
mObjSize = new int[3]; |
|
104 | 96 |
|
105 | 97 |
mScreen = new DistortedScreen(); |
106 | 98 |
mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f); |
107 | 99 |
mScale = new Static3D(1,1,1); |
108 |
} |
|
109 |
|
|
110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
111 |
|
|
112 |
private boolean isAdjacent(float[] pos1, float[] pos2) |
|
113 |
{ |
|
114 |
int len1 = pos1.length/3; |
|
115 |
int len2 = pos2.length/3; |
|
116 |
|
|
117 |
for(int i=0; i<len1; i++) |
|
118 |
for(int j=0; j<len2; j++) |
|
119 |
{ |
|
120 |
float d0 = pos1[3*i ] - pos2[3*j ]; |
|
121 |
float d1 = pos1[3*i+1] - pos2[3*j+1]; |
|
122 |
float d2 = pos1[3*i+2] - pos2[3*j+2]; |
|
123 |
|
|
124 |
if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true; |
|
125 |
} |
|
126 |
|
|
127 |
return false; |
|
128 |
} |
|
129 |
|
|
130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
131 |
|
|
132 |
private int computeNumCubits(int x, int y, int z) |
|
133 |
{ |
|
134 |
return ( x<=1 || y<=1 || z<=1 ) ? x*y*z : x*y*z-(x-2)*(y-2)*(z-2); |
|
135 |
} |
|
136 |
|
|
137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
138 |
|
|
139 |
private void createCubits() |
|
140 |
{ |
|
141 |
mCubits = new BandagedCubit[mNumCubits]; |
|
142 |
int c=0; |
|
143 |
|
|
144 |
float begX = 0.5f*(1-mX); |
|
145 |
float begY = 0.5f*(1-mY); |
|
146 |
float begZ = 0.5f*(1-mZ); |
|
147 |
|
|
148 |
for(int x=0; x<mX; x++) |
|
149 |
for(int y=0; y<mY; y++) |
|
150 |
for(int z=0; z<mZ; z++) |
|
151 |
if( x==0 || x==mX-1 || y==0 || y==mY-1 || z==0 || z==mZ-1 ) |
|
152 |
{ |
|
153 |
float[] pos = new float[] { begX+x,begY+y,begZ+z }; |
|
154 |
mCubits[c] = new BandagedCubit(pos,mX,mY,mZ,mQuatT,mQuatA,mScale,false); |
|
155 |
c++; |
|
156 |
} |
|
157 |
|
|
158 |
mView.setCubits(mCubits,mX,mY,mZ); |
|
159 |
} |
|
160 |
|
|
161 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
162 |
|
|
163 |
private void resetObject() |
|
164 |
{ |
|
165 |
mView.resetCubits(); |
|
166 |
|
|
167 |
for(int c=0; c<mNumCubits; c++) |
|
168 |
{ |
|
169 |
if( !mCubits[c].isAttached() ) |
|
170 |
{ |
|
171 |
mCubits[c].attach(); |
|
172 |
mScreen.attach(mCubits[c].getNode()); |
|
173 |
} |
|
174 |
if( mCubits[c].getPosition().length>3 ) |
|
175 |
{ |
|
176 |
mCubits[c].reset(mScaleValue); |
|
177 |
} |
|
178 |
mCubits[c].setUnmarked(); |
|
179 |
} |
|
100 |
mObject = new BandagedObject(mScreen); |
|
180 | 101 |
} |
181 | 102 |
|
182 | 103 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
236 | 157 |
if( mConnectingCubits ) |
237 | 158 |
{ |
238 | 159 |
mConnectingCubits = false; |
239 |
tryConnectingCubits(mIndex1,mIndex2);
|
|
160 |
mObject.tryConnectingCubits(mIndex1,mIndex2,mScaleValue);
|
|
240 | 161 |
} |
241 | 162 |
|
242 | 163 |
if( mCreatingCubits ) |
243 | 164 |
{ |
244 | 165 |
mCreatingCubits = false; |
245 |
|
|
246 | 166 |
rescaleObject(); |
247 | 167 |
|
248 | 168 |
if( mCubitsCreated ) |
249 | 169 |
{ |
250 |
createCubits(); |
|
251 |
|
|
170 |
mObject.createCubits(mQuatT,mQuatA,mScale); |
|
171 |
float[] dist = mObject.getDist3D(); |
|
172 |
BandagedCreatorTouchControl control = mView.getTouchControl(); |
|
173 |
control.setDist3D(dist); |
|
252 | 174 |
mScreen.detachAll(); |
253 | 175 |
mView.resetCubits(); |
254 |
|
|
255 |
for(int i=0; i<mNumCubits; i++) |
|
256 |
{ |
|
257 |
mCubits[i].scaleMove(mScaleValue); |
|
258 |
DistortedNode node = mCubits[i].getNode(); |
|
259 |
mScreen.attach(node); |
|
260 |
} |
|
176 |
mObject.attachCubits(mScaleValue); |
|
261 | 177 |
} |
262 | 178 |
} |
263 | 179 |
|
... | ... | |
265 | 181 |
{ |
266 | 182 |
mRescaling = false; |
267 | 183 |
rescaleObject(); |
268 |
for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(mScaleValue);
|
|
184 |
mObject.scaleCubits(mScaleValue);
|
|
269 | 185 |
BandagedCreatorTouchControl control = mView.getTouchControl(); |
270 | 186 |
control.setObjectRatio(mObjectScreenRatio); |
271 | 187 |
} |
... | ... | |
281 | 197 |
mWidth = width; |
282 | 198 |
mHeight= height; |
283 | 199 |
rescaleObject(); |
284 |
|
|
285 | 200 |
mScreen.detachAll(); |
286 | 201 |
int touched = mView.getTouched(); |
287 |
|
|
288 |
for(int i=0; i<mNumCubits; i++) |
|
289 |
if( mCubits[i].isAttached() ) |
|
290 |
{ |
|
291 |
mCubits[i].scaleMove(mScaleValue); |
|
292 |
if( touched==i ) mCubits[i].setMarked(); |
|
293 |
else mCubits[i].setUnmarked(); |
|
294 |
DistortedNode node = mCubits[i].getNode(); |
|
295 |
mScreen.attach(node); |
|
296 |
} |
|
297 |
|
|
202 |
mObject.attachAndMarkCubits(mScaleValue,touched); |
|
298 | 203 |
mView.setScreenSize(width,height); |
299 | 204 |
mScreen.resize(width,height); |
300 | 205 |
} |
... | ... | |
306 | 211 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
307 | 212 |
{ |
308 | 213 |
DistortedLibrary.setMax(EffectType.VERTEX,0); |
309 |
MeshBase.setMaxEffComponents(120); |
|
310 |
|
|
214 |
MeshBase.setMaxEffComponents(120); // what? |
|
311 | 215 |
FragmentEffectBrightness.enable(); |
312 |
|
|
313 | 216 |
DistortedLibrary.onSurfaceCreated(this,1); |
314 | 217 |
DistortedLibrary.setCull(true); |
315 |
|
|
316 |
if( mCubits==null ) |
|
317 |
{ |
|
318 |
createCubits(); |
|
319 |
} |
|
320 |
else |
|
321 |
{ |
|
322 |
for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap(); |
|
323 |
} |
|
324 |
|
|
218 |
mObject.recreateCubits(mQuatT,mQuatA,mScale); |
|
325 | 219 |
mCubitsCreated = true; |
326 | 220 |
mWidth = 0; |
327 | 221 |
mHeight= 0; |
... | ... | |
343 | 237 |
mConnectingCubits = true; |
344 | 238 |
} |
345 | 239 |
|
346 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
347 |
|
|
348 |
private void tryConnectingCubits(int index1, int index2) |
|
349 |
{ |
|
350 |
if( index1!=index2 ) |
|
351 |
{ |
|
352 |
float[] pos1 = mCubits[index1].getPosition(); |
|
353 |
float[] pos2 = mCubits[index2].getPosition(); |
|
354 |
|
|
355 |
if( isAdjacent(pos1,pos2) ) |
|
356 |
{ |
|
357 |
mCubits[index2].join(pos1,mScaleValue); |
|
358 |
mCubits[index1].detach(); |
|
359 |
mScreen.detach(mCubits[index1].getNode()); |
|
360 |
} |
|
361 |
} |
|
362 |
} |
|
363 |
|
|
364 | 240 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
365 | 241 |
|
366 | 242 |
public Static4D getQuatAccu() |
... | ... | |
400 | 276 |
|
401 | 277 |
public void saveObject() |
402 | 278 |
{ |
403 |
int numAttached=0; |
|
404 |
|
|
405 |
for(int i=0; i<mNumCubits; i++) |
|
406 |
if( mCubits[i].isAttached() ) numAttached++; |
|
407 |
|
|
408 |
float[][] pos = new float[numAttached][]; |
|
409 |
int attached=0; |
|
410 |
|
|
411 |
for(int i=0; i<mNumCubits; i++) |
|
412 |
if( mCubits[i].isAttached() ) |
|
413 |
{ |
|
414 |
pos[attached++] = mCubits[i].getPosition(); |
|
415 |
} |
|
416 |
|
|
417 |
InitData data = new InitData(mObjSize,pos); |
|
418 |
TwistyObject obj = new TwistyBandagedCuboid( TwistyObject.MESH_NICE, TwistyObject.MODE_NORM, |
|
419 |
new Static4D(0,0,0,1), new Static3D(0,0,0), 1.0f, data, null ); |
|
279 |
TwistyObject obj = mObject.createObject(TwistyObject.MODE_NORM, 1.0f ); |
|
420 | 280 |
String name = obj.getShortName(); |
421 | 281 |
BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext(); |
422 | 282 |
|
423 | 283 |
if( act.objectDoesntExist(name) && createObjectJson(obj,act) ) |
424 | 284 |
{ |
425 |
setupIconCreation(act,data);
|
|
285 |
setupIconCreation(act); |
|
426 | 286 |
act.addObject(obj.getShortName()); |
427 | 287 |
} |
428 | 288 |
} |
... | ... | |
485 | 345 |
|
486 | 346 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
487 | 347 |
|
488 |
private int computeProjectionAngle() |
|
489 |
{ |
|
490 |
float quot1 = mObjSize[2]/ (float)mObjSize[0]; |
|
491 |
float quot2 = mObjSize[2]/ (float)mObjSize[1]; |
|
492 |
float quot3 = mObjSize[0]/ (float)mObjSize[2]; |
|
493 |
float quot4 = mObjSize[0]/ (float)mObjSize[1]; |
|
494 |
|
|
495 |
float quot5 = Math.max(quot1,quot2); |
|
496 |
float quot6 = Math.max(quot3,quot4); |
|
497 |
float quot7 = Math.max(quot5,quot6); |
|
498 |
|
|
499 |
if( quot7<=1.0f ) return 120; |
|
500 |
else if( quot7<=1.5f ) return 90; |
|
501 |
else if( quot7<=2.0f ) return 60; |
|
502 |
else return 30; |
|
503 |
} |
|
504 |
|
|
505 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
506 |
|
|
507 |
private void setupIconCreation(Activity act, InitData data) |
|
348 |
private void setupIconCreation(Activity act) |
|
508 | 349 |
{ |
509 | 350 |
final float R=1.0f; |
510 | 351 |
final int FBO_WIDTH = (int)(R*720); |
511 | 352 |
final int FBO_HEIGHT = (int)(R*1280); |
512 | 353 |
final float OBJECT_SIZE = R*0.35f; |
513 | 354 |
|
514 |
TwistyObject obj = new TwistyBandagedCuboid( TwistyObject.MESH_NICE, TwistyObject.MODE_ICON, |
|
515 |
ShapeHexahedron.DEFAULT_ROT, new Static3D(0,0,0), OBJECT_SIZE, data, null ); |
|
516 |
|
|
355 |
TwistyObject obj = mObject.createObject(TwistyObject.MODE_ICON, OBJECT_SIZE ); |
|
517 | 356 |
DistortedEffects effects = obj.getObjectEffects(); |
518 | 357 |
DistortedNode node = obj.getNode(); |
519 | 358 |
|
... | ... | |
523 | 362 |
mFramebuffer.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f); |
524 | 363 |
} |
525 | 364 |
|
526 |
mFramebuffer.setProjection( computeProjectionAngle() ,0.1f); |
|
365 |
mFramebuffer.setProjection( mObject.computeProjectionAngle() ,0.1f);
|
|
527 | 366 |
mFramebuffer.detachAll(); |
528 | 367 |
mFramebuffer.attach(node); |
529 | 368 |
|
... | ... | |
590 | 429 |
|
591 | 430 |
private void rescaleObject() |
592 | 431 |
{ |
593 |
final int size = mX>mY ? Math.max(mX, mZ) : Math.max(mY, mZ);
|
|
432 |
float size = mObject.getSize();
|
|
594 | 433 |
final float Q = mObjectScreenRatio/size; |
595 | 434 |
mScaleValue = mWidth<mHeight ? Q*mWidth : Q*mHeight; |
596 | 435 |
mScale.set( mScaleValue,mScaleValue,mScaleValue ); |
... | ... | |
600 | 439 |
|
601 | 440 |
public void changeObject(int x, int y, int z) |
602 | 441 |
{ |
603 |
if( mX!=x || mY!=y || mZ!=z ) |
|
604 |
{ |
|
605 |
mX = x; |
|
606 |
mY = y; |
|
607 |
mZ = z; |
|
608 |
|
|
609 |
mObjSize[0] = mX; |
|
610 |
mObjSize[1] = mY; |
|
611 |
mObjSize[2] = mZ; |
|
612 |
|
|
613 |
mNumCubits = computeNumCubits(mX,mY,mZ); |
|
614 |
|
|
615 |
mCreatingCubits = true; |
|
616 |
} |
|
442 |
if( mObject.tryChangeObject(x,y,z) ) mCreatingCubits = true; |
|
617 | 443 |
} |
618 | 444 |
|
619 | 445 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
644 | 470 |
if( mInitialPhase && quotient>0.5f ) |
645 | 471 |
{ |
646 | 472 |
mInitialPhase=false; |
647 |
resetObject(); |
|
473 |
mView.resetCubits(); |
|
474 |
mObject.resetObject(mScaleValue); |
|
648 | 475 |
} |
649 | 476 |
|
650 | 477 |
double angle = 2*Math.PI*quotient*quotient*(3-2*quotient); |
... | ... | |
661 | 488 |
|
662 | 489 |
public void touchCubit(int index) |
663 | 490 |
{ |
664 |
if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
|
|
491 |
mObject.touchCubit(index);
|
|
665 | 492 |
} |
666 | 493 |
|
667 | 494 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
668 | 495 |
|
669 | 496 |
public void untouchCubit(int index) |
670 | 497 |
{ |
671 |
if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked(); |
|
498 |
mObject.untouchCubit(index); |
|
499 |
} |
|
500 |
|
|
501 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
502 |
|
|
503 |
public BandagedCreatorTouchControl createTouchControl() |
|
504 |
{ |
|
505 |
return new BandagedCreatorTouchControl( getObjectRatio() , mScreen.getFOV(), mObject ); |
|
672 | 506 |
} |
673 | 507 |
|
674 | 508 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Introduce 'BandagedObject' - an abstraction and a step towards creator of Bandaged Pyraminxes.