Revision 7f986357
Added by Leszek Koltunski over 5 years ago
src/main/java/org/distorted/examples/rubik/RubikActivity.java | ||
---|---|---|
67 | 67 |
Distorted.onDestroy(); |
68 | 68 |
super.onDestroy(); |
69 | 69 |
} |
70 |
|
|
71 | 70 |
} |
src/main/java/org/distorted/examples/rubik/RubikCube.java | ||
---|---|---|
41 | 41 |
|
42 | 42 |
class RubikCube |
43 | 43 |
{ |
44 |
static final int TEXTURE_SIZE = 100; |
|
44 |
private static final int TEXTURE_SIZE = 100;
|
|
45 | 45 |
|
46 | 46 |
private static final Static3D VectX = new Static3D(1,0,0); |
47 | 47 |
private static final Static3D VectY = new Static3D(0,1,0); |
... | ... | |
161 | 161 |
|
162 | 162 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
163 | 163 |
|
164 |
void addNewRotation(int vector, int row )
|
|
164 |
void addNewRotation(int vector, float offset )
|
|
165 | 165 |
{ |
166 | 166 |
Static3D axis = VectX; |
167 | 167 |
|
... | ... | |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
mRotAxis = vector; |
176 |
mRotRow = row;
|
|
176 |
mRotRow = (int)(mSize*offset);
|
|
177 | 177 |
|
178 | 178 |
mRotationAngleStatic.set1(0.0f); |
179 | 179 |
|
... | ... | |
182 | 182 |
for(int z=0; z<mSize; z++) |
183 | 183 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
184 | 184 |
{ |
185 |
if( belongsToRotation(x,y,z,vector,row) )
|
|
185 |
if( belongsToRotation(x,y,z,vector,mRotRow) )
|
|
186 | 186 |
{ |
187 | 187 |
mRotationAxis[x][y][z].set(axis); |
188 | 188 |
mRotationAngle[x][y][z].add(mRotationAngleStatic); |
... | ... | |
275 | 275 |
int roundedY = (int)(rotatedY+0.1f); |
276 | 276 |
int roundedZ = (int)(rotatedZ+0.1f); |
277 | 277 |
|
278 |
//android.util.Log.e("rubik", "before: ("+((int)beforeX)+","+((int)beforeY)+","+((int)beforeZ)+") after: ("+roundedX+","+roundedY+","+roundedZ+")"); |
|
279 |
|
|
280 | 278 |
mCurrentPosition[x][y][z].set1(roundedX); |
281 | 279 |
mCurrentPosition[x][y][z].set2(roundedY); |
282 | 280 |
mCurrentPosition[x][y][z].set3(roundedZ); |
... | ... | |
346 | 344 |
{ |
347 | 345 |
return mTexture.getDepth(mCubes[0][0][0]); |
348 | 346 |
} |
347 |
|
|
348 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
349 |
|
|
350 |
float getSizeInModelSpace() |
|
351 |
{ |
|
352 |
return mSize*TEXTURE_SIZE; |
|
353 |
} |
|
349 | 354 |
} |
src/main/java/org/distorted/examples/rubik/RubikRenderer.java | ||
---|---|---|
34 | 34 |
|
35 | 35 |
class RubikRenderer implements GLSurfaceView.Renderer |
36 | 36 |
{ |
37 |
static final int NUM_CUBES = 4; |
|
37 |
private static final int NUM_CUBES = 4;
|
|
38 | 38 |
private static final float CUBE_SCREEN_RATIO = 0.5f; |
39 | 39 |
|
40 | 40 |
private RubikSurfaceView mView; |
... | ... | |
98 | 98 |
|
99 | 99 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
100 | 100 |
{ |
101 |
mScreen.setProjection( width>height ? 60.0f : 90.0f, 0.1f); |
|
101 |
float fovInDegrees = (width>height ? 60.0f : 90.0f); |
|
102 |
|
|
103 |
mScreen.setProjection( fovInDegrees, 0.1f); |
|
102 | 104 |
|
103 | 105 |
mView.setScreenSize(width,height); |
104 |
mView.recomputeCameraDistance(); |
|
106 |
mView.recomputeCameraDistance(fovInDegrees*Math.PI/180);
|
|
105 | 107 |
|
106 | 108 |
float w = mCube.getWidth(); |
107 | 109 |
float h = mCube.getHeight(); |
... | ... | |
145 | 147 |
|
146 | 148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
147 | 149 |
|
148 |
float getFOVInRadians() |
|
149 |
{ |
|
150 |
return (float)((mScreen.getFOV()*Math.PI) / 180.0); |
|
151 |
} |
|
152 |
|
|
153 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
154 |
// NUM_CUBES individual little cubes, each TEXTURE_SIZE in size, times 'scaleFactor' (see onSurfaceChanged) |
|
155 |
|
|
156 | 150 |
float returnCubeSize() |
157 | 151 |
{ |
158 |
return mScaleFactor*NUM_CUBES*RubikCube.TEXTURE_SIZE;
|
|
152 |
return mScaleFactor*mCube.getSizeInModelSpace();
|
|
159 | 153 |
} |
160 | 154 |
|
161 | 155 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java | ||
---|---|---|
44 | 44 |
static final int VECTZ = 2; |
45 | 45 |
static final int VECTN = 3; |
46 | 46 |
|
47 |
private boolean mDragging, mRotating;
|
|
47 |
private boolean mDragging, mBeginRot;
|
|
48 | 48 |
private int mX, mY; |
49 | 49 |
private Static4D mQuatCurrent, mQuatAccumulated; |
50 | 50 |
private int mRotationVect; |
... | ... | |
64 | 64 |
super(context); |
65 | 65 |
|
66 | 66 |
mDragging = false; |
67 |
mRotating = false;
|
|
67 |
mBeginRot = false;
|
|
68 | 68 |
mRotationVect = VECTN; |
69 | 69 |
|
70 | 70 |
mScreenWidth = mScreenHeight = mScreenMin = 0; |
... | ... | |
103 | 103 |
mY = y; |
104 | 104 |
mLastTouchedFace = faceTouched(x,y); |
105 | 105 |
|
106 |
if( mLastTouchedFace != NONE ) mRotating = true;
|
|
106 |
if( mLastTouchedFace != NONE ) mBeginRot = true;
|
|
107 | 107 |
else mDragging = true; |
108 | 108 |
|
109 | 109 |
break; |
... | ... | |
112 | 112 |
mQuatCurrent.set(quatFromDrag(mX-x,mY-y)); |
113 | 113 |
mRenderer.setQuatCurrent(mQuatCurrent); |
114 | 114 |
} |
115 |
else if( mRotating )
|
|
115 |
else if( mBeginRot )
|
|
116 | 116 |
{ |
117 | 117 |
int minimumToRotate = (mScreenMin*mScreenMin)/100; |
118 | 118 |
|
119 | 119 |
if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate ) |
120 | 120 |
{ |
121 | 121 |
addNewRotation(x,y); |
122 |
mX = x; |
|
123 |
mY = y; |
|
124 |
mRotating = false; |
|
122 |
mBeginRot = false; |
|
125 | 123 |
} |
126 | 124 |
} |
127 | 125 |
else |
... | ... | |
132 | 130 |
case MotionEvent.ACTION_UP : if( !mDragging ) finishRotation(); |
133 | 131 |
|
134 | 132 |
mDragging = false; |
135 |
mRotating = false;
|
|
133 |
mBeginRot = false;
|
|
136 | 134 |
|
137 | 135 |
mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated)); |
138 | 136 |
mQuatCurrent.set(0f, 0f, 0f, 1f); |
... | ... | |
144 | 142 |
return true; |
145 | 143 |
} |
146 | 144 |
|
145 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
146 |
|
|
147 |
void setScreenSize(int width, int height) |
|
148 |
{ |
|
149 |
mScreenWidth = width; |
|
150 |
mScreenHeight= height; |
|
151 |
|
|
152 |
mScreenMin = width<height ? width:height; |
|
153 |
} |
|
154 |
|
|
155 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
156 |
|
|
157 |
void recomputeCameraDistance(double fovInRadians) |
|
158 |
{ |
|
159 |
mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5)); |
|
160 |
} |
|
161 |
|
|
147 | 162 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
148 | 163 |
|
149 | 164 |
private void addNewRotation(int x, int y) |
... | ... | |
157 | 172 |
float diffY = (mPoiY-mCamY)*A + mCamY - mStartY; |
158 | 173 |
float diffZ = (mPoiZ-mCamZ)*A + mCamZ - mStartZ; |
159 | 174 |
|
160 |
mStartX = diffX + mStartX; |
|
161 |
mStartY = diffY + mStartY; |
|
162 |
mStartZ = diffZ + mStartZ; |
|
163 |
|
|
164 | 175 |
switch(mLastTouchedFace) |
165 | 176 |
{ |
166 | 177 |
case FRONT : |
... | ... | |
171 | 182 |
case BOTTOM: mRotationVect = (isVertical(diffX, diffZ) ? VECTX:VECTZ); break; |
172 | 183 |
} |
173 | 184 |
|
174 |
int row=0; |
|
175 |
float tmp = RubikRenderer.NUM_CUBES/(2*cubeHalfSize); |
|
185 |
float offset=0; |
|
176 | 186 |
|
177 | 187 |
switch(mRotationVect) |
178 | 188 |
{ |
179 |
case VECTX: row = (int)(tmp*(mStartX+cubeHalfSize)); break;
|
|
180 |
case VECTY: row = (int)(tmp*(mStartY+cubeHalfSize)); break;
|
|
181 |
case VECTZ: row = (int)(tmp*(mStartZ+cubeHalfSize)); break;
|
|
189 |
case VECTX: offset = (mStartX+cubeHalfSize)/(2*cubeHalfSize); break;
|
|
190 |
case VECTY: offset = (mStartY+cubeHalfSize)/(2*cubeHalfSize); break;
|
|
191 |
case VECTZ: offset = (mStartZ+cubeHalfSize)/(2*cubeHalfSize); break;
|
|
182 | 192 |
} |
183 | 193 |
|
184 |
mCube.addNewRotation(mRotationVect,row); |
|
194 |
mStartX = diffX + mStartX; |
|
195 |
mStartY = diffY + mStartY; |
|
196 |
mStartZ = diffZ + mStartZ; |
|
197 |
|
|
198 |
mCube.addNewRotation(mRotationVect,offset); |
|
185 | 199 |
} |
186 | 200 |
|
187 | 201 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
206 | 220 |
|
207 | 221 |
float angle=0.0f; |
208 | 222 |
|
209 |
switch(mRotationVect)
|
|
223 |
switch(mLastTouchedFace)
|
|
210 | 224 |
{ |
211 |
case VECTX: switch(mLastTouchedFace) |
|
212 |
{ |
|
213 |
case FRONT : angle = -diffY; break; |
|
214 |
case BACK : angle = diffY; break; |
|
215 |
case TOP : angle = diffZ; break; |
|
216 |
case BOTTOM: angle = -diffZ; break; |
|
217 |
} |
|
218 |
break; |
|
219 |
case VECTY: switch(mLastTouchedFace) |
|
220 |
{ |
|
221 |
case FRONT : angle = diffX; break; |
|
222 |
case BACK : angle = -diffX; break; |
|
223 |
case LEFT : angle = diffZ; break; |
|
224 |
case RIGHT : angle = -diffZ; break; |
|
225 |
} |
|
226 |
break; |
|
227 |
case VECTZ: switch(mLastTouchedFace) |
|
228 |
{ |
|
229 |
case TOP : angle = -diffX; break; |
|
230 |
case BOTTOM: angle = diffX; break; |
|
231 |
case LEFT : angle = -diffY; break; |
|
232 |
case RIGHT : angle = diffY; break; |
|
233 |
} |
|
234 |
break; |
|
235 |
default : android.util.Log.e("View", "impossible vector: "+mRotationVect); |
|
225 |
case FRONT : angle = (mRotationVect==VECTX ? -diffY : diffX); break; |
|
226 |
case BACK : angle = (mRotationVect==VECTX ? diffY :-diffX); break; |
|
227 |
case LEFT : angle = (mRotationVect==VECTY ? diffZ :-diffY); break; |
|
228 |
case RIGHT : angle = (mRotationVect==VECTY ? -diffZ : diffY); break; |
|
229 |
case TOP : angle = (mRotationVect==VECTZ ? -diffX : diffZ); break; |
|
230 |
case BOTTOM: angle = (mRotationVect==VECTZ ? diffX :-diffZ); break; |
|
236 | 231 |
} |
237 | 232 |
|
238 | 233 |
mCube.continueRotation(200.0f*angle/mScreenMin); |
... | ... | |
329 | 324 |
|
330 | 325 |
private boolean faceIsVisible(int face) |
331 | 326 |
{ |
332 |
float cubeHalfSize = mRenderer.returnCubeSize()*0.5f;
|
|
327 |
float cubeHalfSize= mRenderer.returnCubeSize()*0.5f; |
|
333 | 328 |
|
334 | 329 |
Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,mCameraDistance,0), mQuatAccumulated); |
335 | 330 |
|
... | ... | |
378 | 373 |
|
379 | 374 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
380 | 375 |
|
381 |
void fillTouchPoint(int x, int y) |
|
376 |
private void fillTouchPoint(int x, int y)
|
|
382 | 377 |
{ |
383 | 378 |
float halfScrWidth = mScreenWidth *0.5f; |
384 | 379 |
float halfScrHeight = mScreenHeight*0.5f; |
... | ... | |
392 | 387 |
|
393 | 388 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
394 | 389 |
|
395 |
void fillCamera() |
|
390 |
private void fillCamera()
|
|
396 | 391 |
{ |
397 | 392 |
Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0); |
398 | 393 |
Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated); |
... | ... | |
404 | 399 |
|
405 | 400 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
406 | 401 |
|
407 |
float retA(int face, float cubeHalfSize) |
|
402 |
private float retA(int face, float cubeHalfSize)
|
|
408 | 403 |
{ |
409 | 404 |
switch(face) |
410 | 405 |
{ |
... | ... | |
421 | 416 |
|
422 | 417 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
423 | 418 |
|
424 |
boolean faceCondition(int face) |
|
419 |
private boolean faceCondition(int face)
|
|
425 | 420 |
{ |
426 | 421 |
switch(face) |
427 | 422 |
{ |
... | ... | |
435 | 430 |
|
436 | 431 |
return false; |
437 | 432 |
} |
438 |
|
|
439 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
440 |
|
|
441 |
void setScreenSize(int width, int height) |
|
442 |
{ |
|
443 |
mScreenWidth = width; |
|
444 |
mScreenHeight= height; |
|
445 |
|
|
446 |
mScreenMin = width<height ? width:height; |
|
447 |
} |
|
448 |
|
|
449 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
450 |
|
|
451 |
void recomputeCameraDistance() |
|
452 |
{ |
|
453 |
double fovInRadians = mRenderer.getFOVInRadians(); |
|
454 |
mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5)); |
|
455 |
} |
|
456 | 433 |
} |
457 | 434 |
|
Also available in: Unified diff
RubikApp: simplifications.