Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 629120e4

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 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
      fpsEffects.move( new Static3D(5,5,0) );
118
      durations = new long[NUM_FRAMES+1];
119
      currDuration = 0;
120

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

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

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

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

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

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

    
152
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
153
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
154

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

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

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

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

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

    
188
      long time = System.currentTimeMillis();
189

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

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

    
200
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
201
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
202
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
203

    
204
      for(int i=0; i<NUM_OBJECTS; i++)
205
        {
206
        mEffects[i].abortEffects(EffectTypes.MATRIX);
207

    
208
        mEffects[i].move(moveVec);
209
        mEffects[i].scale(factor);
210
        mEffects[i].quaternion(mQuatInt1, center);
211
        mEffects[i].quaternion(mQuatInt2, center);
212
        mEffects[i].move(mMoveDynamic[i]);
213
        }
214

    
215
      computeMoveVectors();
216
      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
      DistortedEffects.enableEffect(EffectNames.BLUR);
246

    
247
      try
248
        {
249
        Distorted.onCreate(mView.getContext());
250
        }
251
      catch(Exception ex)
252
        {
253
        android.util.Log.e("Multiblur", ex.getMessage() );
254
        }
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    private void computeMoveVectors()
277
      {
278
      float size= 0.026f*OBJ_SIZE*mDistance;
279

    
280
      for(int i=0; i<NUM_OBJECTS; i++)
281
        {
282
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
283
        }
284
      }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
    void setDistance(int distance)
289
      {
290
      mDistance = distance;
291
      computeMoveVectors();
292
      }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
    void setRange(int range)
297
      {
298
      mBlurVector.set(range/2);
299
      }
300

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

    
303
   boolean[] getChecked()
304
     {
305
     return mBlurStatus;
306
     }
307

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

    
310
   void setChecked(int number, boolean checked)
311
     {
312
     if( number>=0 && number<=7 )
313
       {
314
       if( checked && !mBlurStatus[number] )
315
         {
316
         mBlurStatus[number] = true;
317
         mNode[number].setPostprocessEffects(mPostEffects);
318
         }
319
       if( !checked && mBlurStatus[number] )
320
         {
321
         mBlurStatus[number] = false;
322
         mNode[number].setPostprocessEffects(null);
323
         }
324
       }
325
     else
326
       {
327
       android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
328
       }
329

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