Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 39a0d81b

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