Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 7abd1d00

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 NUMLINES = 10;
46
   private static final int NUMFRAMES =10;
47

    
48
   private GLSurfaceView mView;
49
   private DistortedBitmap fps;
50
   private DistortedBitmap stretch;
51
   private Static3D touchPoint;
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

    
63
   private static Static4D dr;
64
   private Canvas fpsCanvas;
65
   private Bitmap fpsBitmap, stretchBitmap;
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 static EffectNames mMode = EffectNames.DISTORT;
75
   private static boolean bitmapCreated = false;
76

    
77
   private static long mLastEffect = -1;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
   public DeformRenderer(GLSurfaceView view) 
82
      { 
83
      mView = view;
84
      
85
      dr = new Static4D(0,0,0,0);
86

    
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);
100

    
101
      vDistort = new Static3D[NUM_VECTORS];
102

    
103
      for(int i=0; i<NUM_VECTORS; i++)
104
        {
105
        vDistort[i] = new Static3D(0,0,0);
106
        mReleasedDistortDynamic.add(vDistort[i]);
107
        }
108

    
109
      mMovingDistortDynamic.add(vDistort[0]);
110

    
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++)
120
        {
121
        vDeform[i] = new Static3D(0,0,0);
122
        mReleasedDeformDynamic.add(vDeform[i]);
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]);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
   public static void setMode(EffectNames mode)
147
      {
148
      mMode = mode;  
149
      }
150
   
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
   public static void setRegionRadius(int r)
154
      {
155
      dr.set(0,0,r); 
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    public static void onPause()
161
      {
162
      bitmapCreated = false;  
163
      }
164
      
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
   
167
    public void onDrawFrame(GL10 glUnused) 
168
      {
169
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
170
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
171
    
172
      long time = System.currentTimeMillis();
173
      
174
      stretch.draw(time);
175
      
176
      mPaint.setColor(0xffffffff);
177
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
178
      mPaint.setColor(0xff000000);
179
      fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
180
      
181
      fps.setBitmap(fpsBitmap);
182
      fps.draw(time);
183
      
184
      computeFPS(time);
185
      }
186

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

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

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
277
      switch(mMode)
278
        {
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;
288
        }                   
289
      }
290
    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public void move(int x, int y)
294
      {
295
      switch(mMode)
296
        {
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;
303
        }
304
      }
305
    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

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

    
312
      float damp = -0.65f;
313

    
314
      switch(mMode)
315
        {
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;
337
        }      
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

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