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.deform;
|
21
|
|
22
|
import javax.microedition.khronos.egl.EGLConfig;
|
23
|
import javax.microedition.khronos.opengles.GL10;
|
24
|
|
25
|
import org.distorted.library.Distorted;
|
26
|
import org.distorted.library.DistortedBitmap;
|
27
|
import org.distorted.library.EffectTypes;
|
28
|
import org.distorted.library.type.Interpolator2D;
|
29
|
import org.distorted.library.type.Float3D;
|
30
|
import org.distorted.library.type.Float4D;
|
31
|
|
32
|
import android.graphics.Bitmap;
|
33
|
import android.graphics.Canvas;
|
34
|
import android.graphics.Paint;
|
35
|
import android.graphics.Paint.Style;
|
36
|
import android.opengl.GLES20;
|
37
|
import android.opengl.GLSurfaceView;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
class DeformRenderer implements GLSurfaceView.Renderer
|
42
|
{
|
43
|
public static final int MODE_DISTORT= 0;
|
44
|
public static final int MODE_DEFORM = 1;
|
45
|
public static final int MODE_SHEAR = 2;
|
46
|
|
47
|
private static final int NUM_VECTORS = 8;
|
48
|
private static final int NUMLINES = 10;
|
49
|
private static final int NUMFRAMES =10;
|
50
|
|
51
|
private GLSurfaceView mView;
|
52
|
private DistortedBitmap fps;
|
53
|
private DistortedBitmap stretch;
|
54
|
private Float3D touchPoint;
|
55
|
private static Interpolator2D di;
|
56
|
private static Float3D[] v;
|
57
|
private static Float4D dr;
|
58
|
private Canvas fpsCanvas;
|
59
|
private Bitmap fpsBitmap, stretchBitmap;
|
60
|
private int scrHeight, scrWidth;
|
61
|
private Paint mPaint;
|
62
|
private int fpsH, fpsW;
|
63
|
private String fpsString = "";
|
64
|
private long lastTime=0;
|
65
|
private long[] durations;
|
66
|
private int currDuration;
|
67
|
private long shearID=0;
|
68
|
|
69
|
private static int mMode = MODE_DISTORT;
|
70
|
private static boolean bitmapCreated = false;
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
|
74
|
public DeformRenderer(GLSurfaceView view)
|
75
|
{
|
76
|
mView = view;
|
77
|
|
78
|
dr = new Float4D(0,0,0,0);
|
79
|
|
80
|
di = new Interpolator2D();
|
81
|
di.setMode(Interpolator2D.MODE_PATH);
|
82
|
di.setCount(0.5f);
|
83
|
di.setDuration(NUM_VECTORS*500);
|
84
|
|
85
|
v = new Float3D[NUM_VECTORS];
|
86
|
|
87
|
for(int i=0; i<NUM_VECTORS; i++)
|
88
|
{
|
89
|
v[i] = new Float3D(0,0,0);
|
90
|
di.add(v[i]);
|
91
|
}
|
92
|
|
93
|
durations = new long[NUMFRAMES+1];
|
94
|
currDuration = 0;
|
95
|
|
96
|
for(int i=0; i<NUMFRAMES+1; i++)
|
97
|
{
|
98
|
durations[i]=0;
|
99
|
}
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
public static void setMode(int mode)
|
105
|
{
|
106
|
mMode = mode;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public static void setRegionRadius(int r)
|
112
|
{
|
113
|
dr.set(0,0,r);
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public static void onPause()
|
119
|
{
|
120
|
bitmapCreated = false;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void onDrawFrame(GL10 glUnused)
|
126
|
{
|
127
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
128
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
129
|
|
130
|
long time = System.currentTimeMillis();
|
131
|
|
132
|
stretch.draw(time);
|
133
|
|
134
|
mPaint.setColor(0xffffffff);
|
135
|
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
|
136
|
mPaint.setColor(0xff000000);
|
137
|
fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
|
138
|
|
139
|
fps.setBitmap(fpsBitmap);
|
140
|
fps.draw(time);
|
141
|
|
142
|
computeFPS(time);
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
|
147
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
148
|
{
|
149
|
scrHeight = height;
|
150
|
scrWidth = width;
|
151
|
|
152
|
Distorted.onSurfaceChanged(width, height);
|
153
|
|
154
|
if( bitmapCreated==false )
|
155
|
{
|
156
|
createBitmap(scrWidth/2,scrHeight/2);
|
157
|
stretch.move(scrWidth/4,scrHeight/4,0);
|
158
|
fps.move(5,5,0);
|
159
|
bitmapCreated=true;
|
160
|
}
|
161
|
else
|
162
|
{
|
163
|
stretch.abortEffects(EffectTypes.VERTEX);
|
164
|
stretch.abortEffects(EffectTypes.FRAGMENT);
|
165
|
stretch.abortEffect(shearID);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
172
|
{
|
173
|
try
|
174
|
{
|
175
|
Distorted.onSurfaceCreated(mView.getContext());
|
176
|
}
|
177
|
catch(Exception ex)
|
178
|
{
|
179
|
android.util.Log.e("DeformRenderer", ex.toString() );
|
180
|
}
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
private void createBitmap(int w, int h)
|
186
|
{
|
187
|
Canvas stretchCanvas;
|
188
|
|
189
|
mPaint = new Paint();
|
190
|
stretch = new DistortedBitmap(w,h, 50);
|
191
|
stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
|
192
|
stretchCanvas = new Canvas(stretchBitmap);
|
193
|
|
194
|
fpsW = scrWidth/5;
|
195
|
fpsH = fpsW/2;
|
196
|
|
197
|
mPaint.setAntiAlias(true);
|
198
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
199
|
mPaint.setTextSize(2*fpsH/3);
|
200
|
mPaint.setColor(0xff008800);
|
201
|
mPaint.setStyle(Style.FILL);
|
202
|
stretchCanvas.drawRect(0, 0, w, h, mPaint);
|
203
|
mPaint.setColor(0xffffffff);
|
204
|
|
205
|
for(int i=0; i<=NUMLINES ; i++ )
|
206
|
{
|
207
|
stretchCanvas.drawRect(w*i/NUMLINES - 1, 0, w*i/NUMLINES + 1, h , mPaint);
|
208
|
stretchCanvas.drawRect( 0, h *i/NUMLINES -1, w , h*i/NUMLINES + 1, mPaint);
|
209
|
}
|
210
|
|
211
|
touchPoint= new Float3D(0,0,0);
|
212
|
|
213
|
fps = new DistortedBitmap( fpsW, fpsH, 2);
|
214
|
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
|
215
|
fpsCanvas = new Canvas(fpsBitmap);
|
216
|
|
217
|
stretch.setBitmap(stretchBitmap);
|
218
|
fps.setBitmap(fpsBitmap);
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
public void down(int x, int y)
|
224
|
{
|
225
|
stretch.abortEffects(EffectTypes.VERTEX);
|
226
|
stretch.abortEffects(EffectTypes.FRAGMENT);
|
227
|
stretch.abortEffect(shearID);
|
228
|
|
229
|
int xt = x-scrWidth/4;
|
230
|
int yt = y-scrHeight/4;
|
231
|
|
232
|
if( xt<0 ) xt=0;
|
233
|
if( xt>scrWidth/2 ) xt=scrWidth/2;
|
234
|
if( yt<0 ) yt=0;
|
235
|
if( yt>scrHeight/2 ) yt=scrHeight/2;
|
236
|
|
237
|
touchPoint.set(xt,yt,0);
|
238
|
|
239
|
v[0].set(0,0,0);
|
240
|
|
241
|
switch(mMode)
|
242
|
{
|
243
|
case MODE_DISTORT: stretch.distort(v[0], dr, touchPoint, 0, 0.5f);
|
244
|
break;
|
245
|
case MODE_DEFORM : stretch.deform( v[0], touchPoint);
|
246
|
break;
|
247
|
case MODE_SHEAR : shearID = stretch.shear(touchPoint,v[0]);
|
248
|
break;
|
249
|
}
|
250
|
}
|
251
|
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
public void move(int x, int y)
|
255
|
{
|
256
|
float fx = x;
|
257
|
float fy = y;
|
258
|
|
259
|
if( mMode==MODE_SHEAR )
|
260
|
{
|
261
|
fx /= (scrWidth/2);
|
262
|
fy /= (scrHeight/2);
|
263
|
}
|
264
|
|
265
|
v[0].set(fx,fy);
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
public void up()
|
271
|
{
|
272
|
stretch.abortEffects(EffectTypes.VERTEX);
|
273
|
stretch.abortEffects(EffectTypes.FRAGMENT);
|
274
|
stretch.abortEffect(shearID);
|
275
|
|
276
|
float damp = -0.65f;
|
277
|
|
278
|
for(int i=1; i<NUM_VECTORS-1; i++)
|
279
|
{
|
280
|
v[i].set( v[i-1].getX()*damp, v[i-1].getY()*damp );
|
281
|
}
|
282
|
v[NUM_VECTORS-1].set(0,0);
|
283
|
|
284
|
switch(mMode)
|
285
|
{
|
286
|
case MODE_DISTORT: stretch.distort(di, dr, touchPoint);
|
287
|
break;
|
288
|
case MODE_DEFORM : stretch.deform( di, touchPoint);
|
289
|
break;
|
290
|
case MODE_SHEAR : shearID = stretch.shear(touchPoint,di);
|
291
|
break;
|
292
|
}
|
293
|
}
|
294
|
|
295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
296
|
|
297
|
private void computeFPS(long currentTime)
|
298
|
{
|
299
|
if( lastTime!=0 )
|
300
|
{
|
301
|
currDuration++;
|
302
|
if( currDuration>=NUMFRAMES ) currDuration = 0;
|
303
|
durations[NUMFRAMES] += ((currentTime-lastTime)-durations[currDuration]);
|
304
|
durations[currDuration] = currentTime-lastTime;
|
305
|
|
306
|
fpsString = "" + ((int)(10000.0f*NUMFRAMES/durations[NUMFRAMES]))/10.0f;
|
307
|
}
|
308
|
|
309
|
lastTime = currentTime;
|
310
|
}
|
311
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313
|
|
314
|
}
|