Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 91786be2

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

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

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

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

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

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

    
308
    void up()
309
      {
310
      stretch.abortEffect(mLastEffect);
311

    
312
      float damp = -0.65f;
313

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

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

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