Project

General

Profile

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

examples / src / main / java / org / distortedandroid / examples / deform / DeformRenderer.java @ 427ab7bf

1

    
2
package org.distortedandroid.examples.deform;
3

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

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

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

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21

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

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

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

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

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

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

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

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

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public void down(int x, int y)
204
      {
205
      stretch.abortAllEffects(Distorted.TYPE_FRAG|Distorted.TYPE_VERT);  
206
      stretch.abortEffect(shearID);
207

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

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

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

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