Project

General

Profile

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

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

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.GLES30;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

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

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

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

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

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

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

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

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

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

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

    
112
      vDistort = new Static3D[NUM_VECTORS];
113

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

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

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

    
128
      vDeform = new Static3D[NUM_VECTORS];
129

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

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

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

    
144
      vShear = new Static3D[NUM_VECTORS];
145

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

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

    
154
      mScreen = new DistortedScreen();
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
   public void onPause()
175
      {
176
      bitmapCreated = false;
177
      }
178
      
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
   
181
   public void onDrawFrame(GL10 glUnused)
182
     {
183
     GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
184

    
185
     mPaint.setColor(0xffffffff);
186
     fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
187
     mPaint.setColor(0xff000000);
188
     fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
189
     fpsTexture.setTexture(fpsBitmap);
190

    
191
     long time = System.currentTimeMillis();
192

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

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

    
204
     mRegion.set3(mRadius*scrWidth);
205

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

    
215
       mScreen.detachAll();
216
       mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
217
       mScreen.attach(fpsTexture,fpsEffects,fpsMesh);
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)