Project

General

Profile

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

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

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

    
98
      mRegion = new Static4D(0,0,50,50);
99

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

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

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

    
111
      vDistort = new Static3D[NUM_VECTORS];
112

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

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

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

    
127
      vDeform = new Static3D[NUM_VECTORS];
128

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

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

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

    
143
      vShear = new Static3D[NUM_VECTORS];
144

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

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

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

    
221
     mScreen.resize(width, height);
222
     }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
    
226
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
227
     {
228
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
229

    
230
     try
231
       {
232
       Distorted.onCreate(mView.getContext());
233
       }
234
     catch(Exception ex)
235
       {
236
       android.util.Log.e("DeformRenderer", ex.toString() );
237
       }
238
     }
239
    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

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

    
271
     fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
272
     fpsCanvas = new Canvas(fpsBitmap);
273
        
274
     stretchTexture.setTexture(stretchBitmap);
275
     fpsTexture.setTexture(fpsBitmap);
276
     }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

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

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

    
323
   void up()
324
     {
325
     stretchEffects.abortEffect(mLastEffect);
326

    
327
     float damp = -0.65f;
328

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
   private void computeFPS(long currentTime)
358
     {
359
     if( lastTime!=0 )
360
       {
361
       currDuration++;
362
       if( currDuration>=NUM_FRAMES ) currDuration = 0;
363
       durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
364
       durations[currDuration] = currentTime-lastTime;
365

    
366
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
367
       }
368
      
369
     lastTime = currentTime;
370
     }
371
   }
(2-2/3)