Revision da231553
Added by Leszek Koltunski about 5 years ago
src/main/java/org/distorted/examples/rubik/RubikCube.java | ||
---|---|---|
45 | 45 |
|
46 | 46 |
class RubikCube |
47 | 47 |
{ |
48 |
private static final int VECTX = 0; //
|
|
49 |
private static final int VECTY = 1; // don't change this
|
|
50 |
private static final int VECTZ = 2; //
|
|
48 |
private static final int VECTX = 0; |
|
49 |
private static final int VECTY = 1; |
|
50 |
private static final int VECTZ = 2; |
|
51 | 51 |
|
52 | 52 |
private static final int ROTATION_MILLISEC = 1500; |
53 | 53 |
private static final int TEXTURE_SIZE = 100; |
... | ... | |
56 | 56 |
private static final Static3D VectY = new Static3D(0,1,0); |
57 | 57 |
private static final Static3D VectZ = new Static3D(0,0,1); |
58 | 58 |
|
59 |
private static Random mRnd = new Random(0); |
|
60 |
|
|
59 | 61 |
private DistortedNode[][][] mNodes; |
60 | 62 |
private MeshCubes[][][] mCubes; |
61 | 63 |
private DistortedEffects[][][] mEffects; |
62 | 64 |
private Static3D[][][] mRotationAxis; |
63 | 65 |
private Dynamic1D[][][] mRotationAngle; |
64 | 66 |
private DistortedTexture mTexture; |
65 |
|
|
66 | 67 |
private int mSize; |
67 | 68 |
|
68 |
private Random mRnd = new Random(0); |
|
69 |
|
|
70 | 69 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
71 | 70 |
|
72 | 71 |
RubikCube(int size, Static3D move, Static3D scale) |
... | ... | |
148 | 147 |
} |
149 | 148 |
|
150 | 149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
150 |
// Initial rotation of the cube. Something semi-random that looks good. |
|
151 | 151 |
|
152 |
void attachToScreen(DistortedScreen screen) |
|
153 |
{ |
|
154 |
for(int x=0; x<mSize; x++) |
|
155 |
for(int y=0; y<mSize; y++) |
|
156 |
for(int z=0; z<mSize; z++) |
|
157 |
{ |
|
158 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
159 |
{ |
|
160 |
mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]); |
|
161 |
screen.attach(mNodes[x][y][z]); |
|
162 |
} |
|
163 |
} |
|
164 |
} |
|
152 |
private Static4D initializeQuat() |
|
153 |
{ |
|
154 |
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); |
|
155 |
} |
|
165 | 156 |
|
166 | 157 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
167 |
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of |
|
168 |
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major |
|
169 |
// memory leak. |
|
170 |
|
|
171 |
void releaseResources() |
|
172 |
{ |
|
173 |
mTexture.markForDeletion(); |
|
174 | 158 |
|
175 |
for(int x=0; x<mSize; x++) |
|
176 |
for(int y=0; y<mSize; y++) |
|
177 |
for(int z=0; z<mSize; z++) |
|
178 |
{ |
|
179 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
180 |
{ |
|
181 |
mCubes[x][y][z].markForDeletion(); |
|
182 |
mNodes[x][y][z].markForDeletion(); |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
159 |
void attachToScreen(DistortedScreen screen) |
|
160 |
{ |
|
161 |
for(int x=0; x<mSize; x++) |
|
162 |
for(int y=0; y<mSize; y++) |
|
163 |
for(int z=0; z<mSize; z++) |
|
164 |
{ |
|
165 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
166 |
{ |
|
167 |
mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]); |
|
168 |
screen.attach(mNodes[x][y][z]); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
186 | 172 |
|
187 | 173 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
174 |
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of |
|
175 |
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major |
|
176 |
// memory leak. |
|
188 | 177 |
|
189 |
void addRotation(EffectListener listener) |
|
190 |
{ |
|
191 |
mRnd.setSeed(System.currentTimeMillis()); |
|
192 |
|
|
193 |
int vector = mRnd.nextInt(3); |
|
194 |
int rotRow = mRnd.nextInt(mSize); |
|
195 |
|
|
196 |
boolean first = true; |
|
197 |
Static3D axis = VectX; |
|
198 |
|
|
199 |
switch(vector) |
|
200 |
{ |
|
201 |
case VECTX: axis = VectX; break; |
|
202 |
case VECTY: axis = VectY; break; |
|
203 |
case VECTZ: axis = VectZ; break; |
|
204 |
} |
|
205 |
|
|
206 |
for(int x=0; x<mSize; x++) |
|
207 |
for(int y=0; y<mSize; y++) |
|
208 |
for(int z=0; z<mSize; z++) |
|
209 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
210 |
{ |
|
211 |
if( belongsToRotation(x,y,z,vector,rotRow) ) |
|
212 |
{ |
|
213 |
mRotationAxis[x][y][z].set(axis); |
|
214 |
mRotationAngle[x][y][z].makeRunNowFor(ROTATION_MILLISEC); |
|
215 |
mRotationAngle[x][y][z].setPoint(1,90.0f); |
|
216 |
|
|
217 |
if( first ) |
|
218 |
{ |
|
219 |
first = false; |
|
220 |
mEffects[x][y][z].registerForMessages(listener); |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
} |
|
178 |
void releaseResources() |
|
179 |
{ |
|
180 |
mTexture.markForDeletion(); |
|
181 |
|
|
182 |
for(int x=0; x<mSize; x++) |
|
183 |
for(int y=0; y<mSize; y++) |
|
184 |
for(int z=0; z<mSize; z++) |
|
185 |
{ |
|
186 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
187 |
{ |
|
188 |
mCubes[x][y][z].markForDeletion(); |
|
189 |
mNodes[x][y][z].markForDeletion(); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
225 | 193 |
|
226 | 194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
227 |
// Initial rotation of the cube. Something semi-random that looks good. |
|
228 | 195 |
|
229 |
private Static4D initializeQuat()
|
|
196 |
void addRotation(EffectListener listener)
|
|
230 | 197 |
{ |
231 |
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); |
|
198 |
mRnd.setSeed(System.currentTimeMillis()); |
|
199 |
|
|
200 |
int vector = mRnd.nextInt(3); |
|
201 |
int rotRow = mRnd.nextInt(mSize); |
|
202 |
|
|
203 |
boolean first = true; |
|
204 |
Static3D axis = VectX; |
|
205 |
|
|
206 |
switch(vector) |
|
207 |
{ |
|
208 |
case VECTX: axis = VectX; break; |
|
209 |
case VECTY: axis = VectY; break; |
|
210 |
case VECTZ: axis = VectZ; break; |
|
211 |
} |
|
212 |
|
|
213 |
for(int x=0; x<mSize; x++) |
|
214 |
for(int y=0; y<mSize; y++) |
|
215 |
for(int z=0; z<mSize; z++) |
|
216 |
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) |
|
217 |
{ |
|
218 |
if( belongsToRotation(x,y,z,vector,rotRow) ) |
|
219 |
{ |
|
220 |
mRotationAxis[x][y][z].set(axis); |
|
221 |
mRotationAngle[x][y][z].makeRunNowFor(ROTATION_MILLISEC); |
|
222 |
mRotationAngle[x][y][z].setPoint(1,90.0f); |
|
223 |
|
|
224 |
if( first ) |
|
225 |
{ |
|
226 |
first = false; |
|
227 |
mEffects[x][y][z].registerForMessages(listener); |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
232 | 231 |
} |
233 | 232 |
|
234 | 233 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
235 | 234 |
|
236 |
private boolean belongsToRotation(int x, int y, int z, int vector, int row)
|
|
237 |
{
|
|
238 |
switch(vector)
|
|
239 |
{
|
|
240 |
case VECTX: return x==row;
|
|
241 |
case VECTY: return y==row;
|
|
242 |
case VECTZ: return z==row;
|
|
243 |
}
|
|
244 |
|
|
245 |
return false;
|
|
246 |
}
|
|
235 |
private boolean belongsToRotation(int x, int y, int z, int vector, int row) |
|
236 |
{ |
|
237 |
switch(vector) |
|
238 |
{ |
|
239 |
case VECTX: return x==row; |
|
240 |
case VECTY: return y==row; |
|
241 |
case VECTZ: return z==row; |
|
242 |
} |
|
243 |
|
|
244 |
return false; |
|
245 |
} |
|
247 | 246 |
|
248 | 247 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
249 | 248 |
|
250 |
private float getSinkStrength()
|
|
251 |
{
|
|
252 |
switch(mSize)
|
|
253 |
{
|
|
254 |
case 1 : return 1.1f;
|
|
255 |
case 2 : return 1.5f;
|
|
256 |
case 3 : return 1.8f;
|
|
257 |
case 4 : return 2.0f;
|
|
258 |
default: return 3.0f - 4.0f/mSize;
|
|
259 |
}
|
|
260 |
}
|
|
249 |
private float getSinkStrength() |
|
250 |
{ |
|
251 |
switch(mSize) |
|
252 |
{ |
|
253 |
case 1 : return 1.1f; |
|
254 |
case 2 : return 1.5f; |
|
255 |
case 3 : return 1.8f; |
|
256 |
case 4 : return 2.0f; |
|
257 |
default: return 3.0f - 4.0f/mSize; |
|
258 |
} |
|
259 |
} |
|
261 | 260 |
|
262 | 261 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
263 | 262 |
|
264 |
void createTexture()
|
|
265 |
{
|
|
266 |
Bitmap bitmap;
|
|
267 |
|
|
268 |
final int S = 128;
|
|
269 |
final int W = 3*S;
|
|
270 |
final int H = 2*S;
|
|
271 |
final int R = S/10;
|
|
272 |
final int M = S/20;
|
|
273 |
|
|
274 |
Paint paint = new Paint();
|
|
275 |
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
|
|
276 |
Canvas canvas = new Canvas(bitmap);
|
|
277 |
|
|
278 |
paint.setAntiAlias(true);
|
|
279 |
paint.setTextAlign(Paint.Align.CENTER);
|
|
280 |
paint.setStyle(Paint.Style.FILL);
|
|
281 |
|
|
282 |
// 3x2 bitmap = 6 squares:
|
|
283 |
//
|
|
284 |
// RED GREEN BLUE
|
|
285 |
// YELLOW WHITE BROWN
|
|
286 |
|
|
287 |
paint.setColor(0xff000000); // BLACK BACKGROUND
|
|
288 |
canvas.drawRect(0, 0, W, H, paint); //
|
|
289 |
|
|
290 |
paint.setColor(0xffff0000); // RED
|
|
291 |
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); //
|
|
292 |
paint.setColor(0xff00ff00); // GREEN
|
|
293 |
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); //
|
|
294 |
paint.setColor(0xff0000ff); // BLUE
|
|
295 |
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); //
|
|
296 |
paint.setColor(0xffffff00); // YELLOW
|
|
297 |
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); //
|
|
298 |
paint.setColor(0xffffffff); // WHITE
|
|
299 |
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
|
|
300 |
paint.setColor(0xffb5651d); // BROWN
|
|
301 |
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
|
|
302 |
|
|
303 |
mTexture.setTexture(bitmap);
|
|
304 |
}
|
|
263 |
void createTexture() |
|
264 |
{ |
|
265 |
Bitmap bitmap; |
|
266 |
|
|
267 |
final int S = 128; |
|
268 |
final int W = 3*S; |
|
269 |
final int H = 2*S; |
|
270 |
final int R = S/10; |
|
271 |
final int M = S/20; |
|
272 |
|
|
273 |
Paint paint = new Paint(); |
|
274 |
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888); |
|
275 |
Canvas canvas = new Canvas(bitmap); |
|
276 |
|
|
277 |
paint.setAntiAlias(true); |
|
278 |
paint.setTextAlign(Paint.Align.CENTER); |
|
279 |
paint.setStyle(Paint.Style.FILL); |
|
280 |
|
|
281 |
// 3x2 bitmap = 6 squares: |
|
282 |
// |
|
283 |
// RED GREEN BLUE |
|
284 |
// YELLOW WHITE BROWN |
|
285 |
|
|
286 |
paint.setColor(0xff000000); // BLACK BACKGROUND |
|
287 |
canvas.drawRect(0, 0, W, H, paint); // |
|
288 |
|
|
289 |
paint.setColor(0xffff0000); // RED |
|
290 |
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); // |
|
291 |
paint.setColor(0xff00ff00); // GREEN |
|
292 |
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); // |
|
293 |
paint.setColor(0xff0000ff); // BLUE |
|
294 |
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); // |
|
295 |
paint.setColor(0xffffff00); // YELLOW |
|
296 |
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); // |
|
297 |
paint.setColor(0xffffffff); // WHITE |
|
298 |
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); // |
|
299 |
paint.setColor(0xffb5651d); // BROWN |
|
300 |
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); // |
|
301 |
|
|
302 |
mTexture.setTexture(bitmap); |
|
303 |
} |
|
305 | 304 |
|
306 | 305 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
307 | 306 |
|
308 |
float getTextureSize()
|
|
309 |
{
|
|
310 |
return TEXTURE_SIZE;
|
|
311 |
}
|
|
307 |
float getTextureSize() |
|
308 |
{ |
|
309 |
return TEXTURE_SIZE; |
|
310 |
} |
|
312 | 311 |
|
313 | 312 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
314 | 313 |
|
315 |
int getSize()
|
|
316 |
{
|
|
317 |
return mSize;
|
|
318 |
}
|
|
314 |
int getSize() |
|
315 |
{ |
|
316 |
return mSize; |
|
317 |
} |
|
319 | 318 |
} |
Also available in: Unified diff
minor.