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.polymesh;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Paint;
|
27
|
import android.graphics.Paint.Style;
|
28
|
import android.opengl.GLSurfaceView;
|
29
|
import android.util.AttributeSet;
|
30
|
import android.view.MotionEvent;
|
31
|
|
32
|
import org.distorted.library.effect.FragmentEffectAlpha;
|
33
|
import org.distorted.library.effect.FragmentEffectChroma;
|
34
|
import org.distorted.library.effect.VertexEffectDeform;
|
35
|
import org.distorted.library.effect.VertexEffectSink;
|
36
|
import org.distorted.library.effect.VertexEffectSwirl;
|
37
|
import org.distorted.library.type.Dynamic3D;
|
38
|
import org.distorted.library.type.Static1D;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
import org.distorted.library.type.Static4D;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public class PolymeshSurfaceView extends GLSurfaceView
|
45
|
{
|
46
|
private static final int LOOP_TIME = 5000;
|
47
|
private static final int NUM_POINTS= 100;
|
48
|
|
49
|
public static final int EFFECT_POINTS=0;
|
50
|
public static final int EFFECT_BUBBLE=1;
|
51
|
public static final int EFFECT_SINK =2;
|
52
|
public static final int EFFECT_TRANS =3;
|
53
|
public static final int EFFECT_CHROMA=4;
|
54
|
public static final int EFFECT_SWIRL =5;
|
55
|
|
56
|
private static final Object lock = new Object();
|
57
|
|
58
|
private Dynamic3D mCenter;
|
59
|
private Paint mPaint;
|
60
|
private int moving;
|
61
|
private int mScrWidth, mScrHeight;
|
62
|
private long mTime = 0;
|
63
|
|
64
|
private int mCurrEffect;
|
65
|
private int mSize1, mSize2;
|
66
|
private Static3D mDeform;
|
67
|
private Static1D mRadius;
|
68
|
|
69
|
private PolymeshRenderer mRenderer;
|
70
|
|
71
|
private VertexEffectDeform mEffectDeform;
|
72
|
private VertexEffectSink mEffectSink;
|
73
|
private VertexEffectSwirl mEffectSwirl;
|
74
|
private FragmentEffectChroma mEffectChroma;
|
75
|
private FragmentEffectAlpha mEffectAlpha;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
public PolymeshSurfaceView(Context context, AttributeSet attrs)
|
80
|
{
|
81
|
super(context, attrs);
|
82
|
|
83
|
mCurrEffect=EFFECT_POINTS;
|
84
|
mPaint = new Paint();
|
85
|
mPaint.setStyle(Style.FILL);
|
86
|
moving = -1;
|
87
|
|
88
|
mCenter = new Dynamic3D(LOOP_TIME,0.0f);
|
89
|
mDeform = new Static3D(0,0,0.15f);
|
90
|
mRadius = new Static1D(0);
|
91
|
|
92
|
if(!isInEditMode())
|
93
|
{
|
94
|
mRenderer = new PolymeshRenderer(this);
|
95
|
|
96
|
setFocusable(true);
|
97
|
setFocusableInTouchMode(true);
|
98
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
99
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
100
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
101
|
setRenderer(mRenderer);
|
102
|
|
103
|
Static3D regionFrag = new Static3D(0.15f,0.15f,0.15f);
|
104
|
Static4D regionVert = new Static4D(0,0,0,0.15f);
|
105
|
|
106
|
mEffectDeform = new VertexEffectDeform ( mDeform , mRadius, mCenter, regionVert);
|
107
|
mEffectSink = new VertexEffectSink (new Static1D(10) , mCenter, regionVert);
|
108
|
mEffectSwirl = new VertexEffectSwirl (new Static1D(30) , mCenter, regionVert);
|
109
|
mEffectAlpha = new FragmentEffectAlpha (new Static1D(0.5f), mCenter, regionFrag, true);
|
110
|
mEffectChroma = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mCenter, regionFrag, true);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115
|
|
116
|
public void onSurfaceChanged(int width,int height)
|
117
|
{
|
118
|
int max = Math.max(width, height);
|
119
|
|
120
|
mScrWidth = width;
|
121
|
mScrHeight = height;
|
122
|
|
123
|
mRadius.set(max/2);
|
124
|
|
125
|
mSize1 = max/200;
|
126
|
mSize2 = max/80;
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
public int getCurrentEffect()
|
132
|
{
|
133
|
return mCurrEffect;
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
public void Bubble()
|
139
|
{
|
140
|
if( mCurrEffect==EFFECT_BUBBLE ) return;
|
141
|
|
142
|
synchronized(lock)
|
143
|
{
|
144
|
mRenderer.apply(mEffectDeform);
|
145
|
mCurrEffect = EFFECT_BUBBLE;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
public void Sink()
|
152
|
{
|
153
|
if( mCurrEffect==EFFECT_SINK ) return;
|
154
|
|
155
|
synchronized(lock)
|
156
|
{
|
157
|
mRenderer.apply(mEffectSink);
|
158
|
mCurrEffect = EFFECT_SINK;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public void Chroma()
|
165
|
{
|
166
|
if( mCurrEffect==EFFECT_CHROMA ) return;
|
167
|
|
168
|
synchronized(lock)
|
169
|
{
|
170
|
mRenderer.apply(mEffectChroma);
|
171
|
mCurrEffect = EFFECT_CHROMA;
|
172
|
}
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
public void Transparency()
|
178
|
{
|
179
|
if( mCurrEffect==EFFECT_TRANS ) return;
|
180
|
|
181
|
synchronized(lock)
|
182
|
{
|
183
|
mRenderer.apply(mEffectAlpha);
|
184
|
mCurrEffect = EFFECT_TRANS;
|
185
|
}
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
public void Swirl()
|
191
|
{
|
192
|
if( mCurrEffect==EFFECT_SWIRL ) return;
|
193
|
|
194
|
synchronized(lock)
|
195
|
{
|
196
|
mRenderer.apply(mEffectSwirl);
|
197
|
mCurrEffect = EFFECT_SWIRL;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
202
|
|
203
|
public void Abort()
|
204
|
{
|
205
|
synchronized(lock)
|
206
|
{
|
207
|
mRenderer.apply(null);
|
208
|
mCenter.removeAll();
|
209
|
mCenter.resetToBeginning();
|
210
|
mCurrEffect = EFFECT_POINTS;
|
211
|
mRenderer.setRefresh();
|
212
|
}
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
|
217
|
public void drawCurve(Canvas c, long time)
|
218
|
{
|
219
|
synchronized(lock)
|
220
|
{
|
221
|
int len = mCenter.getNumPoints();
|
222
|
|
223
|
float[] drawCoord = new float[3];
|
224
|
Static3D cu;
|
225
|
|
226
|
if( len>=2 )
|
227
|
{
|
228
|
final float step = (float)LOOP_TIME/(NUM_POINTS+1);
|
229
|
|
230
|
for(int i=0; i<NUM_POINTS; i++)
|
231
|
{
|
232
|
mPaint.setColor( 0xffffffff );
|
233
|
mCenter.get( drawCoord, 0, (long)(i*step), 0 );
|
234
|
c.drawCircle( (drawCoord[0]+0.5f)*mScrWidth, 0.5f*mScrHeight-drawCoord[1]*mScrWidth, mSize1, mPaint );
|
235
|
}
|
236
|
}
|
237
|
|
238
|
mPaint.setColor(0xffff0000);
|
239
|
|
240
|
for(int curr=0; curr<len; curr++)
|
241
|
{
|
242
|
cu = mCenter.getPoint(curr);
|
243
|
c.drawCircle( (cu.get0()+0.5f)*mScrWidth, 0.5f*mScrHeight-cu.get1()*mScrWidth, mSize2, mPaint);
|
244
|
}
|
245
|
|
246
|
if( time-mTime > LOOP_TIME ) mTime = time;
|
247
|
}
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
|
252
|
@Override
|
253
|
public boolean onTouchEvent(MotionEvent event)
|
254
|
{
|
255
|
if( mCurrEffect!=EFFECT_POINTS ) return true;
|
256
|
|
257
|
float xDown = event.getX()/mScrWidth - 0.5f;
|
258
|
float yDown = (0.5f*mScrHeight - event.getY())/mScrWidth;
|
259
|
|
260
|
switch(event.getAction())
|
261
|
{
|
262
|
case MotionEvent.ACTION_DOWN: float gx, gy;
|
263
|
Static3D dv;
|
264
|
int len = mCenter.getNumPoints();
|
265
|
|
266
|
for(int g=0; g<len; g++)
|
267
|
{
|
268
|
dv = mCenter.getPoint(g);
|
269
|
gx = dv.get0();
|
270
|
gy = dv.get1();
|
271
|
|
272
|
float Z = mDeform.get2()/7;
|
273
|
|
274
|
if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
|
275
|
{
|
276
|
moving = g;
|
277
|
break;
|
278
|
}
|
279
|
}
|
280
|
if( moving<0 )
|
281
|
{
|
282
|
synchronized(lock)
|
283
|
{
|
284
|
mCenter.add(new Static3D(xDown,yDown,0));
|
285
|
}
|
286
|
}
|
287
|
mRenderer.setRefresh();
|
288
|
break;
|
289
|
case MotionEvent.ACTION_MOVE: if( moving>=0 )
|
290
|
{
|
291
|
mCenter.setPoint(moving, xDown, yDown, 0);
|
292
|
}
|
293
|
mRenderer.setRefresh();
|
294
|
break;
|
295
|
case MotionEvent.ACTION_UP : moving = -1;
|
296
|
break;
|
297
|
}
|
298
|
return true;
|
299
|
}
|
300
|
}
|