Revision 815687bb
Added by Leszek Koltunski about 8 years ago
src/main/java/org/distorted/examples/wind/WindEffectsManager.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2016 Leszek Koltunski // |
|
3 |
// // |
|
4 |
// This file is part of Distorted. // |
|
5 |
// // |
|
6 |
// Distorted is free software: you can redistribute it and/or modify // |
|
7 |
// 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 |
// Distorted is distributed in the hope that it will be useful, // |
|
12 |
// 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 |
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. // |
|
18 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
|
20 |
package org.distorted.examples.wind; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
|
|
24 |
import org.distorted.library.DistortedObject; |
|
25 |
|
|
26 |
class WindEffectsManager |
|
27 |
{ |
|
28 |
|
|
29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
30 |
|
|
31 |
void apply(DistortedObject obj, int wind) |
|
32 |
{ |
|
33 |
|
|
34 |
} |
|
35 |
} |
src/main/java/org/distorted/examples/wind/WindRenderer.java | ||
---|---|---|
29 | 29 |
import org.distorted.library.DistortedCubes; |
30 | 30 |
import org.distorted.library.DistortedObject; |
31 | 31 |
import org.distorted.library.EffectTypes; |
32 |
import org.distorted.library.type.DynamicQuat;
|
|
32 |
import org.distorted.library.type.Static1D;
|
|
33 | 33 |
import org.distorted.library.type.Static3D; |
34 |
import org.distorted.library.type.Static4D; |
|
35 | 34 |
|
36 | 35 |
import java.io.IOException; |
37 | 36 |
import java.io.InputStream; |
... | ... | |
45 | 44 |
{ |
46 | 45 |
private GLSurfaceView mView; |
47 | 46 |
private DistortedObject mObject; |
48 |
private DynamicQuat mQuatInt1, mQuatInt2; |
|
49 |
private int mWind; |
|
47 |
private WindEffectsManager mEffects; |
|
50 | 48 |
private int mObjWidth, mObjHeight; |
51 | 49 |
|
52 |
Static4D mQuat1, mQuat2; |
|
53 |
int mScreenMin; |
|
54 |
|
|
55 | 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
56 | 51 |
|
57 | 52 |
WindRenderer(GLSurfaceView view) |
... | ... | |
59 | 54 |
mView = view; |
60 | 55 |
|
61 | 56 |
mObject = new DistortedCubes(50,30,10,false); |
57 |
mEffects = new WindEffectsManager(); |
|
62 | 58 |
|
63 | 59 |
mObjWidth = mObject.getWidth(); |
64 | 60 |
mObjHeight= mObject.getHeight(); |
65 |
|
|
66 |
mQuat1 = new Static4D( 0, 0, 0, 1); // unity quaternion |
|
67 |
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); // something semi-random that looks good |
|
68 |
|
|
69 |
mQuatInt1 = new DynamicQuat(0,0.5f); |
|
70 |
mQuatInt2 = new DynamicQuat(0,0.5f); |
|
71 |
|
|
72 |
mQuatInt1.add(mQuat1); |
|
73 |
mQuatInt2.add(mQuat2); |
|
74 | 61 |
} |
75 | 62 |
|
76 | 63 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
77 | 64 |
|
78 | 65 |
void setWind(int wind) |
79 | 66 |
{ |
80 |
mWind = wind;
|
|
67 |
mEffects.apply(mObject,wind);
|
|
81 | 68 |
} |
82 | 69 |
|
83 | 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
94 | 81 |
|
95 | 82 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
96 | 83 |
{ |
97 |
mScreenMin = width<height ? width:height; |
|
98 |
|
|
99 | 84 |
mObject.abortEffects(EffectTypes.MATRIX); |
100 |
float factor; |
|
85 |
float factor = 0.8f*(width<height? width:height)/mObjWidth;
|
|
101 | 86 |
|
102 |
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object |
|
103 |
{ |
|
104 |
factor = (0.8f*height)/mObjHeight; |
|
105 |
} |
|
106 |
else |
|
107 |
{ |
|
108 |
factor = (0.8f*width)/mObjWidth; |
|
109 |
} |
|
110 |
|
|
111 |
mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) ); |
|
87 |
mObject.move( new Static3D( (width-factor*mObjWidth)/2 + width/8 , (height-factor*mObjHeight)/2 -height/4 , 0) ); |
|
112 | 88 |
mObject.scale(factor); |
113 |
Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0); |
|
114 | 89 |
|
115 |
mObject.quaternion(mQuatInt1, center); |
|
116 |
mObject.quaternion(mQuatInt2, center); |
|
90 |
Static1D angle = new Static1D(-45); |
|
91 |
Static3D axis = new Static3D(0,0,1); |
|
92 |
Static3D center= new Static3D(0,mObjHeight/2,0); |
|
93 |
|
|
94 |
mObject.rotate(angle, axis, center); |
|
117 | 95 |
|
118 | 96 |
Distorted.onSurfaceChanged(width, height); |
119 | 97 |
} |
src/main/java/org/distorted/examples/wind/WindSurfaceView.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
class WindSurfaceView extends GLSurfaceView |
31 | 31 |
{ |
32 |
private int mX, mY; |
|
33 | 32 |
private WindRenderer mRenderer; |
34 | 33 |
|
35 | 34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
38 | 37 |
{ |
39 | 38 |
super(c, attrs); |
40 | 39 |
|
41 |
mX = -1; |
|
42 |
mY = -1; |
|
43 |
|
|
44 | 40 |
if(!isInEditMode()) |
45 | 41 |
{ |
46 | 42 |
setEGLContextClientVersion(2); |
... | ... | |
60 | 56 |
{ |
61 | 57 |
return mRenderer; |
62 | 58 |
} |
63 |
|
|
64 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
65 |
|
|
66 |
@Override public boolean onTouchEvent(MotionEvent event) |
|
67 |
{ |
|
68 |
int action = event.getAction(); |
|
69 |
int x = (int)event.getX(); |
|
70 |
int y = (int)event.getY(); |
|
71 |
|
|
72 |
switch(action) |
|
73 |
{ |
|
74 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
75 |
mY = y; |
|
76 |
break; |
|
77 |
|
|
78 |
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 ) |
|
79 |
{ |
|
80 |
float px = mY-y; |
|
81 |
float py = mX-x; |
|
82 |
float pz = 0; |
|
83 |
float plen = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
84 |
|
|
85 |
if( plen>0 ) |
|
86 |
{ |
|
87 |
px /= plen; |
|
88 |
py /= plen; |
|
89 |
pz /= plen; |
|
90 |
|
|
91 |
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin); |
|
92 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
93 |
|
|
94 |
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA); |
|
95 |
} |
|
96 |
} |
|
97 |
break; |
|
98 |
|
|
99 |
case MotionEvent.ACTION_UP : mX = -1; |
|
100 |
mY = -1; |
|
101 |
|
|
102 |
float qx = mRenderer.mQuat1.getX(); |
|
103 |
float qy = mRenderer.mQuat1.getY(); |
|
104 |
float qz = mRenderer.mQuat1.getZ(); |
|
105 |
float qw = mRenderer.mQuat1.getW(); |
|
106 |
|
|
107 |
float rx = mRenderer.mQuat2.getX(); |
|
108 |
float ry = mRenderer.mQuat2.getY(); |
|
109 |
float rz = mRenderer.mQuat2.getZ(); |
|
110 |
float rw = mRenderer.mQuat2.getW(); |
|
111 |
|
|
112 |
// This is quaternion multiplication. (tx.ty.tz.tw) |
|
113 |
// is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
114 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
115 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
116 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
117 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
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! |
|
127 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
|
128 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
|
129 |
|
|
130 |
break; |
|
131 |
} |
|
132 |
|
|
133 |
return true; |
|
134 |
} |
|
135 |
|
|
136 | 59 |
} |
137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
138 | 60 |
|
Also available in: Unified diff
Progress with 'Wind'