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