Project

General

Profile

« Previous | Next » 

Revision 7abd1d00

Added by Leszek Koltunski over 8 years ago

make the 'Deform' app much more flexible (now we can mix effects atthe same time!)

View differences:

src/main/java/org/distorted/examples/deform/DeformActivity.java
21 21

  
22 22
import org.distorted.library.Distorted;
23 23
import org.distorted.examples.R;
24
import org.distorted.library.EffectNames;
24 25

  
25 26
import android.app.Activity;
26 27
import android.opengl.GLSurfaceView;
......
35 36

  
36 37
public class DeformActivity extends Activity implements OnSeekBarChangeListener
37 38
{
38
    private SeekBar barR;
39 39
    private TextView textR;
40
    private RadioButton effect;
41
    
40

  
42 41
///////////////////////////////////////////////////////////////////////////////////////////////////
43 42
    
44 43
    @Override
......
46 45
      {
47 46
      super.onCreate(icicle);
48 47
      setContentView(R.layout.deformlayout);
49
       
50
      barR = (SeekBar)findViewById(R.id.deformSeekRadius);
48

  
49
      SeekBar barR = (SeekBar)findViewById(R.id.deformSeekRadius);
51 50
      barR.setOnSeekBarChangeListener(this); 
52 51
        
53 52
      textR = (TextView)findViewById(R.id.deformTextRadius);
......
55 54
      barR.setProgress(50);
56 55
      
57 56
      textR.setText("Radius: 50");
58
      
59
      effect = (RadioButton)findViewById(R.id.deformDistortButton);
57

  
58
      RadioButton effect = (RadioButton)findViewById(R.id.deformDistortButton);
60 59
      effect.setChecked(true);
61
      DeformRenderer.setMode(DeformRenderer.MODE_DISTORT);
60
      DeformRenderer.setMode(EffectNames.DISTORT);
62 61
      }
63 62

  
64 63
///////////////////////////////////////////////////////////////////////////////////////////////////
......
90 89
    @Override
91 90
    protected void onDestroy() 
92 91
      {
93
      android.util.Log.e("activity","onDestroy");  
94
      
95
      Distorted.onDestroy();  
92
      Distorted.onDestroy();
96 93
      super.onDestroy();
97 94
      }
98 95
 
......
100 97
    
101 98
    public void Distort(View v)
102 99
      {
103
      DeformRenderer.setMode(DeformRenderer.MODE_DISTORT);
100
      DeformRenderer.setMode(EffectNames.DISTORT);
104 101
      }     
105 102
    
106 103
///////////////////////////////////////////////////////////////////////////////////////////////////
107 104

  
108 105
    public void Deform(View v)
109 106
      {
110
      DeformRenderer.setMode(DeformRenderer.MODE_DEFORM);
107
      DeformRenderer.setMode(EffectNames.DEFORM);
111 108
      }       
112 109

  
113 110
///////////////////////////////////////////////////////////////////////////////////////////////////
114 111

  
115 112
    public void Shear(View v)
116 113
      {
117
      DeformRenderer.setMode(DeformRenderer.MODE_SHEAR);
114
      DeformRenderer.setMode(EffectNames.SHEAR);
118 115
      }       
119 116
    
120 117
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/deform/DeformRenderer.java
24 24

  
25 25
import org.distorted.library.Distorted;
26 26
import org.distorted.library.DistortedBitmap;
27
import org.distorted.library.EffectNames;
27 28
import org.distorted.library.EffectTypes;
28 29
import org.distorted.library.type.Dynamic3D;
29 30
import org.distorted.library.type.Static3D;
......
40 41

  
41 42
class DeformRenderer implements GLSurfaceView.Renderer 
42 43
{
43
   public static final int MODE_DISTORT= 0;
44
   public static final int MODE_DEFORM = 1;
45
   public static final int MODE_SHEAR  = 2;
46
    
47 44
   private static final int NUM_VECTORS = 8;
48 45
   private static final int NUMLINES = 10;
49 46
   private static final int NUMFRAMES =10;
......
52 49
   private DistortedBitmap fps;
53 50
   private DistortedBitmap stretch;
54 51
   private Static3D touchPoint;
55
   private static Dynamic3D mReleasedDynamic;
56
   private static Dynamic3D mMovingDynamic;
57
   private static Static3D[] v;
52

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

  
58 63
   private static Static4D dr;
59 64
   private Canvas fpsCanvas;
60 65
   private Bitmap fpsBitmap, stretchBitmap;
......
65 70
   private long lastTime=0;
66 71
   private long[] durations;
67 72
   private int currDuration;
68
   private long shearID=0;
69
    
70
   private static int mMode = MODE_DISTORT;
73

  
74
   private static EffectNames mMode = EffectNames.DISTORT;
71 75
   private static boolean bitmapCreated = false;
72
    
76

  
77
   private static long mLastEffect = -1;
78

  
73 79
///////////////////////////////////////////////////////////////////////////////////////////////////
74 80

  
75 81
   public DeformRenderer(GLSurfaceView view) 
......
77 83
      mView = view;
78 84
      
79 85
      dr = new Static4D(0,0,0,0);
80
     
81
      mReleasedDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
82
      mReleasedDynamic.setMode(Dynamic3D.MODE_PATH);
83 86

  
84
      mMovingDynamic = new Dynamic3D(0,0.5f);
85
      mMovingDynamic.setMode(Dynamic3D.MODE_PATH);
87
      durations = new long[NUMFRAMES+1];
88
      currDuration = 0;
89

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

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

  
87
      v = new Static3D[NUM_VECTORS];
101
      vDistort = new Static3D[NUM_VECTORS];
88 102

  
89 103
      for(int i=0; i<NUM_VECTORS; i++)
90 104
        {
91
        v[i] = new Static3D(0,0,0);
92
        mReleasedDynamic.add(v[i]);
105
        vDistort[i] = new Static3D(0,0,0);
106
        mReleasedDistortDynamic.add(vDistort[i]);
93 107
        }
94 108

  
95
      mMovingDynamic.add(v[0]);
109
      mMovingDistortDynamic.add(vDistort[0]);
96 110

  
97
      durations = new long[NUMFRAMES+1];
98
      currDuration = 0;
99
      
100
      for(int i=0; i<NUMFRAMES+1; i++)
111
      // Deform
112
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
113
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
114
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
115
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
116

  
117
      vDeform = new Static3D[NUM_VECTORS];
118

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

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

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

  
133
      vShear = new Static3D[NUM_VECTORS];
134

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

  
141
      mMovingShearDynamic.add(vShear[0]);
104 142
      }
105 143

  
106 144
///////////////////////////////////////////////////////////////////////////////////////////////////
107 145

  
108
   public static void setMode(int mode)
146
   public static void setMode(EffectNames mode)
109 147
      {
110 148
      mMode = mode;  
111 149
      }
......
166 204
        {
167 205
        stretch.abortEffects(EffectTypes.VERTEX);
168 206
        stretch.abortEffects(EffectTypes.FRAGMENT);
169
        stretch.abortEffect(shearID);
207
        stretch.abortEffects(EffectNames.SHEAR);
170 208
        }
171 209
      }
