Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 10b7e588

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.DistortedObject;
27
import org.distorted.library.GridObject;
28
import org.distorted.library.GridFlat;
29

    
30
import org.distorted.library.EffectNames;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic3D;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import android.graphics.Bitmap;
37
import android.graphics.Canvas;
38
import android.graphics.Paint;
39
import android.graphics.Paint.Style;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class DeformRenderer implements GLSurfaceView.Renderer 
46
{
47
   private static final int NUM_VECTORS =  8;
48
   private static final int NUM_LINES   = 10;
49
   private static final int NUM_FRAMES  = 10;
50

    
51
   private GLSurfaceView mView;
52
   private DistortedObject fps, stretch;
53
   private GridObject fpsGrid, stretchGrid;
54
   private Static3D touchPoint;
55

    
56
   private Dynamic3D mReleasedDistortDynamic;
57
   private Dynamic3D mMovingDistortDynamic;
58
   private Static3D[] vDistort;
59
   private Dynamic3D mReleasedDeformDynamic;
60
   private Dynamic3D mMovingDeformDynamic;
61
   private Static3D[] vDeform;
62
   private Dynamic3D mReleasedShearDynamic;
63
   private Dynamic3D mMovingShearDynamic;
64
   private Static3D[] vShear;
65

    
66
   private Static4D mRegion;
67
   private Canvas fpsCanvas;
68
   private Bitmap fpsBitmap;
69
   private int scrHeight, scrWidth;
70
   private Paint mPaint;
71
   private int fpsH, fpsW;
72
   private String fpsString = "";
73
   private long lastTime=0;
74
   private long[] durations;
75
   private int currDuration;
76

    
77
   private EffectNames mMode = EffectNames.DISTORT;
78
   private boolean bitmapCreated = false;
79
   private long mLastEffect = -1;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
   DeformRenderer(GLSurfaceView view)
84
      { 
85
      mView = view;
86
      
87
      mRegion = new Static4D(0,0,0,0);
88

    
89
      durations = new long[NUM_FRAMES+1];
90
      currDuration = 0;
91

    
92
      for(int i=0; i<NUM_FRAMES+1; i++)
93
        {
94
        durations[i]=0;
95
        }
96

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

    
103
      vDistort = new Static3D[NUM_VECTORS];
104

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

    
111
      mMovingDistortDynamic.add(vDistort[0]);
112

    
113
      // Deform
114
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
115
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
116
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
117
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
118

    
119
      vDeform = new Static3D[NUM_VECTORS];
120

    
121
      for(int i=0; i<NUM_VECTORS; i++)
122
        {
123
        vDeform[i] = new Static3D(0,0,0);
124
        mReleasedDeformDynamic.add(vDeform[i]);
125
        }
126

    
127
      mMovingDeformDynamic.add(vDeform[0]);
128

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

    
135
      vShear = new Static3D[NUM_VECTORS];
136

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

    
143
      mMovingShearDynamic.add(vShear[0]);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
   void setMode(EffectNames mode)
149
      {
150
      mMode = mode;  
151
      }
152
   
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
   void setRegionRadius(int r)
156
      {
157
      mRegion.set3( r==100 ? 100*scrWidth : r*scrWidth/100.0f);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
191
      { 
192
      scrHeight = height;
193
      scrWidth  = width;
194

    
195
      setRegionRadius(50);
196

    
197
      Distorted.onSurfaceChanged(width, height);
198
      
199
      if( !bitmapCreated )
200
        {
201
        createBitmap(scrWidth/2,scrHeight/2);
202
        stretch.move( new Static3D(scrWidth/4,scrHeight/4,0) );
203
        fps.move( new Static3D(5,5,0) );
204
        bitmapCreated=true;
205
        }
206
      else
207
        {
208
        stretch.abortEffects(EffectTypes.VERTEX);
209
        stretch.abortEffects(EffectTypes.FRAGMENT);
210
        stretch.abortEffects(EffectNames.SHEAR);
211
        }
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
    
216
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
217
      {
218
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
219

    
220
      try
221
        {
222
        Distorted.onSurfaceCreated(mView.getContext());
223
        }
224
      catch(Exception ex)
225
        {
226
        android.util.Log.e("DeformRenderer", ex.toString() );
227
        }
228
      }
229
    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

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

    
264
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
265
      fpsCanvas = new Canvas(fpsBitmap);
266
        
267
      stretch.setTexture(stretchBitmap);
268
      fps.setTexture(fpsBitmap);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    void down(int x, int y)
274
      {
275
      int xt = x-scrWidth/4;
276
      int yt = y-scrHeight/4;
277
      
278
      if( xt<0 ) xt=0;
279
      if( xt>scrWidth/2 ) xt=scrWidth/2;
280
      if( yt<0 ) yt=0;
281
      if( yt>scrHeight/2 ) yt=scrHeight/2;
282
      
283
      touchPoint.set(xt,yt,0);
284

    
285
      switch(mMode)
286
        {
287
        case DISTORT: vDistort[0].set(0,0,0);
288
                      mLastEffect = stretch.distort( mMovingDistortDynamic, touchPoint, mRegion);
289
                      break;
290
        case DEFORM : vDeform[0].set(0,0,0);
291
                      mLastEffect = stretch.deform( mMovingDeformDynamic, touchPoint, mRegion);
292
                      break;
293
        case SHEAR  : vShear[0].set(0,0,0);
294
                      mLastEffect = stretch.shear(mMovingShearDynamic, touchPoint);
295
                      break;
296
        }                   
297
      }
298
    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    void move(int x, int y)
302
      {
303
      switch(mMode)
304
        {
305
        case DISTORT: vDistort[0].set(x,y);
306
                      break;
307
        case DEFORM:  vDeform[0].set(x,y);
308
                      break;
309
        case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
310
                      break;
311
        }
312
      }
313
    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
    void up()
317
      {
318
      stretch.abortEffect(mLastEffect);
319

    
320
      float damp = -0.65f;
321

    
322
      switch(mMode)
323
        {
324
        case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
325
                        {
326
                        vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
327
                        }
328
                      vDistort[NUM_VECTORS-1].set(0,0);
329
                      stretch.distort( mReleasedDistortDynamic, touchPoint, mRegion);
330
                      break;
331
        case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
332
                        {
333
                        vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
334
                        }
335
                      vDeform[NUM_VECTORS-1].set(0,0);
336
                      stretch.deform( mReleasedDeformDynamic, touchPoint, mRegion);
337
                      break;
338
        case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
339
                        {
340
                        vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
341
                        }
342
                      vShear[NUM_VECTORS-1].set(0,0);
343
                      stretch.shear(mReleasedShearDynamic, touchPoint);
344
                      break;
345
        }      
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    private void computeFPS(long currentTime)
351
      {
352
      if( lastTime!=0 )
353
        {
354
        currDuration++;
355
        if( currDuration>=NUM_FRAMES ) currDuration = 0;
356
        durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
357
        durations[currDuration] = currentTime-lastTime;
358

    
359
        fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
360
        }
361
      
362
      lastTime = currentTime;   
363
      }
364
}
(2-2/3)