1
|
|
2
|
package org.distortedandroid.examples.cubes;
|
3
|
|
4
|
import org.distortedandroid.examples.R;
|
5
|
|
6
|
import android.content.Context;
|
7
|
import android.opengl.GLSurfaceView;
|
8
|
import android.os.Build;
|
9
|
import android.util.AttributeSet;
|
10
|
import android.view.MotionEvent;
|
11
|
import android.widget.Toast;
|
12
|
|
13
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
14
|
|
15
|
class CubesSurfaceView extends GLSurfaceView
|
16
|
{
|
17
|
int mX, mY;
|
18
|
CubesRenderer mRenderer;
|
19
|
|
20
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
21
|
|
22
|
public CubesSurfaceView(Context c, AttributeSet attrs)
|
23
|
{
|
24
|
super(c,attrs);
|
25
|
|
26
|
mX = -1;
|
27
|
mY = -1;
|
28
|
|
29
|
if(!isInEditMode())
|
30
|
{
|
31
|
setEGLContextClientVersion(2);
|
32
|
|
33
|
if( Build.FINGERPRINT.startsWith("generic") )
|
34
|
{
|
35
|
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
36
|
}
|
37
|
|
38
|
mRenderer = new CubesRenderer(this);
|
39
|
|
40
|
setRenderer(mRenderer);
|
41
|
|
42
|
Toast.makeText(c, R.string.example_cubes_toast , Toast.LENGTH_SHORT).show();
|
43
|
}
|
44
|
}
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
@Override public boolean onTouchEvent(MotionEvent event)
|
49
|
{
|
50
|
int action = event.getAction();
|
51
|
int x = (int)event.getX();
|
52
|
int y = (int)event.getY();
|
53
|
|
54
|
switch(action)
|
55
|
{
|
56
|
case MotionEvent.ACTION_DOWN: mX = x;
|
57
|
mY = y;
|
58
|
break;
|
59
|
|
60
|
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
|
61
|
{
|
62
|
float px = mY-y;
|
63
|
float py = mX-x;
|
64
|
float pz = 0;
|
65
|
float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
|
66
|
|
67
|
if( plen>0 )
|
68
|
{
|
69
|
px /= plen;
|
70
|
py /= plen;
|
71
|
pz /= plen;
|
72
|
|
73
|
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
|
74
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
75
|
|
76
|
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
|
77
|
}
|
78
|
}
|
79
|
break;
|
80
|
|
81
|
case MotionEvent.ACTION_UP : mX = -1;
|
82
|
mY = -1;
|
83
|
|
84
|
float qx = mRenderer.mQuat1.getX();
|
85
|
float qy = mRenderer.mQuat1.getY();
|
86
|
float qz = mRenderer.mQuat1.getZ();
|
87
|
float qw = mRenderer.mQuat1.getW();
|
88
|
|
89
|
float rx = mRenderer.mQuat2.getX();
|
90
|
float ry = mRenderer.mQuat2.getY();
|
91
|
float rz = mRenderer.mQuat2.getZ();
|
92
|
float rw = mRenderer.mQuat2.getW();
|
93
|
|
94
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
95
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
96
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
97
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
98
|
|
99
|
mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
|
100
|
mRenderer.mQuat2.set(tx, ty, tz, tw);
|
101
|
|
102
|
break;
|
103
|
}
|
104
|
|
105
|
return true;
|
106
|
}
|
107
|
|
108
|
}
|
109
|
|