Revision 4b7c432e
Added by Leszek Koltunski almost 4 years ago
src/main/java/org/distorted/examples/predeform/PredeformSurfaceView.java | ||
---|---|---|
24 | 24 |
import android.content.pm.ConfigurationInfo; |
25 | 25 |
import android.opengl.GLSurfaceView; |
26 | 26 |
import android.util.AttributeSet; |
27 |
import android.view.MotionEvent; |
|
28 |
import android.widget.Toast; |
|
29 |
|
|
30 |
import org.distorted.examples.R; |
|
27 | 31 |
|
28 | 32 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 | 33 |
|
30 | 34 |
class PredeformSurfaceView extends GLSurfaceView |
31 | 35 |
{ |
36 |
private final static int DIRECTION_SENSITIVITY= 12; |
|
37 |
private int mX, mY; |
|
32 | 38 |
private PredeformRenderer mRenderer; |
33 | 39 |
|
34 | 40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
37 | 43 |
{ |
38 | 44 |
super(context,attrs); |
39 | 45 |
|
46 |
mX = -1; |
|
47 |
mY = -1; |
|
48 |
|
|
40 | 49 |
if(!isInEditMode()) |
41 | 50 |
{ |
42 | 51 |
mRenderer = new PredeformRenderer(this); |
... | ... | |
44 | 53 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
45 | 54 |
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 ); |
46 | 55 |
setRenderer(mRenderer); |
56 |
Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show(); |
|
47 | 57 |
} |
48 | 58 |
} |
49 | 59 |
|
... | ... | |
53 | 63 |
{ |
54 | 64 |
return mRenderer; |
55 | 65 |
} |
66 |
|
|
67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
68 |
|
|
69 |
private void resetQuats() |
|
70 |
{ |
|
71 |
float qx = mRenderer.mQuat1.get0(); |
|
72 |
float qy = mRenderer.mQuat1.get1(); |
|
73 |
float qz = mRenderer.mQuat1.get2(); |
|
74 |
float qw = mRenderer.mQuat1.get3(); |
|
75 |
|
|
76 |
float rx = mRenderer.mQuat2.get0(); |
|
77 |
float ry = mRenderer.mQuat2.get1(); |
|
78 |
float rz = mRenderer.mQuat2.get2(); |
|
79 |
float rw = mRenderer.mQuat2.get3(); |
|
80 |
|
|
81 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
82 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
83 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
84 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
85 |
|
|
86 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
|
87 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
|
88 |
} |
|
89 |
|
|
90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
91 |
|
|
92 |
@Override public boolean onTouchEvent(MotionEvent event) |
|
93 |
{ |
|
94 |
int action = event.getAction(); |
|
95 |
int x = (int)event.getX(); |
|
96 |
int y = (int)event.getY(); |
|
97 |
|
|
98 |
switch(action) |
|
99 |
{ |
|
100 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
101 |
mY = y; |
|
102 |
break; |
|
103 |
|
|
104 |
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 ) |
|
105 |
{ |
|
106 |
float px = mY-y; |
|
107 |
float py = mX-x; |
|
108 |
float pz = 0; |
|
109 |
float plen = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
110 |
|
|
111 |
if( plen>0 ) |
|
112 |
{ |
|
113 |
px /= plen; |
|
114 |
py /= plen; |
|
115 |
pz /= plen; |
|
116 |
|
|
117 |
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin); |
|
118 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
119 |
|
|
120 |
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA); |
|
121 |
} |
|
122 |
} |
|
123 |
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) ) |
|
124 |
{ |
|
125 |
mX = x; |
|
126 |
mY = y; |
|
127 |
resetQuats(); |
|
128 |
} |
|
129 |
break; |
|
130 |
|
|
131 |
case MotionEvent.ACTION_UP : mX = -1; |
|
132 |
mY = -1; |
|
133 |
resetQuats(); |
|
134 |
break; |
|
135 |
} |
|
136 |
|
|
137 |
return true; |
|
138 |
} |
|
139 |
|
|
56 | 140 |
} |
57 | 141 |
|
Also available in: Unified diff
Progress (?) with Predeform: give up trying to render the Mesh in Activity1.