Revision e4c0057e
Added by Leszek Koltunski over 5 years ago
src/main/java/org/distorted/examples/rubik/RubikRenderer.java | ||
---|---|---|
24 | 24 |
import android.graphics.Paint; |
25 | 25 |
import android.opengl.GLSurfaceView; |
26 | 26 |
|
27 |
import org.distorted.library.effect.EffectName; |
|
27 | 28 |
import org.distorted.library.effect.MatrixEffectMove; |
28 | 29 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
29 | 30 |
import org.distorted.library.effect.MatrixEffectScale; |
31 |
import org.distorted.library.effect.PostprocessEffectGlow; |
|
30 | 32 |
import org.distorted.library.main.Distorted; |
31 | 33 |
import org.distorted.library.main.DistortedEffects; |
32 | 34 |
import org.distorted.library.main.DistortedScreen; |
33 | 35 |
import org.distorted.library.main.DistortedTexture; |
34 | 36 |
import org.distorted.library.mesh.MeshCubes; |
37 |
import org.distorted.library.type.Static1D; |
|
35 | 38 |
import org.distorted.library.type.Static3D; |
36 | 39 |
import org.distorted.library.type.Static4D; |
37 | 40 |
|
... | ... | |
42 | 45 |
|
43 | 46 |
class RubikRenderer implements GLSurfaceView.Renderer |
44 | 47 |
{ |
45 |
private static final int CUBE_SIZE = 3;
|
|
48 |
static final int NUM_CUBES = 3;
|
|
46 | 49 |
private static final int VERTICES = 5; |
47 | 50 |
private static final int SIZE = 200; |
48 | 51 |
|
52 |
private static final float CUBE_SCREEN_RATIO = 0.5f; |
|
53 |
|
|
49 | 54 |
private GLSurfaceView mView; |
50 | 55 |
private DistortedTexture mTexture; |
51 | 56 |
private DistortedScreen mScreen; |
... | ... | |
53 | 58 |
private MeshCubes[][][] mCubes; |
54 | 59 |
private DistortedEffects[][][] mEffects; |
55 | 60 |
|
56 |
Static4D mQuat1, mQuat2; |
|
61 |
private int mScreenWidth, mScreenHeight; |
|
62 |
private int mLastRow, mLastCol, mLastSli; |
|
63 |
|
|
64 |
private Static1D mGlowRadius; |
|
65 |
private Static4D mGlowColor; |
|
66 |
|
|
67 |
Static4D mQuatCurrent, mQuatAccumulated; |
|
57 | 68 |
int mScreenMin; |
58 | 69 |
|
59 | 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
63 | 74 |
mView = v; |
64 | 75 |
|
65 | 76 |
mScreen = new DistortedScreen(); |
66 |
// mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
77 |
mScreen.setProjection(90.0f, 0.1f);
|
|
67 | 78 |
|
68 |
mQuat1 = new Static4D( 0, 0, 0, 1); // unity quaternion
|
|
69 |
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); // something semi-random that looks good
|
|
79 |
mQuatCurrent = new Static4D( 0, 0, 0, 1); // unity quaternion
|
|
80 |
mQuatAccumulated = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); // something semi-random that looks good
|
|
70 | 81 |
|
71 |
mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
|
72 |
mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
|
73 |
Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
|
82 |
mCubes = new MeshCubes[NUM_CUBES][NUM_CUBES][NUM_CUBES];
|
|
83 |
mEffects = new DistortedEffects[NUM_CUBES][NUM_CUBES][NUM_CUBES];
|
|
84 |
Static3D[][][] cubeVectors = new Static3D[NUM_CUBES][NUM_CUBES][NUM_CUBES];
|
|
74 | 85 |
|
75 | 86 |
mMove = new Static3D(0,0,0); |
76 | 87 |
mScale = new Static3D(1,1,1); |
77 | 88 |
mCenter= new Static3D(0,0,0); |
78 | 89 |
|
90 |
mLastRow = mLastCol = mLastSli = 0; |
|
91 |
|
|
92 |
mGlowRadius = new Static1D(10); |
|
93 |
mGlowColor = new Static4D(1.0f,1.0f,1.0f,0.6f); |
|
94 |
|
|
79 | 95 |
MatrixEffectMove move = new MatrixEffectMove(mMove); |
80 | 96 |
MatrixEffectScale scale = new MatrixEffectScale(mScale); |
81 |
MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, mCenter);
|
|
82 |
MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, mCenter);
|
|
97 |
MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuatCurrent , mCenter);
|
|
98 |
MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuatAccumulated, mCenter);
|
|
83 | 99 |
|
84 | 100 |
// 3x2 bitmap = 6 squares: |
85 | 101 |
// |
... | ... | |
103 | 119 |
|
104 | 120 |
Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom; |
105 | 121 |
|
106 |
for(int x=0; x<CUBE_SIZE; x++)
|
|
107 |
for(int y=0; y<CUBE_SIZE; y++)
|
|
108 |
for(int z=0; z<CUBE_SIZE; z++)
|
|
122 |
for(int x = 0; x< NUM_CUBES; x++)
|
|
123 |
for(int y = 0; y< NUM_CUBES; y++)
|
|
124 |
for(int z = 0; z< NUM_CUBES; z++)
|
|
109 | 125 |
{ |
110 | 126 |
tmpLeft = (x== 0 ? mapLeft :mapBlack); |
111 |
tmpRight = (x==CUBE_SIZE-1 ? mapRight :mapBlack);
|
|
112 |
tmpFront = (z==CUBE_SIZE-1 ? mapFront :mapBlack);
|
|
127 |
tmpRight = (x== NUM_CUBES -1 ? mapRight :mapBlack);
|
|
128 |
tmpFront = (z== NUM_CUBES -1 ? mapFront :mapBlack);
|
|
113 | 129 |
tmpBack = (z== 0 ? mapBack :mapBlack); |
114 |
tmpTop = (y==CUBE_SIZE-1 ? mapTop :mapBlack);
|
|
130 |
tmpTop = (y== NUM_CUBES -1 ? mapTop :mapBlack);
|
|
115 | 131 |
tmpBottom= (y== 0 ? mapBottom:mapBlack); |
116 | 132 |
|
117 | 133 |
mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom); |
118 | 134 |
|
119 |
cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
|
|
135 |
cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(NUM_CUBES -1)), SIZE*(y-0.5f*(NUM_CUBES -1)), SIZE*(z-0.5f*(NUM_CUBES -1)) );
|
|
120 | 136 |
|
121 | 137 |
mEffects[x][y][z] = new DistortedEffects(); |
122 | 138 |
|
... | ... | |
139 | 155 |
|
140 | 156 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
141 | 157 |
{ |
158 |
mScreenWidth = width; |
|
159 |
mScreenHeight= height; |
|
142 | 160 |
mScreenMin = width<height ? width:height; |
143 | 161 |
|
144 | 162 |
float w = mTexture.getWidth(); |
145 | 163 |
float h = mTexture.getHeight(); |
146 | 164 |
float d = mTexture.getDepth(mCubes[0][0][0]); |
147 | 165 |
|
148 |
float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
|
|
166 |
float factor = CUBE_SCREEN_RATIO*(width>height ? height/h:width/w)/ NUM_CUBES;
|
|
149 | 167 |
|
150 | 168 |
mCenter.set(w/2,h/2,d/2); |
151 | 169 |
mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , -factor*d/2 ); |
... | ... | |
183 | 201 |
canvas.drawRect(0, 0, W, H, paint); // |
184 | 202 |
|
185 | 203 |
paint.setColor(0xffff0000); // RED |
186 |
canvas.drawRoundRect( 0+M, 0+M, S-M, S-M, R, R, paint); //
|
|
204 |
canvas.drawRoundRect( M, M, S-M, S-M, R, R, paint); //
|
|
187 | 205 |
paint.setColor(0xff00ff00); // GREEN |
188 |
canvas.drawRoundRect( S+M, 0+M, 2*S-M, S-M, R, R, paint); //
|
|
206 |
canvas.drawRoundRect( S+M, M, 2*S-M, S-M, R, R, paint); //
|
|
189 | 207 |
paint.setColor(0xff0000ff); // BLUE |
190 |
canvas.drawRoundRect(2*S+M, 0+M, 3*S-M, S-M, R, R, paint); //
|
|
208 |
canvas.drawRoundRect(2*S+M, M, 3*S-M, S-M, R, R, paint); //
|
|
191 | 209 |
paint.setColor(0xffffff00); // YELLOW |
192 |
canvas.drawRoundRect( 0+M, S+M, S-M, 2*S-M, R, R, paint); //
|
|
210 |
canvas.drawRoundRect( M, S+M, S-M, 2*S-M, R, R, paint); //
|
|
193 | 211 |
paint.setColor(0xffffffff); // WHITE |
194 | 212 |
canvas.drawRoundRect( S+M, S+M, 2*S-M, 2*S-M, R, R, paint); // |
195 | 213 |
paint.setColor(0xffb5651d); // BROWN |
... | ... | |
200 | 218 |
|
201 | 219 |
mScreen.detachAll(); |
202 | 220 |
|
203 |
for(int x=0; x<CUBE_SIZE; x++)
|
|
204 |
for(int y=0; y<CUBE_SIZE; y++)
|
|
205 |
for(int z=0; z<CUBE_SIZE; z++)
|
|
221 |
for(int x = 0; x< NUM_CUBES; x++)
|
|
222 |
for(int y = 0; y< NUM_CUBES; y++)
|
|
223 |
for(int z = 0; z< NUM_CUBES; z++)
|
|
206 | 224 |
mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]); |
207 | 225 |
|
226 |
PostprocessEffectGlow.enable(); |
|
227 |
|
|
208 | 228 |
try |
209 | 229 |
{ |
210 | 230 |
Distorted.onCreate(mView.getContext()); |
... | ... | |
214 | 234 |
android.util.Log.e("Rubik", ex.getMessage() ); |
215 | 235 |
} |
216 | 236 |
} |
237 |
|
|
238 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
239 |
|
|
240 |
float returnCameraDistance() |
|
241 |
{ |
|
242 |
float fov = mScreen.getFOV(); |
|
243 |
double fovInRadians = (fov*Math.PI) / 180.0; |
|
244 |
double distance = (mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5); |
|
245 |
|
|
246 |
return (float)distance; |
|
247 |
} |
|
248 |
|
|
249 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
250 |
// NUM_CUBES individual little cubes, each SIZE in size, times 'scaleFactor' (see onSurfaceChanged) |
|
251 |
|
|
252 |
float returnCubeSize() |
|
253 |
{ |
|
254 |
float w = mTexture.getWidth(); |
|
255 |
float h = mTexture.getHeight(); |
|
256 |
float max = (mScreenWidth>mScreenHeight ? mScreenHeight/h:mScreenWidth/w); |
|
257 |
float scaleFactor = CUBE_SCREEN_RATIO*max/NUM_CUBES; |
|
258 |
|
|
259 |
return scaleFactor*NUM_CUBES*SIZE; |
|
260 |
} |
|
261 |
|
|
262 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
263 |
|
|
264 |
float getScreenWidth() |
|
265 |
{ |
|
266 |
return mScreenWidth; |
|
267 |
} |
|
268 |
|
|
269 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
270 |
|
|
271 |
float getScreenHeight() |
|
272 |
{ |
|
273 |
return mScreenHeight; |
|
274 |
} |
|
275 |
|
|
276 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
277 |
|
|
278 |
void abortLastEffect() |
|
279 |
{ |
|
280 |
mEffects[mLastCol][mLastRow][mLastSli].abortByName(EffectName.GLOW); |
|
281 |
} |
|
282 |
|
|
283 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
284 |
|
|
285 |
void applyNewEffect(int col, int row, int sli) |
|
286 |
{ |
|
287 |
mLastCol = col; |
|
288 |
mLastRow = row; |
|
289 |
mLastSli = sli; |
|
290 |
|
|
291 |
mEffects[mLastCol][mLastRow][mLastSli].apply(new PostprocessEffectGlow(mGlowRadius,mGlowColor)); |
|
292 |
} |
|
217 | 293 |
} |
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java | ||
---|---|---|
25 | 25 |
import android.opengl.GLSurfaceView; |
26 | 26 |
import android.view.MotionEvent; |
27 | 27 |
|
28 |
import org.distorted.library.type.Static4D; |
|
29 |
|
|
28 | 30 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 | 31 |
|
30 | 32 |
class RubikSurfaceView extends GLSurfaceView |
31 | 33 |
{ |
34 |
private final static int ERROR =-1; |
|
35 |
private final static int FRONT = 0; |
|
36 |
private final static int BACK = 1; |
|
37 |
private final static int LEFT = 2; |
|
38 |
private final static int RIGHT = 3; |
|
39 |
private final static int TOP = 4; |
|
40 |
private final static int BOTTOM = 5; |
|
41 |
|
|
42 |
private boolean mDragging; |
|
32 | 43 |
private int mX, mY; |
44 |
private int mTouchedRow, mTouchedCol, mTouchedSli; |
|
33 | 45 |
private RubikRenderer mRenderer; |
34 | 46 |
|
35 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
38 | 50 |
{ |
39 | 51 |
super(context); |
40 | 52 |
|
41 |
mX = -1; |
|
42 |
mY = -1; |
|
43 |
|
|
53 |
mDragging = false; |
|
44 | 54 |
mRenderer = new RubikRenderer(this); |
45 | 55 |
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
46 | 56 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
... | ... | |
57 | 67 |
|
58 | 68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
59 | 69 |
|
60 |
@Override public boolean onTouchEvent(MotionEvent event) |
|
70 |
@Override |
|
71 |
public boolean onTouchEvent(MotionEvent event) |
|
61 | 72 |
{ |
62 | 73 |
int action = event.getAction(); |
63 | 74 |
int x = (int)event.getX(); |
... | ... | |
65 | 76 |
|
66 | 77 |
switch(action) |
67 | 78 |
{ |
68 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
69 |
mY = y; |
|
70 |
break; |
|
71 |
|
|
72 |
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 ) |
|
79 |
case MotionEvent.ACTION_DOWN: if( faceTouched(x,y) != ERROR ) |
|
73 | 80 |
{ |
74 |
float px = mY-y; |
|
75 |
float py = mX-x; |
|
76 |
float pz = 0; |
|
77 |
float plen = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
78 |
|
|
79 |
if( plen>0 ) |
|
80 |
{ |
|
81 |
px /= plen; |
|
82 |
py /= plen; |
|
83 |
pz /= plen; |
|
84 |
|
|
85 |
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin); |
|
86 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
87 |
|
|
88 |
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA); |
|
89 |
} |
|
81 |
mRenderer.abortLastEffect(); |
|
82 |
mRenderer.applyNewEffect(mTouchedCol, mTouchedRow, mTouchedSli); |
|
83 |
} |
|
84 |
else |
|
85 |
{ |
|
86 |
mX = x; |
|
87 |
mY = y; |
|
88 |
mDragging = true; |
|
90 | 89 |
} |
91 | 90 |
break; |
92 |
|
|
93 |
case MotionEvent.ACTION_UP : mX = -1; |
|
94 |
mY = -1; |
|
95 |
|
|
96 |
float qx = mRenderer.mQuat1.get1(); |
|
97 |
float qy = mRenderer.mQuat1.get2(); |
|
98 |
float qz = mRenderer.mQuat1.get3(); |
|
99 |
float qw = mRenderer.mQuat1.get4(); |
|
100 |
|
|
101 |
float rx = mRenderer.mQuat2.get1(); |
|
102 |
float ry = mRenderer.mQuat2.get2(); |
|
103 |
float rz = mRenderer.mQuat2.get3(); |
|
104 |
float rw = mRenderer.mQuat2.get4(); |
|
105 |
|
|
106 |
// This is quaternion multiplication. (tx.ty.tz.tw) |
|
107 |
// is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
108 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
109 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
110 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
111 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
112 |
|
|
113 |
// The point of this is so that there are always |
|
114 |
// exactly 2 quaternions: Quat1 representing the rotation |
|
115 |
// accumulating only since the last screen touch, and Quat2 |
|
116 |
// which remembers the combined effect of all previous |
|
117 |
// swipes. |
|
118 |
// We cannot be accumulating an ever-growing list of quaternions |
|
119 |
// and add a new one every time user swipes the screen - there |
|
120 |
// is a limited number of slots in the EffectQueueMatrix! |
|
121 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
|
122 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
|
123 |
|
|
91 |
case MotionEvent.ACTION_MOVE: if( mDragging ) mRenderer.mQuatCurrent.set(quatFromDrag(mX-x,mY-y)); |
|
92 |
break; |
|
93 |
case MotionEvent.ACTION_UP : mDragging = false; |
|
94 |
mRenderer.mQuatAccumulated.set(quatMultiply(mRenderer.mQuatCurrent, mRenderer.mQuatAccumulated)); |
|
95 |
mRenderer.mQuatCurrent.set(0f, 0f, 0f, 1f); |
|
124 | 96 |
break; |
125 | 97 |
} |
126 | 98 |
|
127 | 99 |
return true; |
128 | 100 |
} |
129 | 101 |
|
102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
103 |
// return quat1*quat2 |
|
104 |
|
|
105 |
private Static4D quatMultiply( Static4D quat1, Static4D quat2 ) |
|
106 |
{ |
|
107 |
float qx = quat1.get1(); |
|
108 |
float qy = quat1.get2(); |
|
109 |
float qz = quat1.get3(); |
|
110 |
float qw = quat1.get4(); |
|
111 |
|
|
112 |
float rx = quat2.get1(); |
|
113 |
float ry = quat2.get2(); |
|
114 |
float rz = quat2.get3(); |
|
115 |
float rw = quat2.get4(); |
|
116 |
|
|
117 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
118 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
119 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
120 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
121 |
|
|
122 |
return new Static4D(tx,ty,tz,tw); |
|
123 |
} |
|
124 |
|
|
125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
126 |
|
|
127 |
private Static4D quatFromDrag(float dragX, float dragY) |
|
128 |
{ |
|
129 |
float axisX = dragY; // inverted X and Y - rotation axis is |
|
130 |
float axisY = dragX; // perpendicular to (dragX,dragY) |
|
131 |
float axisZ = 0; |
|
132 |
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); |
|
133 |
|
|
134 |
if( axisL>0 ) |
|
135 |
{ |
|
136 |
axisX /= axisL; |
|
137 |
axisY /= axisL; |
|
138 |
axisZ /= axisL; |
|
139 |
|
|
140 |
float cosA = (float)Math.cos(axisL*Math.PI/mRenderer.mScreenMin); |
|
141 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
142 |
|
|
143 |
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
144 |
} |
|
145 |
|
|
146 |
return new Static4D(0f, 0f, 0f, 1f); |
|
147 |
} |
|
148 |
|
|
149 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
150 |
|
|
151 |
private int returnFrontFace() |
|
152 |
{ |
|
153 |
Static4D rotated = rotateVector(new Static4D(0,0,1,0)); |
|
154 |
|
|
155 |
float rotatedX = rotated.get1(); |
|
156 |
float rotatedY = rotated.get2(); |
|
157 |
float rotatedZ = rotated.get3(); |
|
158 |
|
|
159 |
float absX = rotatedX>0 ? rotatedX : -rotatedX; |
|
160 |
float absY = rotatedY>0 ? rotatedY : -rotatedY; |
|
161 |
float absZ = rotatedZ>0 ? rotatedZ : -rotatedZ; |
|
162 |
|
|
163 |
if( absX>absY && absX>absZ ) return rotatedX>0 ? RIGHT:LEFT; |
|
164 |
if( absY>absX && absY>absZ ) return rotatedY>0 ? TOP:BOTTOM; |
|
165 |
if( absZ>absX && absZ>absY ) return rotatedZ>0 ? FRONT:BACK; |
|
166 |
|
|
167 |
return ERROR; |
|
168 |
} |
|
169 |
|
|
170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 |
|
|
172 |
private boolean faceIsVisible(int face) |
|
173 |
{ |
|
174 |
float cameraDistance = mRenderer.returnCameraDistance(); |
|
175 |
float cubeHalfSize = mRenderer.returnCubeSize()*0.5f; |
|
176 |
|
|
177 |
Static4D rotated = rotateVector(new Static4D(0,0,cameraDistance,0)); |
|
178 |
|
|
179 |
switch(face) |
|
180 |
{ |
|
181 |
case FRONT : return rotated.get3() > cubeHalfSize; |
|
182 |
case BACK : return rotated.get3() < -cubeHalfSize; |
|
183 |
case LEFT : return rotated.get1() < -cubeHalfSize; |
|
184 |
case RIGHT : return rotated.get1() > cubeHalfSize; |
|
185 |
case TOP : return rotated.get2() > cubeHalfSize; |
|
186 |
case BOTTOM: return rotated.get2() < -cubeHalfSize; |
|
187 |
} |
|
188 |
|
|
189 |
return false; |
|
190 |
} |
|
191 |
|
|
192 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
193 |
// rotate 'vector' by quaternion q=mQuatAccumulated^(-1) ( i.e. return (q^-1)*vector*q ) |
|
194 |
|
|
195 |
private Static4D rotateVector(Static4D vector) |
|
196 |
{ |
|
197 |
float qx = mRenderer.mQuatAccumulated.get1(); |
|
198 |
float qy = mRenderer.mQuatAccumulated.get2(); |
|
199 |
float qz = mRenderer.mQuatAccumulated.get3(); |
|
200 |
float qw = mRenderer.mQuatAccumulated.get4(); |
|
201 |
|
|
202 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
|
203 |
Static4D tmp = quatMultiply(quatInverted,vector); |
|
204 |
|
|
205 |
return quatMultiply(tmp,mRenderer.mQuatAccumulated); |
|
206 |
} |
|
207 |
|
|
208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
209 |
|
|
210 |
private int faceTouched(int xTouch, int yTouch) |
|
211 |
{ |
|
212 |
float cameraDistance = mRenderer.returnCameraDistance(); |
|
213 |
float halfScrWidth = mRenderer.getScreenWidth()*0.5f; |
|
214 |
float halfScrHeight = mRenderer.getScreenHeight()*0.5f; |
|
215 |
float cubeHalfSize = mRenderer.returnCubeSize()*0.5f; |
|
216 |
|
|
217 |
Static4D cameraPoint = new Static4D( 0, 0, cameraDistance, 0); |
|
218 |
Static4D touchPoint = new Static4D(xTouch-halfScrWidth, halfScrHeight-yTouch, 0, 0); |
|
219 |
|
|
220 |
Static4D rotatedCamera = rotateVector(cameraPoint); |
|
221 |
Static4D rotatedTouchPoint= rotateVector(touchPoint); |
|
222 |
|
|
223 |
float camX = rotatedCamera.get1(); |
|
224 |
float camY = rotatedCamera.get2(); |
|
225 |
float camZ = rotatedCamera.get3(); |
|
226 |
|
|
227 |
float poiX = rotatedTouchPoint.get1(); |
|
228 |
float poiY = rotatedTouchPoint.get2(); |
|
229 |
float poiZ = rotatedTouchPoint.get3(); |
|
230 |
|
|
231 |
if( faceIsVisible(FRONT) ) |
|
232 |
{ |
|
233 |
if( poiZ!= camZ ) |
|
234 |
{ |
|
235 |
float A = (cubeHalfSize-camZ)/(poiZ-camZ); |
|
236 |
float X = (poiX-camX)*A + camX; |
|
237 |
float Y = (poiY-camY)*A + camY; |
|
238 |
float qX= (X+cubeHalfSize) / (2*cubeHalfSize); |
|
239 |
float qY= (Y+cubeHalfSize) / (2*cubeHalfSize); |
|
240 |
|
|
241 |
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 ) |
|
242 |
{ |
|
243 |
mTouchedCol = (int)(qX*RubikRenderer.NUM_CUBES); |
|
244 |
mTouchedRow = (int)(qY*RubikRenderer.NUM_CUBES); |
|
245 |
mTouchedSli = RubikRenderer.NUM_CUBES - 1; |
|
246 |
return FRONT; |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
if( faceIsVisible(BACK) ) |
|
251 |
{ |
|
252 |
if( poiZ!= camZ ) |
|
253 |
{ |
|
254 |
float A = (-cubeHalfSize-camZ)/(poiZ-camZ); |
|
255 |
float X = (poiX-camX)*A + camX; |
|
256 |
float Y = (poiY-camY)*A + camY; |
|
257 |
float qX= (X+cubeHalfSize) / (2*cubeHalfSize); |
|
258 |
float qY= (Y+cubeHalfSize) / (2*cubeHalfSize); |
|
259 |
|
|
260 |
if( qX<=1 && qX>=0 && qY<=1 && qY>=0 ) |
|
261 |
{ |
|
262 |
mTouchedCol = (int)( qX*RubikRenderer.NUM_CUBES); |
|
263 |
mTouchedRow = (int)( qY*RubikRenderer.NUM_CUBES); |
|
264 |
mTouchedSli = 0; |
|
265 |
return BACK; |
|
266 |
} |
|
267 |
} |
|
268 |
} |
|
269 |
if( faceIsVisible(LEFT) ) |
|
270 |
{ |
|
271 |
if( poiX!= camX ) |
|
272 |
{ |
|
273 |
float A = (-cubeHalfSize-camX)/(poiX-camX); |
|
274 |
float Y = (poiY-camY)*A + camY; |
|
275 |
float Z = (poiZ-camZ)*A + camZ; |
|
276 |
float qY= (Y+cubeHalfSize) / (2*cubeHalfSize); |
|
277 |
float qZ= (Z+cubeHalfSize) / (2*cubeHalfSize); |
|
278 |
|
|
279 |
if( qZ<=1 && qZ>=0 && qY<=1 && qY>=0 ) |
|
280 |
{ |
|
281 |
mTouchedCol = 0; |
|
282 |
mTouchedRow = (int)( qY*RubikRenderer.NUM_CUBES); |
|
283 |
mTouchedSli = (int)( qZ*RubikRenderer.NUM_CUBES); |
|
284 |
return LEFT; |
|
285 |
} |
|
286 |
} |
|
287 |
} |
|
288 |
if( faceIsVisible(RIGHT) ) |
|
289 |
{ |
|
290 |
if( poiX!= camX ) |
|
291 |
{ |
|
292 |
float A = (cubeHalfSize-camX)/(poiX-camX); |
|
293 |
float Y = (poiY-camY)*A + camY; |
|
294 |
float Z = (poiZ-camZ)*A + camZ; |
|
295 |
float qY= (Y+cubeHalfSize) / (2*cubeHalfSize); |
|
296 |
float qZ= (Z+cubeHalfSize) / (2*cubeHalfSize); |
|
297 |
|
|
298 |
if( qZ<=1 && qZ>=0 && qY<=1 && qY>=0 ) |
|
299 |
{ |
|
300 |
mTouchedCol = RubikRenderer.NUM_CUBES -1; |
|
301 |
mTouchedRow = (int)( qY*RubikRenderer.NUM_CUBES); |
|
302 |
mTouchedSli = (int)( qZ*RubikRenderer.NUM_CUBES); |
|
303 |
return RIGHT; |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
if( faceIsVisible(TOP) ) |
|
308 |
{ |
|
309 |
if( poiY!= camY ) |
|
310 |
{ |
|
311 |
float A = (cubeHalfSize-camY)/(poiY-camY); |
|
312 |
float X = (poiX-camX)*A + camX; |
|
313 |
float Z = (poiZ-camZ)*A + camZ; |
|
314 |
float qX= (X+cubeHalfSize) / (2*cubeHalfSize); |
|
315 |
float qZ= (Z+cubeHalfSize) / (2*cubeHalfSize); |
|
316 |
|
|
317 |
if( qZ<=1 && qZ>=0 && qX<=1 && qX>=0 ) |
|
318 |
{ |
|
319 |
mTouchedCol = (int)( qX*RubikRenderer.NUM_CUBES); |
|
320 |
mTouchedRow = RubikRenderer.NUM_CUBES -1; |
|
321 |
mTouchedSli = (int)( qZ*RubikRenderer.NUM_CUBES); |
|
322 |
return TOP; |
|
323 |
} |
|
324 |
} |
|
325 |
} |
|
326 |
if( faceIsVisible(BOTTOM) ) |
|
327 |
{ |
|
328 |
if( poiY!= camY ) |
|
329 |
{ |
|
330 |
float A = (-cubeHalfSize-camY)/(poiY-camY); |
|
331 |
float X = (poiX-camX)*A + camX; |
|
332 |
float Z = (poiZ-camZ)*A + camZ; |
|
333 |
float qX= (X+cubeHalfSize) / (2*cubeHalfSize); |
|
334 |
float qZ= (Z+cubeHalfSize) / (2*cubeHalfSize); |
|
335 |
|
|
336 |
if( qZ<=1 && qZ>=0 && qX<=1 && qX>=0 ) |
|
337 |
{ |
|
338 |
mTouchedCol = (int)( qX*RubikRenderer.NUM_CUBES); |
|
339 |
mTouchedRow = 0; |
|
340 |
mTouchedSli = (int)( qZ*RubikRenderer.NUM_CUBES); |
|
341 |
return BOTTOM; |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
|
|
346 |
mTouchedRow = -1; |
|
347 |
mTouchedCol = -1; |
|
348 |
mTouchedSli = -1; |
|
349 |
|
|
350 |
return ERROR; |
|
351 |
} |
|
130 | 352 |
} |
131 | 353 |
|
Also available in: Unified diff
Finish the Rubik app.