1 |
15bcc6f7
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2019 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
15bcc6f7
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
15bcc6f7
|
Leszek Koltunski
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
71c8884f
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
15bcc6f7
|
Leszek Koltunski
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
71c8884f
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
15bcc6f7
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.examples.rubik;
|
21 |
|
|
|
22 |
6e18bd32
|
Leszek Koltunski
|
import java.util.Random;
|
23 |
|
|
|
24 |
15bcc6f7
|
Leszek Koltunski
|
import android.graphics.Bitmap;
|
25 |
|
|
import android.graphics.Canvas;
|
26 |
|
|
import android.graphics.Paint;
|
27 |
|
|
|
28 |
|
|
import org.distorted.library.effect.MatrixEffectMove;
|
29 |
|
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
30 |
|
|
import org.distorted.library.effect.MatrixEffectRotate;
|
31 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
32 |
|
|
import org.distorted.library.effect.VertexEffectSink;
|
33 |
|
|
import org.distorted.library.main.DistortedEffects;
|
34 |
e3a72781
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedNode;
|
35 |
15bcc6f7
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
36 |
|
|
import org.distorted.library.main.DistortedTexture;
|
37 |
|
|
import org.distorted.library.mesh.MeshCubes;
|
38 |
b62eb334
|
Leszek Koltunski
|
import org.distorted.library.message.EffectListener;
|
39 |
15bcc6f7
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
40 |
|
|
import org.distorted.library.type.Static1D;
|
41 |
|
|
import org.distorted.library.type.Static3D;
|
42 |
|
|
import org.distorted.library.type.Static4D;
|
43 |
|
|
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
|
|
46 |
|
|
class RubikCube
|
47 |
|
|
{
|
48 |
da231553
|
Leszek Koltunski
|
private static final int VECTX = 0;
|
49 |
|
|
private static final int VECTY = 1;
|
50 |
|
|
private static final int VECTZ = 2;
|
51 |
6e18bd32
|
Leszek Koltunski
|
|
52 |
|
|
private static final int ROTATION_MILLISEC = 1500;
|
53 |
15bcc6f7
|
Leszek Koltunski
|
|
54 |
|
|
private static final Static3D VectX = new Static3D(1,0,0);
|
55 |
|
|
private static final Static3D VectY = new Static3D(0,1,0);
|
56 |
|
|
private static final Static3D VectZ = new Static3D(0,0,1);
|
57 |
|
|
|
58 |
da231553
|
Leszek Koltunski
|
private static Random mRnd = new Random(0);
|
59 |
|
|
|
60 |
e3a72781
|
Leszek Koltunski
|
private DistortedNode[][][] mNodes;
|
61 |
15bcc6f7
|
Leszek Koltunski
|
private MeshCubes[][][] mCubes;
|
62 |
|
|
private DistortedEffects[][][] mEffects;
|
63 |
|
|
private Static3D[][][] mRotationAxis;
|
64 |
|
|
private Dynamic1D[][][] mRotationAngle;
|
65 |
8d5a8e06
|
Leszek Koltunski
|
private MatrixEffectRotate[][][] mRotate;
|
66 |
15bcc6f7
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
67 |
|
|
private int mSize;
|
68 |
|
|
|
69 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70 |
|
|
|
71 |
ba9ae2c8
|
Leszek Koltunski
|
RubikCube(int size, Static3D scale)
|
72 |
15bcc6f7
|
Leszek Koltunski
|
{
|
73 |
|
|
mSize = size;
|
74 |
|
|
|
75 |
687263cc
|
Leszek Koltunski
|
mTexture = new DistortedTexture();
|
76 |
15bcc6f7
|
Leszek Koltunski
|
|
77 |
e3a72781
|
Leszek Koltunski
|
mNodes = new DistortedNode[mSize][mSize][mSize];
|
78 |
15bcc6f7
|
Leszek Koltunski
|
mCubes = new MeshCubes[mSize][mSize][mSize];
|
79 |
|
|
mEffects = new DistortedEffects[mSize][mSize][mSize];
|
80 |
|
|
mRotationAxis = new Static3D[mSize][mSize][mSize];
|
81 |
|
|
mRotationAngle = new Dynamic1D[mSize][mSize][mSize];
|
82 |
8d5a8e06
|
Leszek Koltunski
|
mRotate = new MatrixEffectRotate[mSize][mSize][mSize];
|
83 |
15bcc6f7
|
Leszek Koltunski
|
|
84 |
|
|
Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
|
85 |
|
|
|
86 |
ba9ae2c8
|
Leszek Koltunski
|
Static3D center = new Static3D(0,0,0);
|
87 |
55908771
|
Leszek Koltunski
|
Static4D region = new Static4D(0,0,0, 0.72f);
|
88 |
15bcc6f7
|
Leszek Koltunski
|
|
89 |
8a40abf4
|
Leszek Koltunski
|
VertexEffectSink sinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), center, region );
|
90 |
15bcc6f7
|
Leszek Koltunski
|
MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
|
91 |
6e18bd32
|
Leszek Koltunski
|
MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion( initializeQuat(), center);
|
92 |
15bcc6f7
|
Leszek Koltunski
|
|
93 |
|
|
// 3x2 bitmap = 6 squares:
|
94 |
|
|
//
|
95 |
|
|
// RED GREEN BLUE
|
96 |
|
|
// YELLOW WHITE BROWN
|
97 |
|
|
|
98 |
|
|
final float ze = 0.0f;
|
99 |
|
|
final float ot = 1.0f/3.0f;
|
100 |
|
|
final float tt = 2.0f/3.0f;
|
101 |
|
|
final float oh = 1.0f/2.0f;
|
102 |
|
|
final float of = 1.0f/40.0f;
|
103 |
|
|
|
104 |
|
|
final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
|
105 |
|
|
final Static4D mapBack = new Static4D(tt,ze, tt+ot,ze+oh);
|
106 |
|
|
final Static4D mapLeft = new Static4D(ot,ze, ot+ot,ze+oh);
|
107 |
|
|
final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
|
108 |
|
|
final Static4D mapTop = new Static4D(tt,oh, tt+ot,oh+oh);
|
109 |
|
|
final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
|
110 |
|
|
|
111 |
|
|
final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
|
112 |
|
|
|
113 |
|
|
Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
|
114 |
|
|
float nc = 0.5f*(mSize-1);
|
115 |
|
|
int vertices = (int)(24.0f/mSize + 2.0f);
|
116 |
|
|
|
117 |
|
|
for(int x = 0; x< mSize; x++)
|
118 |
|
|
for(int y = 0; y< mSize; y++)
|
119 |
|
|
for(int z = 0; z< mSize; z++)
|
120 |
|
|
{
|
121 |
|
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
|
122 |
|
|
{
|
123 |
|
|
tmpLeft = (x== 0 ? mapLeft :mapBlack);
|
124 |
|
|
tmpRight = (x== mSize-1 ? mapRight :mapBlack);
|
125 |
|
|
tmpFront = (z== mSize-1 ? mapFront :mapBlack);
|
126 |
|
|
tmpBack = (z== 0 ? mapBack :mapBlack);
|
127 |
|
|
tmpTop = (y== mSize-1 ? mapTop :mapBlack);
|
128 |
|
|
tmpBottom= (y== 0 ? mapBottom:mapBlack);
|
129 |
|
|
|
130 |
|
|
mCubes[x][y][z] = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
|
131 |
698ad0a8
|
Leszek Koltunski
|
|
132 |
55908771
|
Leszek Koltunski
|
cubeVectors[x][y][z] = new Static3D( x-nc, y-nc, z-nc );
|
133 |
15bcc6f7
|
Leszek Koltunski
|
mRotationAngle[x][y][z] = new Dynamic1D();
|
134 |
|
|
mRotationAxis[x][y][z] = new Static3D(1,0,0);
|
135 |
6e18bd32
|
Leszek Koltunski
|
|
136 |
|
|
mRotationAngle[x][y][z].add(new Static1D(0.0f));
|
137 |
|
|
mRotationAngle[x][y][z].add(new Static1D(0.0f));
|
138 |
8d5a8e06
|
Leszek Koltunski
|
mRotate[x][y][z] = new MatrixEffectRotate( mRotationAngle[x][y][z], mRotationAxis[x][y][z], center);
|
139 |
15bcc6f7
|
Leszek Koltunski
|
|
140 |
698ad0a8
|
Leszek Koltunski
|
mEffects[x][y][z] = new DistortedEffects();
|
141 |
15bcc6f7
|
Leszek Koltunski
|
mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
|
142 |
dae661e9
|
Leszek Koltunski
|
mEffects[x][y][z].apply( mRotate[x][y][z] );
|
143 |
|
|
mEffects[x][y][z].apply(quatEffect);
|
144 |
|
|
mEffects[x][y][z].apply(scaleEffect);
|
145 |
|
|
mEffects[x][y][z].apply(sinkEffect);
|
146 |
15bcc6f7
|
Leszek Koltunski
|
}
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151 |
da231553
|
Leszek Koltunski
|
// Initial rotation of the cube. Something semi-random that looks good.
|
152 |
15bcc6f7
|
Leszek Koltunski
|
|
153 |
da231553
|
Leszek Koltunski
|
private Static4D initializeQuat()
|
154 |
|
|
{
|
155 |
|
|
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
156 |
|
|
}
|
157 |
e3a72781
|
Leszek Koltunski
|
|
158 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159 |
|
|
|
160 |
da231553
|
Leszek Koltunski
|
void attachToScreen(DistortedScreen screen)
|
161 |
|
|
{
|
162 |
|
|
for(int x=0; x<mSize; x++)
|
163 |
|
|
for(int y=0; y<mSize; y++)
|
164 |
|
|
for(int z=0; z<mSize; z++)
|
165 |
|
|
{
|
166 |
|
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
167 |
|
|
{
|
168 |
|
|
mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
|
169 |
|
|
screen.attach(mNodes[x][y][z]);
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
15bcc6f7
|
Leszek Koltunski
|
|
174 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
da231553
|
Leszek Koltunski
|
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
|
176 |
|
|
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
|
177 |
|
|
// memory leak.
|
178 |
15bcc6f7
|
Leszek Koltunski
|
|
179 |
da231553
|
Leszek Koltunski
|
void releaseResources()
|
180 |
|
|
{
|
181 |
|
|
mTexture.markForDeletion();
|
182 |
|
|
|
183 |
|
|
for(int x=0; x<mSize; x++)
|
184 |
|
|
for(int y=0; y<mSize; y++)
|
185 |
|
|
for(int z=0; z<mSize; z++)
|
186 |
|
|
{
|
187 |
|
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
188 |
|
|
{
|
189 |
|
|
mCubes[x][y][z].markForDeletion();
|
190 |
|
|
mNodes[x][y][z].markForDeletion();
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
b62eb334
|
Leszek Koltunski
|
|
195 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196 |
|
|
|
197 |
da231553
|
Leszek Koltunski
|
void addRotation(EffectListener listener)
|
198 |
6e18bd32
|
Leszek Koltunski
|
{
|
199 |
da231553
|
Leszek Koltunski
|
mRnd.setSeed(System.currentTimeMillis());
|
200 |
|
|
|
201 |
|
|
int vector = mRnd.nextInt(3);
|
202 |
|
|
int rotRow = mRnd.nextInt(mSize);
|
203 |
|
|
|
204 |
|
|
boolean first = true;
|
205 |
|
|
Static3D axis = VectX;
|
206 |
|
|
|
207 |
|
|
switch(vector)
|
208 |
|
|
{
|
209 |
|
|
case VECTX: axis = VectX; break;
|
210 |
|
|
case VECTY: axis = VectY; break;
|
211 |
|
|
case VECTZ: axis = VectZ; break;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
for(int x=0; x<mSize; x++)
|
215 |
|
|
for(int y=0; y<mSize; y++)
|
216 |
|
|
for(int z=0; z<mSize; z++)
|
217 |
|
|
if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
|
218 |
|
|
{
|
219 |
|
|
if( belongsToRotation(x,y,z,vector,rotRow) )
|
220 |
|
|
{
|
221 |
|
|
mRotationAxis[x][y][z].set(axis);
|
222 |
2666a48c
|
Leszek Koltunski
|
mRotationAngle[x][y][z].setDuration(ROTATION_MILLISEC);
|
223 |
|
|
mRotationAngle[x][y][z].resetToBeginning();
|
224 |
da231553
|
Leszek Koltunski
|
mRotationAngle[x][y][z].setPoint(1,90.0f);
|
225 |
|
|
|
226 |
|
|
if( first )
|
227 |
|
|
{
|
228 |
|
|
first = false;
|
229 |
8d5a8e06
|
Leszek Koltunski
|
mRotate[x][y][z].notifyWhenFinished(listener);
|
230 |
da231553
|
Leszek Koltunski
|
}
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
6e18bd32
|
Leszek Koltunski
|
}
|
234 |
15bcc6f7
|
Leszek Koltunski
|
|
235 |
6e18bd32
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236 |
15bcc6f7
|
Leszek Koltunski
|
|
237 |
da231553
|
Leszek Koltunski
|
private boolean belongsToRotation(int x, int y, int z, int vector, int row)
|
238 |
|
|
{
|
239 |
|
|
switch(vector)
|
240 |
|
|
{
|
241 |
|
|
case VECTX: return x==row;
|
242 |
|
|
case VECTY: return y==row;
|
243 |
|
|
case VECTZ: return z==row;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
return false;
|
247 |
|
|
}
|
248 |
15bcc6f7
|
Leszek Koltunski
|
|
249 |
8a40abf4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250 |
|
|
|
251 |
da231553
|
Leszek Koltunski
|
private float getSinkStrength()
|
252 |
|
|
{
|
253 |
|
|
switch(mSize)
|
254 |
|
|
{
|
255 |
|
|
case 1 : return 1.1f;
|
256 |
|
|
case 2 : return 1.5f;
|
257 |
|
|
case 3 : return 1.8f;
|
258 |
|
|
case 4 : return 2.0f;
|
259 |
|
|
default: return 3.0f - 4.0f/mSize;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
8a40abf4
|
Leszek Koltunski
|
|
263 |
15bcc6f7
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264 |
|
|
|
265 |
da231553
|
Leszek Koltunski
|
void createTexture()
|
266 |
|
|
{
|
267 |
|
|
Bitmap bitmap;
|
268 |
|
|
|
269 |
|
|
final int S = 128;
|
270 |
|
|
final int W = 3*S;
|
271 |
|
|
final int H = 2*S;
|
272 |
|
|
final int R = S/10;
|
273 |
|
|
final int M = S/20;
|
274 |
|
|
|
275 |
|
|
Paint paint = new Paint();
|
276 |
|
|
bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
|
277 |
|
|
Canvas canvas = new Canvas(bitmap);
|
278 |
|
|
|
279 |
|
|
paint.setAntiAlias(true);
|
280 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
281 |
|
|
paint.setStyle(Paint.Style.FILL);
|
282 |
|
|
|
283 |
|
|
// 3x2 bitmap = 6 squares:
|
284 |
|
|
//
|
285 |
|
|
// RED GREEN BLUE
|
286 |
|
|
// YELLOW WHITE BROWN
|
287 |
|
|
|
288 |
|
|
paint.setColor(0xff000000); // BLACK BACKGROUND
|
289 |
|
|
canvas.drawRect(0, 0, W, H, paint); //
|
290 |
|
|
|
291 |
|
|
paint.setColor(0xffff0000); // RED
|
292 |
|
|
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); //
|
293 |
|
|
paint.setColor(0xff00ff00); // GREEN
|
294 |
|
|
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); //
|
295 |
|
|
paint.setColor(0xff0000ff); // BLUE
|
296 |
|
|
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); //
|
297 |
|
|
paint.setColor(0xffffff00); // YELLOW
|
298 |
|
|
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); //
|
299 |
|
|
paint.setColor(0xffffffff); // WHITE
|
300 |
|
|
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
|
301 |
|
|
paint.setColor(0xffb5651d); // BROWN
|
302 |
|
|
canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
|
303 |
|
|
|
304 |
|
|
mTexture.setTexture(bitmap);
|
305 |
|
|
}
|
306 |
15bcc6f7
|
Leszek Koltunski
|
|
307 |
7f986357
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308 |
|
|
|
309 |
da231553
|
Leszek Koltunski
|
int getSize()
|
310 |
|
|
{
|
311 |
|
|
return mSize;
|
312 |
|
|
}
|
313 |
15bcc6f7
|
Leszek Koltunski
|
}
|