Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 30c71dd5

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.set(0,0,r); 
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.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
169
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
170
    
171
      long time = System.currentTimeMillis();
172
      
173
      stretch.draw(time);
174
      
175
      mPaint.setColor(0xffffffff);
176
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
177
      mPaint.setColor(0xff000000);
178
      fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
179
      
180
      fps.setBitmap(fpsBitmap);
181
      fps.draw(time);
182
      
183
      computeFPS(time);
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
189
      { 
190
      scrHeight = height;
191
      scrWidth  = width;
192
      
193
      Distorted.onSurfaceChanged(width, height);
194
      
195
      if( bitmapCreated==false )
196
        {
197
        createBitmap(scrWidth/2,scrHeight/2);
198
        stretch.move( new Static3D(scrWidth/4,scrHeight/4,0) );
199
        fps.move( new Static3D(5,5,0) );
200
        bitmapCreated=true;
201
        }
202
      else
203
        {
204
        stretch.abortEffects(EffectTypes.VERTEX);
205
        stretch.abortEffects(EffectTypes.FRAGMENT);
206
        stretch.abortEffects(EffectNames.SHEAR);
207
        }
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
    
212
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
213
      {  
214
      try
215
        {
216
        Distorted.onSurfaceCreated(mView.getContext());
217
        }
218
      catch(Exception ex)
219
        {
220
        android.util.Log.e("DeformRenderer", ex.toString() );
221
        }
222
      }
223
    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

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

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

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

    
311
      float damp = -0.65f;
312

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

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

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

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