Project

General

Profile

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

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

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.DistortedScreen;
29
import org.distorted.library.DistortedTexture;
30
import org.distorted.library.MeshObject;
31
import org.distorted.library.MeshFlat;
32

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

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

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

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

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

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

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

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

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

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

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

    
113
      vDistort = new Static3D[NUM_VECTORS];
114

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

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

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

    
129
      vDeform = new Static3D[NUM_VECTORS];
130

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

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

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

    
145
      vShear = new Static3D[NUM_VECTORS];
146

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

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

    
155
      mScreen = new DistortedScreen();
156
      }
157

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

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

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

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

    
208
     mRegion.set3(mRadius*scrWidth);
209

    
210
     if( !bitmapCreated )
211
       {
212
       createBitmap(scrWidth/2,scrHeight/2);
213
       stretchEffects.abortAllEffects();
214
       fpsEffects.abortAllEffects();
215
       stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
216
       fpsEffects.move( new Static3D(5,5,0) );
217
       bitmapCreated=true;
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
     try
236
       {
237
       Distorted.onCreate(mView.getContext());
238
       }
239
     catch(Exception ex)
240
       {
241
       android.util.Log.e("DeformRenderer", ex.toString() );
242
       }
243
     }
244
    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

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

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

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

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

    
332
     float damp = -0.65f;
333

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

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