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.main.Distorted;
|
26
|
import org.distorted.library.main.DistortedEffects;
|
27
|
import org.distorted.library.main.DistortedScreen;
|
28
|
import org.distorted.library.main.DistortedTexture;
|
29
|
import org.distorted.library.main.MeshObject;
|
30
|
import org.distorted.library.main.MeshFlat;
|
31
|
|
32
|
import org.distorted.library.EffectNames;
|
33
|
import org.distorted.library.type.Dynamic3D;
|
34
|
import org.distorted.library.type.Static3D;
|
35
|
import org.distorted.library.type.Static4D;
|
36
|
|
37
|
import android.graphics.Bitmap;
|
38
|
import android.graphics.Canvas;
|
39
|
import android.graphics.Paint;
|
40
|
import android.graphics.Paint.Style;
|
41
|
import android.opengl.GLSurfaceView;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
class DeformRenderer implements GLSurfaceView.Renderer
|
46
|
{
|
47
|
private static final int NUM_VECTORS = 8;
|
48
|
private static final int NUM_LINES = 10;
|
49
|
|
50
|
private GLSurfaceView mView;
|
51
|
private DistortedTexture stretchTexture;
|
52
|
private DistortedEffects stretchEffects;
|
53
|
private MeshObject stretchMesh;
|
54
|
private DistortedScreen mScreen;
|
55
|
private Static3D touchPoint;
|
56
|
|
57
|
private Dynamic3D mReleasedDistortDynamic;
|
58
|
private Dynamic3D mMovingDistortDynamic;
|
59
|
private Static3D[] vDistort;
|
60
|
private Dynamic3D mReleasedDeformDynamic;
|
61
|
private Dynamic3D mMovingDeformDynamic;
|
62
|
private Static3D[] vDeform;
|
63
|
private Dynamic3D mReleasedShearDynamic;
|
64
|
private Dynamic3D mMovingShearDynamic;
|
65
|
private Static3D[] vShear;
|
66
|
|
67
|
private Static4D mRegion;
|
68
|
private int scrHeight, scrWidth;
|
69
|
private float mRadius;
|
70
|
|
71
|
private EffectNames mMode = EffectNames.DISTORT;
|
72
|
private long mLastEffect = -1;
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
DeformRenderer(GLSurfaceView view)
|
77
|
{
|
78
|
mView = view;
|
79
|
|
80
|
stretchEffects = new DistortedEffects();
|
81
|
mRegion = new Static4D(0,0,0,0);
|
82
|
|
83
|
// DISTORT
|
84
|
mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
85
|
mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
|
86
|
mMovingDistortDynamic = new Dynamic3D(0,0.5f);
|
87
|
mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
|
88
|
|
89
|
vDistort = new Static3D[NUM_VECTORS];
|
90
|
|
91
|
for(int i=0; i<NUM_VECTORS; i++)
|
92
|
{
|
93
|
vDistort[i] = new Static3D(0,0,0);
|
94
|
mReleasedDistortDynamic.add(vDistort[i]);
|
95
|
}
|
96
|
|
97
|
mMovingDistortDynamic.add(vDistort[0]);
|
98
|
|
99
|
// Deform
|
100
|
mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
101
|
mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
|
102
|
mMovingDeformDynamic = new Dynamic3D(0,0.5f);
|
103
|
mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
|
104
|
|
105
|
vDeform = new Static3D[NUM_VECTORS];
|
106
|
|
107
|
for(int i=0; i<NUM_VECTORS; i++)
|
108
|
{
|
109
|
vDeform[i] = new Static3D(0,0,0);
|
110
|
mReleasedDeformDynamic.add(vDeform[i]);
|
111
|
}
|
112
|
|
113
|
mMovingDeformDynamic.add(vDeform[0]);
|
114
|
|
115
|
// Shear
|
116
|
mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
|
117
|
mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
|
118
|
mMovingShearDynamic = new Dynamic3D(0,0.5f);
|
119
|
mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
|
120
|
|
121
|
vShear = new Static3D[NUM_VECTORS];
|
122
|
|
123
|
for(int i=0; i<NUM_VECTORS; i++)
|
124
|
{
|
125
|
vShear[i] = new Static3D(0,0,0);
|
126
|
mReleasedShearDynamic.add(vShear[i]);
|
127
|
}
|
128
|
|
129
|
mMovingShearDynamic.add(vShear[0]);
|
130
|
|
131
|
mScreen = new DistortedScreen(mView);
|
132
|
mScreen.setDebug(DistortedScreen.DEBUG_FPS);
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
void setMode(EffectNames mode)
|
138
|
{
|
139
|
mMode = mode;
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
void setRegionRadius(int r)
|
145
|
{
|
146
|
mRadius = ( r==100 ? 100.0f : r/100.0f);
|
147
|
mRegion.set3(mRadius*scrWidth);
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
public void onDrawFrame(GL10 glUnused)
|
153
|
{
|
154
|
mScreen.render(System.currentTimeMillis());
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
160
|
{
|
161
|
scrHeight = height;
|
162
|
scrWidth = width;
|
163
|
|
164
|
mRegion.set3(mRadius*scrWidth);
|
165
|
|
166
|
Canvas stretchCanvas;
|
167
|
int w=width/2;
|
168
|
int h=height/2;
|
169
|
|
170
|
if( stretchMesh!=null ) stretchMesh.markForDeletion();
|
171
|
|
172
|
stretchMesh = new MeshFlat(50,50*h/w);
|
173
|
Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
|
174
|
stretchCanvas = new Canvas(stretchBitmap);
|
175
|
|
176
|
Paint paint = new Paint();
|
177
|
paint.setColor(0xff008800);
|
178
|
paint.setStyle(Style.FILL);
|
179
|
stretchCanvas.drawRect(0, 0, w, h, paint);
|
180
|
paint.setColor(0xffffffff);
|
181
|
|
182
|
for(int i=0; i<=NUM_LINES ; i++ )
|
183
|
{
|
184
|
stretchCanvas.drawRect(w*i/NUM_LINES - 1, 0, w*i/NUM_LINES + 1, h , paint);
|
185
|
stretchCanvas.drawRect( 0, h *i/NUM_LINES -1, w , h*i/NUM_LINES + 1, paint);
|
186
|
}
|
187
|
|
188
|
touchPoint= new Static3D(0,0,0);
|
189
|
|
190
|
if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
|
191
|
stretchTexture.setTexture(stretchBitmap);
|
192
|
|
193
|
stretchEffects.abortAllEffects();
|
194
|
stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
|
195
|
|
196
|
mScreen.detachAll();
|
197
|
mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
|
198
|
|
199
|
mScreen.resize(width, height);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
205
|
{
|
206
|
DistortedEffects.enableEffect(EffectNames.DISTORT);
|
207
|
DistortedEffects.enableEffect(EffectNames.DEFORM);
|
208
|
|
209
|
try
|
210
|
{
|
211
|
Distorted.onCreate(mView.getContext());
|
212
|
}
|
213
|
catch(Exception ex)
|
214
|
{
|
215
|
android.util.Log.e("DeformRenderer", ex.toString() );
|
216
|
}
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
void down(int x, int y)
|
222
|
{
|
223
|
int xt = x-scrWidth/4;
|
224
|
int yt = y-scrHeight/4;
|
225
|
|
226
|
if( xt<0 ) xt=0;
|
227
|
if( xt>scrWidth/2 ) xt=scrWidth/2;
|
228
|
if( yt<0 ) yt=0;
|
229
|
if( yt>scrHeight/2 ) yt=scrHeight/2;
|
230
|
|
231
|
touchPoint.set(xt,yt,0);
|
232
|
|
233
|
switch(mMode)
|
234
|
{
|
235
|
case DISTORT: vDistort[0].set(0,0,0);
|
236
|
mLastEffect = stretchEffects.distort( mMovingDistortDynamic, touchPoint, mRegion);
|
237
|
break;
|
238
|
case DEFORM : vDeform[0].set(0,0,0);
|
239
|
mLastEffect = stretchEffects.deform( mMovingDeformDynamic, touchPoint, mRegion);
|
240
|
break;
|
241
|
case SHEAR : vShear[0].set(0,0,0);
|
242
|
mLastEffect = stretchEffects.shear(mMovingShearDynamic, touchPoint);
|
243
|
break;
|
244
|
}
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
void move(int x, int y)
|
250
|
{
|
251
|
switch(mMode)
|
252
|
{
|
253
|
case DISTORT: vDistort[0].set(x,y);
|
254
|
break;
|
255
|
case DEFORM: vDeform[0].set(x,y);
|
256
|
break;
|
257
|
case SHEAR: vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
|
258
|
break;
|
259
|
}
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
void up()
|
265
|
{
|
266
|
stretchEffects.abortEffect(mLastEffect);
|
267
|
|
268
|
float damp = -0.65f;
|
269
|
|
270
|
switch(mMode)
|
271
|
{
|
272
|
case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
|
273
|
{
|
274
|
vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
|
275
|
}
|
276
|
vDistort[NUM_VECTORS-1].set(0,0);
|
277
|
stretchEffects.distort( mReleasedDistortDynamic, touchPoint, mRegion);
|
278
|
break;
|
279
|
case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
|
280
|
{
|
281
|
vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
|
282
|
}
|
283
|
vDeform[NUM_VECTORS-1].set(0,0);
|
284
|
stretchEffects.deform( mReleasedDeformDynamic, touchPoint, mRegion);
|
285
|
break;
|
286
|
case SHEAR : for(int i=1; i<NUM_VECTORS-1; i++)
|
287
|
{
|
288
|
vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
|
289
|
}
|
290
|
vShear[NUM_VECTORS-1].set(0,0);
|
291
|
stretchEffects.shear(mReleasedShearDynamic, touchPoint);
|
292
|
break;
|
293
|
}
|
294
|
}
|
295
|
}
|