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