Project

General

Profile

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

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

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

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

    
109
      stretchEffects = new DistortedEffects();
110
      stretchEffects.scale(0.5f);
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
     fpsEffects.abortAllEffects();
209
     fpsEffects.move(  new Static3D( -0.5f + (fpsW/2 + 5.0f)/width, -0.5f + (fpsH/2 + 5.0f)/height,0.0f) );
210
     fpsEffects.scale( new Static3D( (float)fpsW/width, (float)fpsH/height, 1.0f) );
211

    
212
     mRegion.set3(mRadius*scrWidth);
213

    
214
     Canvas stretchCanvas;
215
     int w=width/2;
216
     int h=height/2;
217

    
218
     stretchMesh = new MeshFlat(50,50*h/w);
219
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
220
     stretchCanvas = new Canvas(stretchBitmap);
221

    
222
     mPaint.setColor(0xff008800);
223
     mPaint.setStyle(Style.FILL);
224
     stretchCanvas.drawRect(0, 0, w, h, mPaint);
225
     mPaint.setColor(0xffffffff);
226

    
227
     for(int i=0; i<=NUM_LINES ; i++ )
228
       {
229
       stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , mPaint);
230
       stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, mPaint);
231
       }
232

    
233
     touchPoint= new Static3D(0,0,0);
234

    
235
     if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
236
     stretchTexture.setTexture(stretchBitmap);
237

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

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

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

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

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

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

    
308
   void up()
309
     {
310
     stretchEffects.abortEffect(mLastEffect);
311

    
312
     float damp = -0.65f;
313

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

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

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