Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 6161fe9a

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.deform;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import org.distorted.library.Distorted;
26
import org.distorted.library.DistortedBitmap;
27
import org.distorted.library.EffectNames;
28
import org.distorted.library.EffectTypes;
29
import org.distorted.library.type.Dynamic3D;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32

    
33
import android.graphics.Bitmap;
34
import android.graphics.Canvas;
35
import android.graphics.Paint;
36
import android.graphics.Paint.Style;
37
import android.opengl.GLES20;
38
import android.opengl.GLSurfaceView;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class DeformRenderer implements GLSurfaceView.Renderer 
43
{
44
   private static final int NUM_VECTORS = 8;
45
   private static final int NUM_LINES = 10;
46
   private static final int NUM_FRAMES =10;
47

    
48
   private GLSurfaceView mView;
49
   private DistortedBitmap fps;
50
   private DistortedBitmap stretch;
51
   private Static3D touchPoint;
52

    
53
   private Dynamic3D mReleasedDistortDynamic;
54
   private Dynamic3D mMovingDistortDynamic;
55
   private Static3D[] vDistort;
56
   private Dynamic3D mReleasedDeformDynamic;
57
   private Dynamic3D mMovingDeformDynamic;
58
   private Static3D[] vDeform;
59
   private Dynamic3D mReleasedShearDynamic;
60
   private Dynamic3D mMovingShearDynamic;
61
   private Static3D[] vShear;
62

    
63
   private Static4D mRegion;
64
   private Canvas fpsCanvas;
65
   private Bitmap fpsBitmap;
66
   private int scrHeight, scrWidth;
67
   private Paint mPaint;
68
   private int fpsH, fpsW;
69
   private String fpsString = "";
70
   private long lastTime=0;
71
   private long[] durations;
72
   private int currDuration;
73

    
74
   private EffectNames mMode = EffectNames.DISTORT;
75
   private boolean bitmapCreated = false;
76
   private long mLastEffect = -1;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   DeformRenderer(GLSurfaceView view)
81
      { 
82
      mView = view;
83
      
84
      mRegion = new Static4D(0,0,0,0);
85

    
86
      durations = new long[NUM_FRAMES+1];
87
      currDuration = 0;
88

    
89
      for(int i=0; i<NUM_FRAMES+1; i++)
90
        {
91
        durations[i]=0;
92
        }
93

    
94
      // DISTORT
95
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
96
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
97
      mMovingDistortDynamic = new Dynamic3D(0,0.5f);
98
      mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
99

    
100
      vDistort = new Static3D[NUM_VECTORS];
101

    
102
      for(int i=0; i<NUM_VECTORS; i++)
103
        {
104
        vDistort[i] = new Static3D(0,0,0);
105
        mReleasedDistortDynamic.add(vDistort[i]);
106
        }
107

    
108
      mMovingDistortDynamic.add(vDistort[0]);
109

    
110
      // Deform
111
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
112
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
113
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
114
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
115

    
116
      vDeform = new Static3D[NUM_VECTORS];
117

    
118
      for(int i=0; i<NUM_VECTORS; i++)
119
        {
120
        vDeform[i] = new Static3D(0,0,0);
121
        mReleasedDeformDynamic.add(vDeform[i]);
122
        }
123

    
124
      mMovingDeformDynamic.add(vDeform[0]);
125

    
126
      // Shear
127
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
128
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
129
      mMovingShearDynamic = new Dynamic3D(0,0.5f);
130
      mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
131

    
132
      vShear = new Static3D[NUM_VECTORS];
133

    
134
      for(int i=0; i<NUM_VECTORS; i++)
135
        {
136
        vShear[i] = new Static3D(0,0,0);
137
        mReleasedShearDynamic.add(vShear[i]);
138
        }
139

    
140
      mMovingShearDynamic.add(vShear[0]);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
   void setMode(EffectNames mode)
146
      {
147
      mMode = mode;  
148
      }
149
   
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
   void setRegionRadius(int r)
153
      {
154
      mRegion.set3( r==100 ? 100*scrWidth : r*scrWidth/100.0f);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    public void onPause()
160
      {
161
      bitmapCreated = false;  
162
      }
163
      
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
   
166
    public void onDrawFrame(GL10 glUnused) 
167
      {
168
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
169
    
170
      long time = System.currentTimeMillis();
171
      
172
      stretch.draw(time);
173
      
174
      mPaint.setColor(0xffffffff);
175
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
176
      mPaint.setColor(0xff000000);
177
      fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
178
      
179
      fps.setBitmap(fpsBitmap);
180
      fps.draw(time);
181
      
182
      computeFPS(time);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
188
      { 
189
      scrHeight = height;
190
      scrWidth  = width;
191

    
192
      setRegionRadius(50);
193

    
194
      Distorted.onSurfaceChanged(width, height);
195
      
196
      if( bitmapCreated==false )
197
        {
198
        createBitmap(scrWidth/2,scrHeight/2);
199
        stretch.move( new Static3D(scrWidth/4,scrHeight/4,0) );
200
        fps.move( new Static3D(5,5,0) );
201
        bitmapCreated=true;
202
        }
203
      else
204
        {
205
        stretch.abortEffects(EffectTypes.VERTEX);
206
        stretch.abortEffects(EffectTypes.FRAGMENT);
207
        stretch.abortEffects(EffectNames.SHEAR);
208
        }
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
    
213
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
214
      {
215
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
216

    
217
      try
218
        {
219
        Distorted.onSurfaceCreated(mView.getContext());
220
        }
221
      catch(Exception ex)
222
        {
223
        android.util.Log.e("DeformRenderer", ex.toString() );
224
        }
225
      }
226
    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    private void createBitmap(int w, int h)
230
      {  
231
      Canvas stretchCanvas;   
232
      
233
      mPaint = new Paint();
234
      stretch = new DistortedBitmap(w,h, 50);   
235
      Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
236
      stretchCanvas = new Canvas(stretchBitmap);
237
      
238
      fpsW = scrWidth/5;
239
      fpsH = fpsW/2;
240
        
241
      mPaint.setAntiAlias(true);
242
      mPaint.setTextAlign(Paint.Align.CENTER);
243
      mPaint.setTextSize(2*fpsH/3);
244
      mPaint.setColor(0xff008800);
245
      mPaint.setStyle(Style.FILL);
246
      stretchCanvas.drawRect(0, 0, w, h, mPaint);
247
      mPaint.setColor(0xffffffff);
248
      
249
      for(int i=0; i<=NUM_LINES ; i++ )
250
        {
251
        stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , mPaint);
252
        stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, mPaint);
253
        }
254
        
255
      touchPoint= new Static3D(0,0,0);
256
        
257
      fps = new DistortedBitmap( fpsW, fpsH, 2);
258
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
259
      fpsCanvas = new Canvas(fpsBitmap);
260
        
261
      stretch.setBitmap(stretchBitmap);
262
      fps.setBitmap(fpsBitmap);
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    void down(int x, int y)
268
      {
269
      int xt = x-scrWidth/4;
270
      int yt = y-scrHeight/4;
271
      
272
      if( xt<0 ) xt=0;
273
      if( xt>scrWidth/2 ) xt=scrWidth/2;
274
      if( yt<0 ) yt=0;
275
      if( yt>scrHeight/2 ) yt=scrHeight/2;
276
      
277
      touchPoint.set(xt,yt,0);
278

    
279
      switch(mMode)
280
        {
281
        case DISTORT: vDistort[0].set(0,0,0);
282
                      mLastEffect = stretch.distort( mMovingDistortDynamic, touchPoint, mRegion);
283
                      break;
284
        case DEFORM : vDeform[0].set(0,0,0);
285
                      mLastEffect = stretch.deform( mMovingDeformDynamic, touchPoint, mRegion);
286
                      break;
287
        case SHEAR  : vShear[0].set(0,0,0);
288
                      mLastEffect = stretch.shear(mMovingShearDynamic, touchPoint);
289
                      break;
290
        }                   
291
      }
292
    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    void move(int x, int y)
296
      {
297
      switch(mMode)
298
        {
299
        case DISTORT: vDistort[0].set(x,y);
300
                      break;
301
        case DEFORM:  vDeform[0].set(x,y);
302
                      break;
303
        case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
304
                      break;
305
        }
306
      }
307
    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    void up()
311
      {
312
      stretch.abortEffect(mLastEffect);
313

    
314
      float damp = -0.65f;
315

    
316
      switch(mMode)
317
        {
318
        case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
319
                        {
320
                        vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
321
                        }
322
                      vDistort[NUM_VECTORS-1].set(0,0);
323
                      stretch.distort( mReleasedDistortDynamic, touchPoint, mRegion);
324
                      break;
325
        case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
326
                        {
327
                        vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
328
                        }
329
                      vDeform[NUM_VECTORS-1].set(0,0);
330
                      stretch.deform( mReleasedDeformDynamic, touchPoint, mRegion);
331
                      break;
332
        case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
333
                        {
334
                        vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
335
                        }
336
                      vShear[NUM_VECTORS-1].set(0,0);
337
                      stretch.shear(mReleasedShearDynamic, touchPoint);
338
                      break;
339
        }      
340
      }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
    private void computeFPS(long currentTime)
345
      {
346
      if( lastTime!=0 )
347
        {
348
        currDuration++;
349
        if( currDuration>=NUM_FRAMES ) currDuration = 0;
350
        durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
351
        durations[currDuration] = currentTime-lastTime;
352

    
353
        fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
354
        }
355
      
356
      lastTime = currentTime;   
357
      }
358
}
(2-2/3)