Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 6637d0f2

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.DistortedScreen;
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.GLES30;
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 DistortedScreen 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 DistortedScreen();
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
     GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
184

    
185
     mPaint.setColor(0xffffffff);
186
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
187
     mPaint.setColor(0xff000000);
188
     fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
189
     fpsTexture.setTexture(fpsBitmap);
190

    
191
     long time = System.currentTimeMillis();
192

    
193
     mScreen.render(time);
194
     computeFPS(time);
195
     }
196

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

    
204
     mRegion.set3(mRadius*scrWidth);
205

    
206
     if( !bitmapCreated )
207
       {
208
       createBitmap(scrWidth/2,scrHeight/2);
209
       stretchEffects.abortAllEffects();
210
       fpsEffects.abortAllEffects();
211
       stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
212
       fpsEffects.move( new Static3D(5,5,0) );
213
       bitmapCreated=true;
214

    
215
       mScreen.detachAll();
216
       mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
217
       mScreen.attach(fpsTexture,fpsEffects,fpsMesh);
218
       }
219
     else
220
       {
221
       stretchEffects.abortEffects(EffectTypes.VERTEX);
222
       stretchEffects.abortEffects(EffectTypes.FRAGMENT);
223
       stretchEffects.abortEffects(EffectNames.SHEAR);
224
       }
225

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

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

    
235
     DistortedEffects.enableEffect(EffectNames.DISTORT);
236
     DistortedEffects.enableEffect(EffectNames.DEFORM);
237

    
238
     try
239
       {
240
       Distorted.onCreate(mView.getContext());
241
       }
242
     catch(Exception ex)
243
       {
244
       android.util.Log.e("DeformRenderer", ex.toString() );
245
       }
246
     }
247
    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

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

    
279
     fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
280
     fpsCanvas = new Canvas(fpsBitmap);
281
        
282
     stretchTexture.setTexture(stretchBitmap);
283
     fpsTexture.setTexture(fpsBitmap);
284
     }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

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

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

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

    
331
   void up()
332
     {
333
     stretchEffects.abortEffect(mLastEffect);
334

    
335
     float damp = -0.65f;
336

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
   private void computeFPS(long currentTime)
366
     {
367
     if( lastTime!=0 )
368
       {
369
       currDuration++;
370
       if( currDuration>=NUM_FRAMES ) currDuration = 0;
371
       durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
372
       durations[currDuration] = currentTime-lastTime;
373

    
374
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
375
       }
376
      
377
     lastTime = currentTime;
378
     }
379
   }
(2-2/3)