Project

General

Profile

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

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

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.DistortedEffects;
27
import org.distorted.library.DistortedFramebuffer;
28
import org.distorted.library.DistortedTexture;
29
import org.distorted.library.MeshObject;
30
import org.distorted.library.MeshFlat;
31

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
53
   private GLSurfaceView mView;
54
   private DistortedTexture fpsTexture, stretchTexture;
55
   private DistortedEffects fpsEffects, stretchEffects;
56
   private MeshObject fpsMesh, stretchMesh;
57
   private DistortedFramebuffer mScreen;
58
   private Static3D touchPoint;
59

    
60
   private Dynamic3D mReleasedDistortDynamic;
61
   private Dynamic3D mMovingDistortDynamic;
62
   private Static3D[] vDistort;
63
   private Dynamic3D mReleasedDeformDynamic;
64
   private Dynamic3D mMovingDeformDynamic;
65
   private Static3D[] vDeform;
66
   private Dynamic3D mReleasedShearDynamic;
67
   private Dynamic3D mMovingShearDynamic;
68
   private Static3D[] vShear;
69

    
70
   private Static4D mRegion;
71
   private Canvas fpsCanvas;
72
   private Bitmap fpsBitmap;
73
   private int scrHeight, scrWidth;
74
   private Paint mPaint;
75
   private int fpsH, fpsW;
76
   private String fpsString = "";
77
   private long lastTime=0;
78
   private long[] durations;
79
   private int currDuration;
80
   private float mRadius;
81

    
82
   private EffectNames mMode = EffectNames.DISTORT;
83
   private boolean bitmapCreated = false;
84
   private long mLastEffect = -1;
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
   DeformRenderer(GLSurfaceView view)
89
      { 
90
      mView = view;
91

    
92
      mPaint = new Paint();
93
      mPaint.setAntiAlias(true);
94
      mPaint.setTextAlign(Paint.Align.CENTER);
95

    
96
      fpsEffects = new DistortedEffects();
97
      stretchEffects = new DistortedEffects();
98

    
99
      mRegion = new Static4D(0,0,0,0);
100

    
101
      durations = new long[NUM_FRAMES+1];
102
      currDuration = 0;
103

    
104
      for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
105

    
106
      // DISTORT
107
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
108
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
109
      mMovingDistortDynamic = new Dynamic3D(0,0.5f);
110
      mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
111

    
112
      vDistort = new Static3D[NUM_VECTORS];
113

    
114
      for(int i=0; i<NUM_VECTORS; i++)
115
        {
116
        vDistort[i] = new Static3D(0,0,0);
117
        mReleasedDistortDynamic.add(vDistort[i]);
118
        }
119

    
120
      mMovingDistortDynamic.add(vDistort[0]);
121

    
122
      // Deform
123
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
124
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
125
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
126
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
127

    
128
      vDeform = new Static3D[NUM_VECTORS];
129

    
130
      for(int i=0; i<NUM_VECTORS; i++)
131
        {
132
        vDeform[i] = new Static3D(0,0,0);
133
        mReleasedDeformDynamic.add(vDeform[i]);
134
        }
135

    
136
      mMovingDeformDynamic.add(vDeform[0]);
137

    
138
      // Shear
139
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
140
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
141
      mMovingShearDynamic = new Dynamic3D(0,0.5f);
142
      mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
143

    
144
      vShear = new Static3D[NUM_VECTORS];
145

    
146
      for(int i=0; i<NUM_VECTORS; i++)
147
        {
148
        vShear[i] = new Static3D(0,0,0);
149
        mReleasedShearDynamic.add(vShear[i]);
150
        }
151

    
152
      mMovingShearDynamic.add(vShear[0]);
153

    
154
      mScreen = new DistortedFramebuffer(0);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
   void setMode(EffectNames mode)
160
      {
161
      mMode = mode;  
162
      }
163
   
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   void setRegionRadius(int r)
167
      {
168
      mRadius = ( r==100 ? 100.0f : r/100.0f);
169
      mRegion.set3(mRadius*scrWidth);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
   public void onPause()
175
      {
176
      bitmapCreated = false;
177
      }
178
      
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
   
181
   public void onDrawFrame(GL10 glUnused)
182
     {
183
     GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
184
    
185
     long time = System.currentTimeMillis();
186
      
187
     mScreen.renderTo(stretchTexture, stretchMesh,stretchEffects,time);
188
      
189
     mPaint.setColor(0xffffffff);
190
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
191
     mPaint.setColor(0xff000000);
192
     fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
193
      
194
     fpsTexture.setTexture(fpsBitmap);
195
     mScreen.renderTo(fpsTexture, fpsMesh,fpsEffects,time);
196
      
197
     computeFPS(time);
198
     }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
    
202
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
203
     {
204
     scrHeight = height;
205
     scrWidth  = width;
206

    
207
     mRegion.set3(mRadius*scrWidth);
208

    
209
     if( !bitmapCreated )
210
       {
211
       createBitmap(scrWidth/2,scrHeight/2);
212
       stretchEffects.abortAllEffects();
213
       fpsEffects.abortAllEffects();
214
       stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
215
       fpsEffects.move( new Static3D(5,5,0) );
216
       bitmapCreated=true;
217
       }
218
     else
219
       {
220
       stretchEffects.abortEffects(EffectTypes.VERTEX);
221
       stretchEffects.abortEffects(EffectTypes.FRAGMENT);
222
       stretchEffects.abortEffects(EffectNames.SHEAR);
223
       }
224

    
225
     mScreen.resize(width, height);
226
     }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
    
230
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
231
     {
232
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
233

    
234
     try
235
       {
236
       Distorted.onCreate(mView.getContext());
237
       }
238
     catch(Exception ex)
239
       {
240
       android.util.Log.e("DeformRenderer", ex.toString() );
241
       }
242
     }
243
    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
   private void createBitmap(int w, int h)
247
     {
248
     Canvas stretchCanvas;
249
      
250
     stretchTexture = new DistortedTexture(w,h);
251
     stretchMesh = new MeshFlat(50,50*h/w);
252
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
253
     stretchCanvas = new Canvas(stretchBitmap);
254
      
255
     fpsW = scrWidth/5;
256
     fpsH = fpsW/2;
257

    
258
     mPaint.setTextSize(2*fpsH/3);
259
     mPaint.setColor(0xff008800);
260
     mPaint.setStyle(Style.FILL);
261
     stretchCanvas.drawRect(0, 0, w, h, mPaint);
262
     mPaint.setColor(0xffffffff);
263
      
264
     for(int i=0; i<=NUM_LINES ; i++ )
265
       {
266
       stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , mPaint);
267
       stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, mPaint);
268
       }
269
        
270
     touchPoint= new Static3D(0,0,0);
271
        
272
     fpsTexture = new DistortedTexture(fpsW,fpsH);
273
     fpsMesh = new MeshFlat(1,1);
274

    
275
     fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
276
     fpsCanvas = new Canvas(fpsBitmap);
277
        
278
     stretchTexture.setTexture(stretchBitmap);
279
     fpsTexture.setTexture(fpsBitmap);
280
     }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
   void down(int x, int y)
