Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 28fe91ae

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.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

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

    
69
   private Static4D mRegion;
70
   private Canvas fpsCanvas;
71
   private Bitmap fpsBitmap;
72
   private int scrHeight, scrWidth;
73
   private Paint mPaint;
74
   private int fpsH, fpsW;
75
   private String fpsString = "";
76
   private long lastTime=0;
77
   private long[] durations;
78
   private int currDuration;
79
   private float mRadius;
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,0,0);
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 DistortedScreen();
154
      }
155

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

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
   public void onPause()
174
      {
175
      bitmapCreated = false;
176
      }
177
      
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
   
180
   public void onDrawFrame(GL10 glUnused)
181
     {
182
     mPaint.setColor(0xffffffff);
183
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
184
     mPaint.setColor(0xff000000);
185
     fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
186
     fpsTexture.setTexture(fpsBitmap);
187

    
188
     long time = System.currentTimeMillis();
189

    
190
     mScreen.render(time);
191
     computeFPS(time);
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
    
196
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
197
     {
198
     scrHeight = height;
199
     scrWidth  = width;
200

    
201
     mRegion.set3(mRadius*scrWidth);
202

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

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

    
223
     mScreen.resize(width, height);
224
     }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
     {
230
     DistortedEffects.enableEffect(EffectNames.DISTORT);
231
     DistortedEffects.enableEffect(EffectNames.DEFORM);
232

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

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

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

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

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

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

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

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

    
330
     float damp = -0.65f;
331

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

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

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