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.effect.EffectName;
|
26
|
import org.distorted.library.effect.MatrixEffectMove;
|
27
|
import org.distorted.library.effect.MatrixEffectShear;
|
28
|
import org.distorted.library.effect.VertexEffectDeform;
|
29
|
import org.distorted.library.effect.VertexEffectDistort;
|
30
|
import org.distorted.library.main.DistortedLibrary;
|
31
|
import org.distorted.library.main.DistortedEffects;
|
32
|
import org.distorted.library.main.DistortedScreen;
|
33
|
import org.distorted.library.main.DistortedTexture;
|
34
|
import org.distorted.library.mesh.MeshBase;
|
35
|
import org.distorted.library.mesh.MeshRectangles;
|
36
|
|
37
|
import org.distorted.library.message.EffectListener;
|
38
|
import org.distorted.library.type.Dynamic3D;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
import org.distorted.library.type.Static4D;
|
41
|
|
42
|
import android.graphics.Bitmap;
|
43
|
import android.graphics.Canvas;
|
44
|
import android.graphics.Paint;
|
45
|
import android.graphics.Paint.Style;
|
46
|
import android.opengl.GLSurfaceView;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
class DeformRenderer implements GLSurfaceView.Renderer , EffectListener
|
51
|
{
|
52
|
private static final int NUM_VECTORS = 8;
|
53
|
private static final int NUM_LINES = 10;
|
54
|
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedTexture mTexture;
|
57
|
private DistortedEffects mEffects;
|
58
|
private MeshBase mMesh;
|
59
|
private DistortedScreen mScreen;
|
60
|
private Static3D mTouchPoint;
|
61
|
|
62
|
private Static3D[] vDistort;
|
63
|
private Static3D[] vDeform;
|
64
|
private Static3D[] vShear;
|
65
|
|
66
|
private Static4D mRegion;
|
67
|
private Static3D mMove;
|
68
|
private int scrHeight, scrWidth;
|
69
|
private float mRadius;
|
70
|
|
71
|
private EffectName mMode = EffectName.DISTORT;
|
72
|
private long mLastEffect = -1;
|
73
|
|
74
|
private MatrixEffectShear mMovingShear, mReleasedShear;
|
75
|
private VertexEffectDistort mMovingDistort, mReleasedDistort;
|
76
|
private VertexEffectDeform mMovingDeform, mReleasedDeform;
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
DeformRenderer(GLSurfaceView view)
|
81
|
{
|
82
|
mView = view;
|
83
|
|
84
|
mEffects = new DistortedEffects();
|
85
|
mRegion = new Static4D(0,0,0,0);
|
86
|
mMove = new Static3D(0,0,0);
|
87
|
mTouchPoint = new Static3D(0,0,0);
|
88
|
|
89
|
// DISTORT
|
90
|
Dynamic3D releasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
91
|
releasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
|
92
|
Dynamic3D movingDistortDynamic = new Dynamic3D(0,0.5f);
|
93
|
movingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
|
94
|
|
95
|
vDistort = new Static3D[NUM_VECTORS];
|
96
|
|
97
|
for(int i=0; i<NUM_VECTORS; i++)
|
98
|
{
|
99
|
vDistort[i] = new Static3D(0,0,0);
|
100
|
releasedDistortDynamic.add(vDistort[i]);
|
101
|
}
|
102
|
|
103
|
movingDistortDynamic.add(vDistort[0]);
|
104
|
|
105
|
// Deform
|
106
|
Dynamic3D releasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
107
|
releasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
|
108
|
Dynamic3D movingDeformDynamic = new Dynamic3D(0,0.5f);
|
109
|
movingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
|
110
|
|
111
|
vDeform = new Static3D[NUM_VECTORS];
|
112
|
|
113
|
for(int i=0; i<NUM_VECTORS; i++)
|
114
|
{
|
115
|
vDeform[i] = new Static3D(0,0,0);
|
116
|
releasedDeformDynamic.add(vDeform[i]);
|
117
|
}
|
118
|
|
119
|
movingDeformDynamic.add(vDeform[0]);
|
120
|
|
121
|
// Shear
|
122
|
Dynamic3D releasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
123
|
releasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
|
124
|
Dynamic3D movingShearDynamic = new Dynamic3D(0,0.5f);
|
125
|
movingShearDynamic.setMode(Dynamic3D.MODE_PATH);
|
126
|
|
127
|
vShear = new Static3D[NUM_VECTORS];
|
128
|
|
129
|
for(int i=0; i<NUM_VECTORS; i++)
|
130
|
{
|
131
|
vShear[i] = new Static3D(0,0,0);
|
132
|
releasedShearDynamic.add(vShear[i]);
|
133
|
}
|
134
|
|
135
|
movingShearDynamic.add(vShear[0]);
|
136
|
|
137
|
mScreen = new DistortedScreen();
|
138
|
mScreen.showFPS();
|
139
|
|
140
|
mMovingDistort = new VertexEffectDistort( movingDistortDynamic , mTouchPoint, mRegion);
|
141
|
mMovingDeform = new VertexEffectDeform ( movingDeformDynamic , mTouchPoint, mRegion);
|
142
|
mMovingShear = new MatrixEffectShear ( movingShearDynamic , mTouchPoint );
|
143
|
mReleasedDistort = new VertexEffectDistort( releasedDistortDynamic, mTouchPoint, mRegion);
|
144
|
mReleasedDeform = new VertexEffectDeform ( releasedDeformDynamic , mTouchPoint, mRegion);
|
145
|
mReleasedShear = new MatrixEffectShear ( releasedShearDynamic , mTouchPoint );
|
146
|
|
147
|
mEffects.apply(new MatrixEffectMove(mMove));
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
void setMode(EffectName mode)
|
153
|
{
|
154
|
mMode = mode;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
void setRegionRadius(int r)
|
160
|
{
|
161
|
mRadius = ( r==100 ? 100.0f : r/200.0f);
|
162
|
mRegion.set3(mRadius*scrWidth);
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
// keep aborting the 'released' effects, otherwise we are quickly going to run out of room in
|
167
|
// effect queues.
|
168
|
|
169
|
public void effectFinished(final long effectID)
|
170
|
{
|
171
|
mEffects.abortById(effectID);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
public void onDrawFrame(GL10 glUnused)
|
177
|
{
|
178
|
mScreen.render(System.currentTimeMillis());
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
184
|
{
|
185
|
scrHeight = height;
|
186
|
scrWidth = width;
|
187
|
|
188
|
mRegion.set3(mRadius*scrWidth);
|
189
|
|
190
|
Canvas stretchCanvas;
|
191
|
int w=width/2;
|
192
|
int h=height/2;
|
193
|
|
194
|
if( mMesh!=null ) mMesh.markForDeletion();
|
195
|
mMesh = new MeshRectangles(50,50*h/w);
|
196
|
mMesh.setStretch(w,h,0);
|
197
|
|
198
|
Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
|
199
|
stretchCanvas = new Canvas(stretchBitmap);
|
200
|
|
201
|
Paint paint = new Paint();
|
202
|
paint.setColor(0xff008800);
|
203
|
paint.setStyle(Style.FILL);
|
204
|
stretchCanvas.drawRect(0, 0, w, h, paint);
|
205
|
paint.setColor(0xffffffff);
|
206
|
|
207
|
for(int i=0; i<=NUM_LINES ; i++ )
|
208
|
{
|
209
|
stretchCanvas.drawRect(w*i/NUM_LINES-1, 0, w*i/NUM_LINES+1, h , paint);
|
210
|
stretchCanvas.drawRect( 0, h*i/NUM_LINES-1, w , h*i/NUM_LINES+1, paint);
|
211
|
}
|
212
|
|
213
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
214
|
mTexture.setTexture(stretchBitmap);
|
215
|
|
216
|
mMove.set(scrWidth/4,scrHeight/4,0);
|
217
|
|
218
|
mScreen.detachAll();
|
219
|
mScreen.attach(mTexture,mEffects,mMesh);
|
220
|
|
221
|
mScreen.resize(width, height);
|
222
|
}
|
223
|
|
224
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225
|
|
226
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
227
|
{
|
228
|
VertexEffectDistort.enable();
|
229
|
VertexEffectDeform.enable();
|
230
|
|
231
|
try
|
232
|
{
|
233
|
DistortedLibrary.onCreate(mView.getContext());
|
234
|
}
|
235
|
catch(Exception ex)
|
236
|
{
|
237
|
android.util.Log.e("Deform", ex.toString() );
|
238
|
}
|
239
|
}
|
240
|
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
|
243
|
void down(int x, int y)
|
244
|
{
|
245
|
int xt = x-scrWidth /4;
|
246
|
int yt = y-scrHeight/4;
|
247
|
|
248
|
if( xt<0 ) xt=0;
|
249
|
if( xt>scrWidth /2 ) xt=scrWidth/2;
|
250
|
if( yt<0 ) yt=0;
|
251
|
if( yt>scrHeight/2 ) yt=scrHeight/2;
|
252
|
|
253
|
mTouchPoint.set(xt, scrHeight/2-yt,0); // OpenGL coord system and 2D coords have inverted Y axis
|
254
|
|
255
|
switch(mMode)
|
256
|
{
|
257
|
case DISTORT: vDistort[0].set(0,0,0);
|
258
|
mEffects.apply(mMovingDistort);
|
259
|
mLastEffect = mMovingDistort.getID();
|
260
|
break;
|
261
|
case DEFORM : vDeform[0].set(0,0,0);
|
262
|
mEffects.apply(mMovingDeform);
|
263
|
mLastEffect = mMovingDeform.getID();
|
264
|
break;
|
265
|
case SHEAR : vShear[0].set(0,0,0);
|
266
|
mEffects.apply(mMovingShear);
|
267
|
mLastEffect = mMovingShear.getID();
|
268
|
break;
|
269
|
}
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
void move(int x, int y)
|
275
|
{
|
276
|
switch(mMode)
|
277
|
{
|
278
|
case DISTORT: vDistort[0].set(x,-y,0);
|
279
|
break;
|
280
|
case DEFORM: vDeform[0].set(x,-y,0);
|
281
|
break;
|
282
|
case SHEAR: vShear[0].set( (float)x/(scrWidth/2), (float)(-y)/(scrHeight/2), 0);
|
283
|
break;
|
284
|
}
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
void up()
|
290
|
{
|
291
|
mEffects.abortById(mLastEffect);
|
292
|
|
293
|
float damp = -0.65f;
|
294
|
|
295
|
switch(mMode)
|
296
|
{
|
297
|
case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
|
298
|
{
|
299
|
vDistort[i].set( vDistort[i-1].get0()*damp, vDistort[i-1].get1()*damp, 0 );
|
300
|
}
|
301
|
vDistort[NUM_VECTORS-1].set(0,0,0);
|
302
|
mEffects.apply(mReleasedDistort);
|
303
|
mReleasedDistort.notifyWhenFinished(this);
|
304
|
break;
|
305
|
case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
|
306
|
{
|
307
|
vDeform[i].set( vDeform[i-1].get0()*damp, vDeform[i-1].get1()*damp, 0 );
|
308
|
}
|
309
|
vDeform[NUM_VECTORS-1].set(0,0,0);
|
310
|
mEffects.apply(mReleasedDeform);
|
311
|
mReleasedDeform.notifyWhenFinished(this);
|
312
|
break;
|
313
|
case SHEAR : for(int i=1; i<NUM_VECTORS-1; i++)
|
314
|
{
|
315
|
vShear[i].set( vShear[i-1].get0()*damp, vShear[i-1].get1()*damp, 0 );
|
316
|
}
|
317
|
vShear[NUM_VECTORS-1].set(0,0,0);
|
318
|
mEffects.apply(mReleasedShear);
|
319
|
mReleasedShear.notifyWhenFinished(this);
|
320
|
break;
|
321
|
}
|
322
|
}
|
323
|
}
|