Project

General

Profile

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

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

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.abortEffects(EffectTypes.VERTEX);
146
        stretch.abortEffects(EffectTypes.FRAGMENT);
147
        stretch.abortEffect(shearID);
148
        }
149
      }
150

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

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    public void down(int x, int y)
206
      {
207
      stretch.abortEffects(EffectTypes.VERTEX);
208
      stretch.abortEffects(EffectTypes.FRAGMENT);
209
      stretch.abortEffect(shearID);
210

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

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

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

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

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

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