Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 630703d1

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 stretchTexture;
53
   private DistortedEffects stretchEffects;
54
   private MeshObject 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 int scrHeight, scrWidth;
70
   private float mRadius;
71

    
72
   private EffectNames mMode = EffectNames.DISTORT;
73
   private long mLastEffect = -1;
74

    
75
   // FPS
76
   private MeshObject fpsMesh;
77
   private DistortedTexture fpsTexture;
78
   private DistortedEffects fpsEffects;
79
   private Canvas fpsCanvas;
80
   private Bitmap fpsBitmap;
81
   private Paint mPaint;
82
   private int fpsH, fpsW;
83
   private String fpsString = "";
84
   private long lastTime=0;
85
   private long[] durations;
86
   private int currDuration;
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

    
94
      fpsW = 120;
95
      fpsH =  70;
96

    
97
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
98
      fpsMesh = new MeshFlat(1,1);
99
      fpsTexture = new DistortedTexture(fpsW,fpsH);
100
      fpsTexture.setTexture(fpsBitmap);
101
      fpsCanvas = new Canvas(fpsBitmap);
102
      fpsEffects = new DistortedEffects();
103
      fpsEffects.move( new Static3D(5,5,0) );
104

    
105
      mPaint = new Paint();
106
      mPaint.setAntiAlias(true);
107
      mPaint.setTextAlign(Paint.Align.CENTER);
108
      mPaint.setTextSize(0.7f*fpsH);
109

    
110
      stretchEffects = new DistortedEffects();
111

    
112
      mRegion = new Static4D(0,0,0,0);
113

    
114
      durations = new long[NUM_FRAMES+1];
115
      currDuration = 0;
116

    
117
      for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
118

    
119
      // DISTORT
120
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
121
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
122
      mMovingDistortDynamic = new Dynamic3D(0,0.5f);
123
      mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
124

    
125
      vDistort = new Static3D[NUM_VECTORS];
126

    
127
      for(int i=0; i<NUM_VECTORS; i++)
128
        {
129
        vDistort[i] = new Static3D(0,0,0);
130
        mReleasedDistortDynamic.add(vDistort[i]);
131
        }
132

    
133
      mMovingDistortDynamic.add(vDistort[0]);
134

    
135
      // Deform
136
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
137
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
138
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
139
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
140

    
141
      vDeform = new Static3D[NUM_VECTORS];
142

    
143
      for(int i=0; i<NUM_VECTORS; i++)
144
        {
145
        vDeform[i] = new Static3D(0,0,0);
146
        mReleasedDeformDynamic.add(vDeform[i]);
147
        }
148

    
149
      mMovingDeformDynamic.add(vDeform[0]);
150

    
151
      // Shear
152
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
153
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
154
      mMovingShearDynamic = new Dynamic3D(0,0.5f);
155
      mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
156

    
157
      vShear = new Static3D[NUM_VECTORS];
158

    
159
      for(int i=0; i<NUM_VECTORS; i++)
160
        {
161
        vShear[i] = new Static3D(0,0,0);
162
        mReleasedShearDynamic.add(vShear[i]);
163
        }
164

    
165
      mMovingShearDynamic.add(vShear[0]);
166

    
167
      mScreen = new DistortedScreen();
168
      }
169

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

    
172
   void setMode(EffectNames mode)
173
      {
174
      mMode = mode;  
175
      }
176
   
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
   void setRegionRadius(int r)
180
      {
181
      mRadius = ( r==100 ? 100.0f : r/100.0f);
182
      mRegion.set3(mRadius*scrWidth);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
   
187
   public void onDrawFrame(GL10 glUnused)
188
     {
189
     mPaint.setColor(0xffffffff);
190
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
191
     mPaint.setColor(0xff000000);
192
     fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
193
     fpsTexture.setTexture(fpsBitmap);
194

    
195
     long time = System.currentTimeMillis();
196

    
197
     mScreen.render(time);
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
     Canvas stretchCanvas;
211
     int w=width/2;
212
     int h=height/2;
213

    
214
     stretchMesh = new MeshFlat(50,50*h/w);
215
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
216
     stretchCanvas = new Canvas(stretchBitmap);
217

    
218
     mPaint.setColor(0xff008800);
219
     mPaint.setStyle(Style.FILL);
220
     stretchCanvas.drawRect(0, 0, w, h, mPaint);
221
     mPaint.setColor(0xffffffff);
222

    
223
     for(int i=0; i<=NUM_LINES ; i++ )
224
       {
225
       stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , mPaint);
226
       stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, mPaint);
227
       }
228

    
229
     touchPoint= new Static3D(0,0,0);
230

    
231
     if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
232
     stretchTexture.setTexture(stretchBitmap);
233

    
234
     stretchEffects.abortAllEffects();
235
     stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
236

    
237
     mScreen.detachAll();
238
     mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
239
     mScreen.attach(fpsTexture,fpsEffects,fpsMesh);
240

    
241
     mScreen.resize(width, height);
242
     }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
    
246
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
247
     {
248
     DistortedEffects.enableEffect(EffectNames.DISTORT);
249
     DistortedEffects.enableEffect(EffectNames.DEFORM);
250

    
251
     try
252
       {
253
       Distorted.onCreate(mView.getContext());
254
       }
255
     catch(Exception ex)
256
       {
257
       android.util.Log.e("DeformRenderer", ex.toString() );
258
       }
259
     }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
   void down(int x, int y)
264
     {
265
     int xt = x-scrWidth/4;
266
     int yt = y-scrHeight/4;
267
      
268
     if( xt<0 ) xt=0;
269
     if( xt>scrWidth/2 ) xt=scrWidth/2;
270
     if( yt<0 ) yt=0;
271
     if( yt>scrHeight/2 ) yt=scrHeight/2;
272
      
273
     touchPoint.set(xt,yt,0);
274

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

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

    
306
   void up()
307
     {
308
     stretchEffects.abortEffect(mLastEffect);
309

    
310
     float damp = -0.65f;
311

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

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
   private void computeFPS(long currentTime)
341
     {
342
     if( lastTime!=0 )
343
       {
344
       currDuration++;
345
       if( currDuration>=NUM_FRAMES ) currDuration = 0;
346
       durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
347
       durations[currDuration] = currentTime-lastTime;
348

    
349
       fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
350
       }
351
      
352
     lastTime = currentTime;
353
     }
354
   }
(2-2/3)