Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 95593730

1

    
2
package org.distorted.examples.deform;
3

    
4
import javax.microedition.khronos.egl.EGLConfig;
5
import javax.microedition.khronos.opengles.GL10;
6

    
7
import org.distorted.library.Distorted;
8
import org.distorted.library.DistortedBitmap;
9
import org.distorted.library.EffectTypes;
10
import org.distorted.library.Interpolator2D;
11
import org.distorted.library.Float3D;
12
import org.distorted.library.Float4D;
13

    
14
import android.graphics.Bitmap;
15
import android.graphics.Canvas;
16
import android.graphics.Paint;
17
import android.graphics.Paint.Style;
18
import android.opengl.GLES20;
19
import android.opengl.GLSurfaceView;
20

    
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22

    
23
class DeformRenderer implements GLSurfaceView.Renderer 
24
{
25
   public static final int MODE_DISTORT= 0;
26
   public static final int MODE_DEFORM = 1;
27
   public static final int MODE_SHEAR  = 2;
28
    
29
   private static final int NUM_VECTORS = 8;
30
   private static final int NUMLINES = 10;
31
   private static final int NUMFRAMES =10;
32

    
33
   private GLSurfaceView mView;
34
   private DistortedBitmap fps;
35
   private DistortedBitmap stretch;
36
   private Float3D touchPoint;
37
   private static Interpolator2D di;
38
   private static Float3D[] v;
39
   private static Float4D dr;
40
   private Canvas fpsCanvas;
41
   private Bitmap fpsBitmap, stretchBitmap;
42
   private int scrHeight, scrWidth;
43
   private Paint mPaint;
44
   private int fpsH, fpsW;
45
   private String fpsString = "";
46
   private long lastTime=0;
47
   private long[] durations;
48
   private int currDuration;
49
   private long shearID=0;
50
    
51
   private static int mMode = MODE_DISTORT;
52
   private static boolean bitmapCreated = false;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   public DeformRenderer(GLSurfaceView view) 
57
      { 
58
      mView = view;
59
      
60
      dr = new Float4D(0,0,0,0);
61
     
62
      di = new Interpolator2D();
63
      di.setMode(Interpolator2D.MODE_PATH);
64
      di.setCount(0.5f);
65
      di.setDuration(NUM_VECTORS*500);
66
      
67
      v = new Float3D[NUM_VECTORS];
68
      
69
      for(int i=0; i<NUM_VECTORS; i++)
70
        {
71
        v[i] = new Float3D(0,0,0);  
72
        di.add(v[i]);
73
        }
74
      
75
      durations = new long[NUMFRAMES+1];
76
      currDuration = 0;
77
      
78
      for(int i=0; i<NUMFRAMES+1; i++)
79
        {
80
        durations[i]=0;   
81
        }
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   public static void setMode(int mode)
87
      {
88
      mMode = mode;  
89
      }
90
   
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   public static void setRegionRadius(int r)
94
      {
95
      dr.set(0,0,r); 
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public static void onPause()
101
      {
102
      bitmapCreated = false;  
103
      }
104
      
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
    public void onDrawFrame(GL10 glUnused) 
108
      {
109
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
110
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
111
    
112
      long time = System.currentTimeMillis();
113
      
114
      stretch.draw(time);
115
      
116
      mPaint.setColor(0xffffffff);
117
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
118
      mPaint.setColor(0xff000000);
119
      fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
120
      
121
      fps.setBitmap(fpsBitmap);
122
      fps.draw(time);
123
      
124
      computeFPS(time);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
130
      { 
131
      scrHeight = height;
132
      scrWidth  = width;
133
      
134
      Distorted.onSurfaceChanged(width, height);
135
      
136
      if( bitmapCreated==false )
137
        {
138
        createBitmap(scrWidth/2,scrHeight/2);
139
        stretch.move(scrWidth/4,scrHeight/4,0);
140
        fps.move(5,5,0);
141
        bitmapCreated=true;
142
        }
143
      else
144
        {
145
        stretch.abortAllEffects(EffectTypes.FRAGMENT.type|EffectTypes.VERTEX.type);
146
        stretch.abortEffect(shearID);
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
153
      {  
154
      try
155
        {
156
        Distorted.onSurfaceCreated(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("DeformRenderer", ex.toString() );
161
        }
162
      }
163
    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    private void createBitmap(int w, int h)
167
      {  
168
      Canvas stretchCanvas;   
169
      
170
      mPaint = new Paint();
171
      stretch = new DistortedBitmap(w,h, 50);   
172
      stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
173
      stretchCanvas = new Canvas(stretchBitmap);
174
      
175
      fpsW = scrWidth/5;
176
      fpsH = fpsW/2;
177
        
178
      mPaint.setAntiAlias(true);
179
      mPaint.setTextAlign(Paint.Align.CENTER);
180
      mPaint.setTextSize(2*fpsH/3);
181
      mPaint.setColor(0xff008800);
182
      mPaint.setStyle(Style.FILL);
183
      stretchCanvas.drawRect(0, 0, w, h, mPaint);
184
      mPaint.setColor(0xffffffff);
185
      
186
      for(int i=0; i<=NUMLINES ; i++ )
187
        {
188
        stretchCanvas.drawRect(w*i/NUMLINES - 1,                0,  w*i/NUMLINES + 1,  h               , mPaint);
189
        stretchCanvas.drawRect(               0, h *i/NUMLINES -1,  w               ,  h*i/NUMLINES + 1, mPaint);  
190
        }
191
        
192
      touchPoint= new Float3D(0,0,0);
193
        
194
      fps = new DistortedBitmap( fpsW, fpsH, 2);
195
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
196
      fpsCanvas = new Canvas(fpsBitmap);
197
        
198
      stretch.setBitmap(stretchBitmap);
199
      fps.setBitmap(fpsBitmap);
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    public void down(int x, int y)
205
      {
206
      stretch.abortAllEffects(EffectTypes.FRAGMENT.type|EffectTypes.VERTEX.type);
207
      stretch.abortEffect(shearID);
208

    
209
      int xt = x-scrWidth/4;
210
      int yt = y-scrHeight/4;
211
      
212
      if( xt<0 ) xt=0;
213
      if( xt>scrWidth/2 ) xt=scrWidth/2;
214
      if( yt<0 ) yt=0;
215
      if( yt>scrHeight/2 ) yt=scrHeight/2;
216
      
217
      touchPoint.set(xt,yt,0);
218
      
219
      v[0].set(0,0,0);
220
      
221
      switch(mMode)
222
        {
223
        case MODE_DISTORT: stretch.distort(v[0], dr, touchPoint, 0, 0.5f);
224
                           break;
225
        case MODE_DEFORM : stretch.deform( v[0], touchPoint);
226
                           break;
227
        case MODE_SHEAR  : shearID = stretch.shear(touchPoint,v[0]);
228
                           break;
229
        }                   
230
      }
231
    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public void move(int x, int y)
235
      {
236
      float fx = x;
237
      float fy = y;
238
      
239
      if( mMode==MODE_SHEAR )
240
        {
241
        fx /= (scrWidth/2);
242
        fy /= (scrHeight/2);
243
        }
244
      
245
      v[0].set(fx,fy);
246
      }
247
    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
    public void up()
251
      {
252
      stretch.abortAllEffects(EffectTypes.FRAGMENT.type|EffectTypes.VERTEX.type);
253
      stretch.abortEffect(shearID);
254
      
255
      float damp = -0.65f;
256
      
257
      for(int i=1; i<NUM_VECTORS-1; i++)
258
        {
259
        v[i].set( v[i-1].getX()*damp, v[i-1].getY()*damp );
260
        }
261
      v[NUM_VECTORS-1].set(0,0);
262
      
263
      switch(mMode)
264
        {
265
        case MODE_DISTORT: stretch.distort(di, dr, touchPoint);
266
                           break;
267
        case MODE_DEFORM : stretch.deform( di, touchPoint);
268
                           break;
269
        case MODE_SHEAR  : shearID = stretch.shear(touchPoint,di); 
270
                           break;
271
        }      
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    private void computeFPS(long currentTime)
277
      {
278
      if( lastTime!=0 )
279
        {
280
        currDuration++;
281
        if( currDuration>=NUMFRAMES ) currDuration = 0;  
282
        durations[NUMFRAMES] += ((currentTime-lastTime)-durations[currDuration]);
283
        durations[currDuration] = currentTime-lastTime;
284

    
285
        fpsString = "" + ((int)(10000.0f*NUMFRAMES/durations[NUMFRAMES]))/10.0f;
286
        }
287
      
288
      lastTime = currentTime;   
289
      }
290
    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
    
293
}
(2-2/3)