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.multiblur;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.graphics.Canvas;
|
25
|
import android.graphics.Paint;
|
26
|
import android.opengl.GLSurfaceView;
|
27
|
|
28
|
import org.distorted.examples.R;
|
29
|
import org.distorted.library.Distorted;
|
30
|
import org.distorted.library.DistortedEffects;
|
31
|
import org.distorted.library.DistortedEffectsPostprocess;
|
32
|
import org.distorted.library.DistortedNode;
|
33
|
import org.distorted.library.DistortedScreen;
|
34
|
import org.distorted.library.DistortedTexture;
|
35
|
import org.distorted.library.EffectNames;
|
36
|
import org.distorted.library.EffectQuality;
|
37
|
import org.distorted.library.EffectTypes;
|
38
|
import org.distorted.library.MeshCubes;
|
39
|
import org.distorted.library.MeshFlat;
|
40
|
import org.distorted.library.type.Dynamic1D;
|
41
|
import org.distorted.library.type.Dynamic3D;
|
42
|
import org.distorted.library.type.DynamicQuat;
|
43
|
import org.distorted.library.type.Static1D;
|
44
|
import org.distorted.library.type.Static3D;
|
45
|
import org.distorted.library.type.Static4D;
|
46
|
|
47
|
import java.io.IOException;
|
48
|
import java.io.InputStream;
|
49
|
|
50
|
import javax.microedition.khronos.egl.EGLConfig;
|
51
|
import javax.microedition.khronos.opengles.GL10;
|
52
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
class MultiblurRenderer implements GLSurfaceView.Renderer
|
56
|
{
|
57
|
private static final int NUM_FRAMES = 100;
|
58
|
|
59
|
private static final int[] MOVE_VEC =
|
60
|
{
|
61
|
+1,+1,+1,
|
62
|
+1,-1,+1,
|
63
|
+1,+1,-1,
|
64
|
+1,-1,-1,
|
65
|
-1,+1,+1,
|
66
|
-1,-1,+1,
|
67
|
-1,+1,-1,
|
68
|
-1,-1,-1
|
69
|
};
|
70
|
|
71
|
private static final int NUM_OBJECTS = MOVE_VEC.length/3;
|
72
|
private static final int OBJ_SIZE = 100;
|
73
|
|
74
|
private GLSurfaceView mView;
|
75
|
private DistortedTexture mTex1, mTex2;
|
76
|
private DistortedEffects[] mEffects;
|
77
|
private DistortedNode[] mNode;
|
78
|
private DistortedEffectsPostprocess mPostEffects;
|
79
|
private Static3D[] mMoveVector;
|
80
|
private Dynamic3D[] mMoveDynamic;
|
81
|
private Static1D mBlurVector;
|
82
|
private Dynamic1D mBlurDynamic;
|
83
|
private DistortedScreen mScreen;
|
84
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
85
|
private int mDistance;
|
86
|
private boolean[] mBlurStatus;
|
87
|
|
88
|
Static4D mQuat1, mQuat2;
|
89
|
int mScreenMin;
|
90
|
|
91
|
// FPS
|
92
|
private DistortedTexture fpsTexture;
|
93
|
private DistortedEffects fpsEffects;
|
94
|
private Canvas fpsCanvas;
|
95
|
private Bitmap fpsBitmap;
|
96
|
private Paint mPaint;
|
97
|
private int fpsH, fpsW;
|
98
|
private String fpsString = "";
|
99
|
private long lastTime=0;
|
100
|
private long[] durations;
|
101
|
private int currDuration;
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
MultiblurRenderer(GLSurfaceView v)
|
106
|
{
|
107
|
mView = v;
|
108
|
mDistance = -1;
|
109
|
|
110
|
fpsW = 120;
|
111
|
fpsH = 70;
|
112
|
|
113
|
fpsTexture = new DistortedTexture(fpsW,fpsH);
|
114
|
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
|
115
|
fpsTexture.setTexture(fpsBitmap);
|
116
|
fpsCanvas = new Canvas(fpsBitmap);
|
117
|
fpsEffects = new DistortedEffects();
|
118
|
fpsEffects.move( new Static3D(5,5,0) );
|
119
|
durations = new long[NUM_FRAMES+1];
|
120
|
currDuration = 0;
|
121
|
|
122
|
mPaint = new Paint();
|
123
|
mPaint.setAntiAlias(true);
|
124
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
125
|
mPaint.setTextSize(0.7f*fpsH);
|
126
|
|
127
|
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
|
128
|
|
129
|
mBlurStatus = new boolean[NUM_OBJECTS];
|
130
|
mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
|
131
|
mMoveVector = new Static3D[NUM_OBJECTS];
|
132
|
mNode = new DistortedNode[NUM_OBJECTS];
|
133
|
mEffects = new DistortedEffects[NUM_OBJECTS];
|
134
|
mPostEffects= new DistortedEffectsPostprocess();
|
135
|
|
136
|
for(int i=0; i<NUM_OBJECTS; i++)
|
137
|
{
|
138
|
mMoveVector[i] = new Static3D(0,0,0);
|
139
|
mEffects[i] = new DistortedEffects();
|
140
|
mMoveDynamic[i] = new Dynamic3D();
|
141
|
|
142
|
mMoveDynamic[i].add(mMoveVector[i]);
|
143
|
mBlurStatus[i] = false;
|
144
|
}
|
145
|
|
146
|
mBlurDynamic= new Dynamic1D();
|
147
|
mBlurVector = new Static1D(10);
|
148
|
mBlurDynamic.add(mBlurVector);
|
149
|
mPostEffects.blur(mBlurDynamic);
|
150
|
|
151
|
MeshCubes mesh = new MeshCubes(1,1,false);
|
152
|
|
153
|
mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
|
154
|
mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
|
155
|
|
156
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
157
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
158
|
|
159
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
160
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
161
|
|
162
|
mQuatInt1.add(mQuat1);
|
163
|
mQuatInt2.add(mQuat2);
|
164
|
|
165
|
mScreen = new DistortedScreen();
|
166
|
|
167
|
for(int i=0; i<NUM_OBJECTS; i++)
|
168
|
{
|
169
|
mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, mEffects[i], mesh);
|
170
|
mScreen.attach(mNode[i]);
|
171
|
}
|
172
|
|
173
|
mScreen.attach(fpsTexture,fpsEffects,new MeshFlat(1,1));
|
174
|
|
175
|
mBlurStatus[0] = true;
|
176
|
mNode[0].setPostprocessEffects(mPostEffects);
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
void setQuality(EffectQuality quality)
|
182
|
{
|
183
|
mPostEffects.setQuality(quality);
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public void onDrawFrame(GL10 glUnused)
|
189
|
{
|
190
|
mPaint.setColor(0xffffffff);
|
191
|
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
|
192
|
mPaint.setColor(0xff000000);
|
193
|
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
|
194
|
fpsTexture.setTexture(fpsBitmap);
|
195
|
|
196
|
long time = System.currentTimeMillis();
|
197
|
|
198
|
mScreen.render(time);
|
199
|
computeFPS(time);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
205
|
{
|
206
|
mScreenMin = width<height ? width:height;
|
207
|
|
208
|
float factor = 0.15f*mScreenMin/OBJ_SIZE;
|
209
|
Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
|
210
|
Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
|
211
|
|
212
|
for(int i=0; i<NUM_OBJECTS; i++)
|
213
|
{
|
214
|
mEffects[i].abortEffects(EffectTypes.MATRIX);
|
215
|
|
216
|
mEffects[i].move(moveVec);
|
217
|
mEffects[i].scale(factor);
|
218
|
mEffects[i].quaternion(mQuatInt1, center);
|
219
|
mEffects[i].quaternion(mQuatInt2, center);
|
220
|
mEffects[i].move(mMoveDynamic[i]);
|
221
|
}
|
222
|
|
223
|
computeMoveVectors();
|
224
|
mScreen.resize(width, height);
|
225
|
}
|
226
|
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
|
229
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
230
|
{
|
231
|
InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
|
232
|
InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
|
233
|
Bitmap bitmap1,bitmap2;
|
234
|
|
235
|
try
|
236
|
{
|
237
|
bitmap1 = BitmapFactory.decodeStream(is1);
|
238
|
bitmap2 = BitmapFactory.decodeStream(is2);
|
239
|
}
|
240
|
finally
|
241
|
{
|
242
|
try
|
243
|
{
|
244
|
is1.close();
|
245
|
is2.close();
|
246
|
}
|
247
|
catch(IOException e) { }
|
248
|
}
|
249
|
|
250
|
mTex1.setTexture(bitmap1);
|
251
|
mTex2.setTexture(bitmap2);
|
252
|
|
253
|
DistortedEffects.enableEffect(EffectNames.BLUR);
|
254
|
|
255
|
try
|
256
|
{
|
257
|
Distorted.onCreate(mView.getContext());
|
258
|
}
|
259
|
catch(Exception ex)
|
260
|
{
|
261
|
android.util.Log.e("Multiblur", ex.getMessage() );
|
262
|
}
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
private void computeFPS(long currentTime)
|
268
|
{
|
269
|
if( lastTime!=0 )
|
270
|
{
|
271
|
currDuration++;
|
272
|
if( currDuration>=NUM_FRAMES ) currDuration = 0;
|
273
|
durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
|
274
|
durations[currDuration] = currentTime-lastTime;
|
275
|
|
276
|
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
277
|
}
|
278
|
|
279
|
lastTime = currentTime;
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
private void computeMoveVectors()
|
285
|
{
|
286
|
float size= 0.026f*OBJ_SIZE*mDistance;
|
287
|
|
288
|
for(int i=0; i<NUM_OBJECTS; i++)
|
289
|
{
|
290
|
mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
|
291
|
}
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
void setDistance(int distance)
|
297
|
{
|
298
|
mDistance = distance;
|
299
|
computeMoveVectors();
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
void setRange(int range)
|
305
|
{
|
306
|
mBlurVector.set(range/2);
|
307
|
}
|
308
|
|
309
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
310
|
|
311
|
boolean[] getChecked()
|
312
|
{
|
313
|
return mBlurStatus;
|
314
|
}
|
315
|
|
316
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
317
|
|
318
|
void setChecked(int number, boolean checked)
|
319
|
{
|
320
|
if( number>=0 && number<=7 )
|
321
|
{
|
322
|
if( checked && !mBlurStatus[number] )
|
323
|
{
|
324
|
mBlurStatus[number] = true;
|
325
|
mNode[number].setPostprocessEffects(mPostEffects);
|
326
|
}
|
327
|
if( !checked && mBlurStatus[number] )
|
328
|
{
|
329
|
mBlurStatus[number] = false;
|
330
|
mNode[number].setPostprocessEffects(null);
|
331
|
}
|
332
|
}
|
333
|
else
|
334
|
{
|
335
|
android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
|
336
|
}
|
337
|
|
338
|
//android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
|
339
|
}
|
340
|
}
|