Revision 833685d0
Added by Leszek Koltunski over 8 years ago
src/main/java/org/distorted/examples/cubes/CubesRenderer.java | ||
---|---|---|
70 | 70 |
mQuat1 = new Static4D(0,0,0,1); // unity |
71 | 71 |
mQuat2 = new Static4D(0,0,0,1); // quaternions |
72 | 72 |
|
73 |
mQuatInt1 = new DynamicQuat(); |
|
74 |
mQuatInt2 = new DynamicQuat(); |
|
75 |
mQuatInt1.setDuration(0); |
|
76 |
mQuatInt2.setDuration(0); |
|
77 |
mQuatInt1.setCount(0.5f); |
|
78 |
mQuatInt2.setCount(0.5f); |
|
79 |
|
|
73 |
mQuatInt1 = new DynamicQuat(0,0.5f); |
|
74 |
mQuatInt2 = new DynamicQuat(0,0.5f); |
|
75 |
|
|
80 | 76 |
mQuatInt1.add(mQuat1); |
81 | 77 |
mQuatInt2.add(mQuat2); |
82 | 78 |
} |
src/main/java/org/distorted/examples/cubes/CubesSurfaceView.java | ||
---|---|---|
32 | 32 |
|
33 | 33 |
class CubesSurfaceView extends GLSurfaceView |
34 | 34 |
{ |
35 |
int mX, mY; |
|
36 |
CubesRenderer mRenderer; |
|
35 |
private int mX, mY;
|
|
36 |
private CubesRenderer mRenderer;
|
|
37 | 37 |
|
38 | 38 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
39 | 39 |
|
... | ... | |
109 | 109 |
float rz = mRenderer.mQuat2.getZ(); |
110 | 110 |
float rw = mRenderer.mQuat2.getW(); |
111 | 111 |
|
112 |
// This is quaternion multiplication. (tx.ty.tz.tw) |
|
113 |
// is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
112 | 114 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
113 | 115 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
114 | 116 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
115 | 117 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
116 |
|
|
118 |
|
|
119 |
// The point of this is so that there are always |
|
120 |
// exactly 2 quaternions: Quat1 representing the rotation |
|
121 |
// accumulating only since the last screen touch, and Quat2 |
|
122 |
// which remembers the combined effect of all previous |
|
123 |
// swipes. |
|
124 |
// We cannot be accumulating an ever-growing list of quaternions |
|
125 |
// and add a new one every time user swipes the screen - there |
|
126 |
// is a limited number of slots in the EffectQueueMatrix! |
|
117 | 127 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
118 | 128 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
119 | 129 |
|
src/main/java/org/distorted/examples/vertex3d/Vertex3DRenderer.java | ||
---|---|---|
32 | 32 |
import org.distorted.library.type.Dynamic1D; |
33 | 33 |
import org.distorted.library.type.Dynamic3D; |
34 | 34 |
import org.distorted.library.type.Dynamic4D; |
35 |
import org.distorted.library.type.DynamicQuat; |
|
35 | 36 |
import org.distorted.library.type.Static1D; |
36 | 37 |
import org.distorted.library.type.Static2D; |
37 | 38 |
import org.distorted.library.type.Static3D; |
... | ... | |
52 | 53 |
private GLSurfaceView mView; |
53 | 54 |
private static DistortedCubes mCube; |
54 | 55 |
private int mCols, mRows; |
56 |
private DynamicQuat mQuatInt1, mQuatInt2; |
|
55 | 57 |
|
56 | 58 |
private static EffectNames[] order; |
57 | 59 |
|
... | ... | |
62 | 64 |
private static Static3D mDeformPoint, mDistortPoint; |
63 | 65 |
private static Static1D mSinkPoint, mSwirlPoint; |
64 | 66 |
|
67 |
Static4D mQuat1, mQuat2; |
|
68 |
int mScreenMin; |
|
69 |
|
|
65 | 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
66 | 71 |
|
67 | 72 |
public static void setDeform(float x, float y, float z) |
... | ... | |
144 | 149 |
mDistortInter.add(mDistortPoint); |
145 | 150 |
mSwirlInter.add(mSwirlPoint); |
146 | 151 |
mSinkInter.add(mSinkPoint); |
152 |
|
|
153 |
mQuat1 = new Static4D(0,0,0,1); // unity |
|
154 |
mQuat2 = new Static4D(0,0,0,1); // quaternions |
|
155 |
|
|
156 |
mQuatInt1 = new DynamicQuat(0,0.5f); |
|
157 |
mQuatInt2 = new DynamicQuat(0,0.5f); |
|
158 |
|
|
159 |
mQuatInt1.add(mQuat1); |
|
160 |
mQuatInt2.add(mQuat2); |
|
147 | 161 |
} |
148 | 162 |
|
149 | 163 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
160 | 174 |
|
161 | 175 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
162 | 176 |
{ |
177 |
mScreenMin = width<height ? width:height; |
|
178 |
|
|
163 | 179 |
mCube.abortEffects(EffectTypes.MATRIX); |
164 | 180 |
float factor; |
165 | 181 |
|
... | ... | |
175 | 191 |
mCube.scale(factor); |
176 | 192 |
mCube.move( new Static3D( (width-factor*mCols*SIZE)/2 , (height-factor*mRows*SIZE)/2 , 0) ); |
177 | 193 |
|
194 |
Static3D center = new Static3D(mCols*SIZE/2,mRows*SIZE/2, 0); |
|
195 |
|
|
196 |
mCube.quaternion(mQuatInt1, center); |
|
197 |
mCube.quaternion(mQuatInt2, center); |
|
198 |
|
|
178 | 199 |
setVertexEffects(); |
179 | 200 |
|
180 | 201 |
Distorted.onSurfaceChanged(width, height); |
src/main/java/org/distorted/examples/vertex3d/Vertex3DSurfaceView.java | ||
---|---|---|
23 | 23 |
import android.opengl.GLSurfaceView; |
24 | 24 |
import android.os.Build; |
25 | 25 |
import android.util.AttributeSet; |
26 |
import android.view.MotionEvent; |
|
26 | 27 |
|
27 | 28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
28 | 29 |
|
29 | 30 |
class Vertex3DSurfaceView extends GLSurfaceView |
30 |
{ |
|
31 |
{ |
|
32 |
private int mX, mY; |
|
33 |
private Vertex3DRenderer mRenderer; |
|
34 |
|
|
31 | 35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
32 | 36 |
|
33 |
public Vertex3DSurfaceView(Context c, AttributeSet attrs)
|
|
34 |
{
|
|
35 |
super(c, attrs);
|
|
37 |
public Vertex3DSurfaceView(Context c, AttributeSet attrs) |
|
38 |
{ |
|
39 |
super(c, attrs); |
|
36 | 40 |
|
37 |
if(!isInEditMode()) |
|
38 |
{ |
|
39 |
setEGLContextClientVersion(2); |
|
40 |
|
|
41 |
if( Build.FINGERPRINT.startsWith("generic") ) |
|
42 |
{ |
|
43 |
setEGLConfigChooser(8, 8, 8, 8, 16, 0); |
|
44 |
} |
|
41 |
if(!isInEditMode()) |
|
42 |
{ |
|
43 |
setEGLContextClientVersion(2); |
|
45 | 44 |
|
46 |
setRenderer(new Vertex3DRenderer(this)); |
|
45 |
if( Build.FINGERPRINT.startsWith("generic") ) |
|
46 |
{ |
|
47 |
setEGLConfigChooser(8, 8, 8, 8, 16, 0); |
|
47 | 48 |
} |
49 |
|
|
50 |
mRenderer = new Vertex3DRenderer(this); |
|
51 |
|
|
52 |
setRenderer(mRenderer); |
|
48 | 53 |
} |
54 |
} |
|
55 |
|
|
56 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
57 |
|
|
58 |
@Override |
|
59 |
public boolean onTouchEvent(MotionEvent event) |
|
60 |
{ |
|
61 |
int action = event.getAction(); |
|
62 |
int x = (int)event.getX(); |
|
63 |
int y = (int)event.getY(); |
|
64 |
|
|
65 |
switch(action) |
|
66 |
{ |
|
67 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
68 |
mY = y; |
|
69 |
break; |
|
70 |
|
|
71 |
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 ) |
|
72 |
{ |
|
73 |
float px = mY-y; |
|
74 |
float py = mX-x; |
|
75 |
float pz = 0; |
|
76 |
float plen = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
77 |
|
|
78 |
if( plen>0 ) |
|
79 |
{ |
|
80 |
px /= plen; |
|
81 |
py /= plen; |
|
82 |
pz /= plen; |
|
83 |
|
|
84 |
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin); |
|
85 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
86 |
|
|
87 |
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA); |
|
88 |
} |
|
89 |
} |
|
90 |
break; |
|
91 |
|
|
92 |
case MotionEvent.ACTION_UP : mX = -1; |
|
93 |
mY = -1; |
|
94 |
|
|
95 |
float qx = mRenderer.mQuat1.getX(); |
|
96 |
float qy = mRenderer.mQuat1.getY(); |
|
97 |
float qz = mRenderer.mQuat1.getZ(); |
|
98 |
float qw = mRenderer.mQuat1.getW(); |
|
99 |
|
|
100 |
float rx = mRenderer.mQuat2.getX(); |
|
101 |
float ry = mRenderer.mQuat2.getY(); |
|
102 |
float rz = mRenderer.mQuat2.getZ(); |
|
103 |
float rw = mRenderer.mQuat2.getW(); |
|
104 |
|
|
105 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
106 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
107 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
108 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
109 |
|
|
110 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
|
111 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
|
112 |
|
|
113 |
break; |
|
114 |
} |
|
115 |
|
|
116 |
return true; |
|
117 |
} |
|
49 | 118 |
} |
50 | 119 |
|
Also available in: Unified diff
Progress with Vertex3D