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