172 210

  
......
226 264

  
227 265
    public void down(int x, int y)
228 266
      {
229
      stretch.abortEffects(EffectTypes.VERTEX);
230
      stretch.abortEffects(EffectTypes.FRAGMENT);
231
      stretch.abortEffect(shearID);
232

  
233 267
      int xt = x-scrWidth/4;
234 268
      int yt = y-scrHeight/4;
235 269
      
......
239 273
      if( yt>scrHeight/2 ) yt=scrHeight/2;
240 274
      
241 275
      touchPoint.set(xt,yt,0);
242
      
243
      v[0].set(0,0,0);
244
      
276

  
245 277
      switch(mMode)
246 278
        {
247
        case MODE_DISTORT: stretch.distort( mMovingDynamic, touchPoint, dr);
248
                           break;
249
        case MODE_DEFORM : stretch.deform( mMovingDynamic, touchPoint);
250
                           break;
251
        case MODE_SHEAR  : shearID = stretch.shear(mMovingDynamic, touchPoint);
252
                           break;
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);
284
                      break;
285
        case SHEAR  : vShear[0].set(0,0,0);
286
                      mLastEffect = stretch.shear(mMovingShearDynamic, touchPoint);
287
                      break;
253 288
        }                   
254 289
      }
255 290
    
......
257 292

  
258 293
    public void move(int x, int y)
259 294
      {
260
      float fx = x;
261
      float fy = y;
262
      
263
      if( mMode==MODE_SHEAR )
295
      switch(mMode)
264 296
        {
265
        fx /= (scrWidth/2);
266
        fy /= (scrHeight/2);
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;
267 303
        }
268
      
269
      v[0].set(fx,fy);
270 304
      }
271 305
    
272 306
///////////////////////////////////////////////////////////////////////////////////////////////////
273 307

  
274 308
    public void up()
275 309
      {
276
      stretch.abortEffects(EffectTypes.VERTEX);
277
      stretch.abortEffects(EffectTypes.FRAGMENT);
278
      stretch.abortEffect(shearID);
279
      
310
      stretch.abortEffect(mLastEffect);
311

  
280 312
      float damp = -0.65f;
281
      
282
      for(int i=1; i<NUM_VECTORS-1; i++)
283
        {
284
        v[i].set( v[i-1].getX()*damp, v[i-1].getY()*damp );
285
        }
286
      v[NUM_VECTORS-1].set(0,0);
287
      
313

  
288 314
      switch(mMode)
289 315
        {
290
        case MODE_DISTORT: stretch.distort( mReleasedDynamic, touchPoint, dr);
291
                           break;
292
        case MODE_DEFORM : stretch.deform( mReleasedDynamic, touchPoint);
293
                           break;
294
        case MODE_SHEAR  : shearID = stretch.shear(mReleasedDynamic, touchPoint);
295
                           break;
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);
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;
296 337
        }      
297 338
      }
298 339

  
......
312 353
      
313 354
      lastTime = currentTime;   
314 355
      }
315
    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
    
318 356
}

Also available in: Unified diff