Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 629120e4

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.type.Dynamic3D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

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

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

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

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

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

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

    
80
   private EffectNames mMode = EffectNames.DISTORT;
81
   private long mLastEffect = -1;
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   DeformRenderer(GLSurfaceView view)
86
      { 
87
      mView = view;
88

    
89
      fpsW = 120;
90
      fpsH =  70;
91

    
92
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
93
      fpsMesh = new MeshFlat(1,1);
94
      fpsTexture = new DistortedTexture(fpsW,fpsH);
95
      fpsTexture.setTexture(fpsBitmap);
96
      fpsCanvas = new Canvas(fpsBitmap);
97
      fpsEffects = new DistortedEffects();
98
      fpsEffects.move( new Static3D(5,5,0) );
99

    
100
      mPaint = new Paint();
101
      mPaint.setAntiAlias(true);
102
      mPaint.setTextAlign(Paint.Align.CENTER);
103
      mPaint.setTextSize(0.7f*fpsH);
104

    
105
      stretchEffects = new DistortedEffects();
106

    
107
      mRegion = new Static4D(0,0,0,0);
108

    
109
      durations = new long[NUM_FRAMES+1];
110
      currDuration = 0;
111

    
112
      for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
113

    
114
      // DISTORT
115
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
116
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
117
      mMovingDistortDynamic = new Dynamic3D(0,0.5f);
118
      mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
119

    
120
      vDistort = new Static3D[NUM_VECTORS];
121

    
122
      for(int i=0; i<NUM_VECTORS; i++)
123
        {
124
        vDistort[i] = new Static3D(0,0,0);
125
        mReleasedDistortDynamic.add(vDistort[i]);
126
        }
127

    
128
      mMovingDistortDynamic.add(vDistort[0]);
129

    
130
      // Deform
131
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
132
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
133
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
134
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
135

    
136
      vDeform = new Static3D[NUM_VECTORS];
137

    
138
      for(int i=0; i<NUM_VECTORS; i++)
139
        {
140
        vDeform[i] = new Static3D(0,0,0);
141
        mReleasedDeformDynamic.add(vDeform[i]);
142
        }
143

    
144
      mMovingDeformDynamic.add(vDeform[0]);
145

    
146
      // Shear
147
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
148
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
149
      mMovingShearDynamic = new Dynamic3D(0,0.5f);
150
      mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
151

    
152
      vShear = new Static3D[NUM_VECTORS];
153

    
154
      for(int i=0; i<NUM_VECTORS; i++)
155
        {
156
        vShear[i] = new Static3D(0,0,0);
157
        mReleasedShearDynamic.add(vShear[i]);
158
        }
159

    
160
      mMovingShearDynamic.add(vShear[0]);
161

    
162
      mScreen = new DistortedScreen();
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
   void setMode(EffectNames mode)
168
      {
169
      mMode = mode;  
170
      }
171
   
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
   void setRegionRadius(int r)
175
      {
176
      mRadius = ( r==100 ? 100.0f : r/100.0f);
177
      mRegion.set3(mRadius*scrWidth);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
   
182
   public void onDrawFrame(GL10 glUnused)
183
     {
184
     mPaint.setColor(0xffffffff);
185
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
186
     mPaint.setColor(0xff000000);
187
     fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
188
     fpsTexture.setTexture(fpsBitmap);
189

    
190
     long time = System.currentTimeMillis();
191

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

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

    
203
     mRegion.set3(mRadius*scrWidth);
204

    
205
     Canvas stretchCanvas;
206
     int w=width/2;
207
     int h=height/2;
208

    
209
     stretchMesh = new MeshFlat(50,50*h/w);
210
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
211
     stretchCanvas = new Canvas(stretchBitmap);
212

    
213
     mPaint.setColor(0xff008800);
214
     mPaint.setStyle(Style.FILL);
215
     stretchCanvas.drawRect(0, 0, w, h, mPaint);
216
     mPaint.setColor(0xffffffff);
217

    
218
     for(int i=0; i<=NUM_LINES ; i++ )
219
       {
220
       stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , mPaint);
221
       stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, mPaint);
222
       }
223

    
224
     touchPoint= new Static3D(0,0,0);
225

    
226
     if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
227
     stretchTexture.setTexture(stretchBitmap);
228

    
229
     stretchEffects.abortAllEffects();
230
     stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
231

    
232
     mScreen.detachAll();
233
     mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
234
     mScreen.attach(fpsTexture,fpsEffects,fpsMesh);
235

    
236
     mScreen.resize(width, height);
237
     }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
    
241
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
242
     {
243
     DistortedEffects.enableEffect(EffectNames.DISTORT);
244
     DistortedEffects.enableEffect(EffectNames.DEFORM);
245

    
246
     try
247
       {
248
       Distorted.onCreate(mView.getContext());
249
       }
250
     catch(Exception ex)
251
       {
252
       android.util.Log.e("DeformRenderer", ex.toString() );
253
       }
254
     }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
   void down(int x, int y)
259
     {
260
     int xt = x-scrWidth/4;
261
     int yt = y-scrHeight/4;
262
      
263
     if( xt<0 ) xt=0;
264
     if( xt>scrWidth/2 ) xt=scrWidth/2;
265
     if( yt<0 ) yt=0;
266
     if( yt>scrHeight/2 ) yt=scrHeight/2;
267
      
268
     touchPoint.set(xt,yt,0);
269

    
270
     switch(mMode)
271
       {
272
       case DISTORT: vDistort[0].set(0,0,0);
273
                     mLastEffect = stretchEffects.distort( mMovingDistortDynamic, touchPoint, mRegion);
274
                     break;
275
       case DEFORM : vDeform[0].set(0,0,0);
276
                     mLastEffect = stretchEffects.deform( mMovingDeformDynamic, touchPoint, mRegion);
277
                     break;
278
       case SHEAR  : vShear[0].set(0,0,0);
279
                     mLastEffect = stretchEffects.shear(mMovingShearDynamic, touchPoint);
280
                     break;
281
       }
282
     }
283
    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
   void move(int x, int y)
287
     {
288
     switch(mMode)
289
       {
290
       case DISTORT: vDistort[0].set(x,y);
291
                     break;
292
       case DEFORM:  vDeform[0].set(x,y);
293
                     break;
294
       case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
295
                     break;
296
       }
297
     }
298
    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
   void up()
302
     {
303
     stretchEffects.abortEffect(mLastEffect);
304

    
305
     float damp = -0.65f;
306

    
307
     switch(mMode)
308
       {
309
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
310
                       {
311
                       vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
312
                       }
313
                     vDistort[NUM_VECTORS-1].set(0,0);
314
                     stretchEffects.distort( mReleasedDistortDynamic, touchPoint, mRegion);
315
                     break;
316
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
317
                       {
318
                       vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
319
                       }
320
                     vDeform[NUM_VECTORS-1].set(0,0);
321
                     stretchEffects.deform( mReleasedDeformDynamic, touchPoint, mRegion);
322
                     break;
323
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
324
                       {
325
                       vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
326
                       }
327
                     vShear[NUM_VECTORS-1].set(0,0);
328
                     stretchEffects.shear(mReleasedShearDynamic, touchPoint);
329
                     break;
330
       }
331
     }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
   private void computeFPS(long currentTime)
336
     {
337
     if( lastTime!=0 )
338
       {
339
       currDuration++;
340
       if( currDuration>=NUM_FRAMES ) currDuration = 0;
341
       durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
342
       durations[currDuration] = currentTime-lastTime;
343

    
344
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
345
       }
346
      
347
     lastTime = currentTime;
348
     }
349
   }
(2-2/3)