285
     {
286
     int xt = x-scrWidth/4;
287
     int yt = y-scrHeight/4;
288
      
289
     if( xt<0 ) xt=0;
290
     if( xt>scrWidth/2 ) xt=scrWidth/2;
291
     if( yt<0 ) yt=0;
292
     if( yt>scrHeight/2 ) yt=scrHeight/2;
293
      
294
     touchPoint.set(xt,yt,0);
295

    
296
     switch(mMode)
297
       {
298
       case DISTORT: vDistort[0].set(0,0,0);
299
                     mLastEffect = stretchEffects.distort( mMovingDistortDynamic, touchPoint, mRegion);
300
                     break;
301
       case DEFORM : vDeform[0].set(0,0,0);
302
                     mLastEffect = stretchEffects.deform( mMovingDeformDynamic, touchPoint, mRegion);
303
                     break;
304
       case SHEAR  : vShear[0].set(0,0,0);
305
                     mLastEffect = stretchEffects.shear(mMovingShearDynamic, touchPoint);
306
                     break;
307
       }
308
     }
309
    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
   void move(int x, int y)
313
     {
314
     switch(mMode)
315
       {
316
       case DISTORT: vDistort[0].set(x,y);
317
                     break;
318
       case DEFORM:  vDeform[0].set(x,y);
319
                     break;
320
       case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
321
                     break;
322
       }
323
     }
324
    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
   void up()
328
     {
329
     stretchEffects.abortEffect(mLastEffect);
330

    
331
     float damp = -0.65f;
332

    
333
     switch(mMode)
334
       {
335
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
336
                       {
337
                       vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
338
                       }
339
                     vDistort[NUM_VECTORS-1].set(0,0);
340
                     stretchEffects.distort( mReleasedDistortDynamic, touchPoint, mRegion);
341
                     break;
342
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
343
                       {
344
                       vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
345
                       }
346
                     vDeform[NUM_VECTORS-1].set(0,0);
347
                     stretchEffects.deform( mReleasedDeformDynamic, touchPoint, mRegion);
348
                     break;
349
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
350
                       {
351
                       vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
352
                       }
353
                     vShear[NUM_VECTORS-1].set(0,0);
354
                     stretchEffects.shear(mReleasedShearDynamic, touchPoint);
355
                     break;
356
       }
357
     }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
   private void computeFPS(long currentTime)
362
     {
363
     if( lastTime!=0 )
364
       {
365
       currDuration++;
366
       if( currDuration>=NUM_FRAMES ) currDuration = 0;
367
       durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
368
       durations[currDuration] = currentTime-lastTime;
369

    
370
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
371
       }
372
      
373
     lastTime = currentTime;
374
     }
375
   }
(2-2/3)