Project

General

Profile

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

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

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

    
72
    private GLSurfaceView mView;
73
    private DistortedTexture mTex1, mTex2;
74
    private DistortedEffects[] mEffects;
75
    private DistortedNode[] mNode;
76
    private DistortedEffectsPostprocess mPostEffects;
77
    private Static3D[]  mMoveVector;
78
    private Dynamic3D[] mMoveDynamic;
79
    private Static1D  mBlurVector;
80
    private Dynamic1D  mBlurDynamic;
81
    private DistortedScreen mScreen;
82
    private DynamicQuat mQuatInt1, mQuatInt2;
83
    private int mDistance;
84
    private boolean[] mBlurStatus;
85
    private int mScreenW, mScreenH;
86

    
87
    Static4D mQuat1, mQuat2;
88
    int mScreenMin;
89

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
    MultiblurRenderer(GLSurfaceView v)
105
      {
106
      mView = v;
107
      mDistance = -1;
108

    
109
      fpsW = 120;
110
      fpsH =  70;
111

    
112
      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
      fpsEffects = new DistortedEffects();
117
      durations = new long[NUM_FRAMES+1];
118
      currDuration = 0;
119

    
120
      mPaint = new Paint();
121
      mPaint.setAntiAlias(true);
122
      mPaint.setTextAlign(Paint.Align.CENTER);
123
      mPaint.setTextSize(0.7f*fpsH);
124

    
125
      for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
126

    
127
      mBlurStatus = new boolean[NUM_OBJECTS];
128
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
129
      mMoveVector = new Static3D[NUM_OBJECTS];
130
      mNode       = new DistortedNode[NUM_OBJECTS];
131
      mEffects    = new DistortedEffects[NUM_OBJECTS];
132
      mPostEffects= new DistortedEffectsPostprocess();
133

    
134
      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
        mBlurStatus[i] = false;
142
        }
143

    
144
      mBlurDynamic= new Dynamic1D();
145
      mBlurVector = new Static1D(10);
146
      mBlurDynamic.add(mBlurVector);
147
      mPostEffects.blur(mBlurDynamic);
148

    
149
      MeshCubes mesh = new MeshCubes(1,1,false);
150

    
151
      mTex1 = new DistortedTexture(1,1);
152
      mTex2 = new DistortedTexture(1,1);
153

    
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
      mScreen = new DistortedScreen();
164

    
165
      for(int i=0; i<NUM_OBJECTS; i++)
166
        {
167
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, mEffects[i], mesh);
168
        mScreen.attach(mNode[i]);
169
        }
170

    
171
      mScreen.attach(fpsTexture,fpsEffects,new MeshFlat(1,1));
172

    
173
      mBlurStatus[0] = true;
174
      mNode[0].setPostprocessEffects(mPostEffects);
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
   
179
    public void onDrawFrame(GL10 glUnused) 
180
      {
181
      mPaint.setColor(0xffffffff);
182
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
183
      mPaint.setColor(0xff000000);
184
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
185
      fpsTexture.setTexture(fpsBitmap);
186

    
187
      long time = System.currentTimeMillis();
188

    
189
      mScreen.render(time);
190
      computeFPS(time);
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
    
195
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
196
      {
197
      mScreenMin = width<height ? width:height;
198
      mScreenW = width;
199
      mScreenH = height;
200

    
201
      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

    
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
        mEffects[i].move(mMoveDynamic[i]);
217
        mEffects[i].scale(factor);
218
        }
219

    
220
      computeMoveVectors();
221
      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
      DistortedEffects.enableEffect(EffectNames.BLUR);
251

    
252
      try
253
        {
254
        Distorted.onCreate(mView.getContext());
255
        }
256
      catch(Exception ex)
257
        {
258
        android.util.Log.e("Multiblur", ex.getMessage() );
259
        }
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    private void computeMoveVectors()
282
      {
283
      float size = 2.6f*mDistance;
284
      float sizeX= size/mScreenW;
285
      float sizeY= size/mScreenH;
286

    
287
      for(int i=0; i<NUM_OBJECTS; i++)
288
        {
289
        mMoveVector[i].set(sizeX*MOVE_VEC[3*i], sizeY*MOVE_VEC[3*i+1], sizeX*MOVE_VEC[3*i+2]);
290
        }
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    void setDistance(int distance)
296
      {
297
      mDistance = distance;
298
      computeMoveVectors();
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
    void setRange(int range)
304
      {
305
      mBlurVector.set(range/2);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
   boolean[] getChecked()
311
     {
312
     return mBlurStatus;
313
     }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
   void setChecked(int number, boolean checked)
318
     {
319
     if( number>=0 && number<=7 )
320
       {
321
       if( checked && !mBlurStatus[number] )
322
         {
323
         mBlurStatus[number] = true;
324
         mNode[number].setPostprocessEffects(mPostEffects);
325
         }
326
       if( !checked && mBlurStatus[number] )
327
         {
328
         mBlurStatus[number] = false;
329
         mNode[number].setPostprocessEffects(null);
330
         }
331
       }
332
     else
333
       {
334
       android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
335
       }
336

    
337
     //android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
338
     }
339
}
(2-2/3)