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