Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 7589635e

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.DistortedBitmap;
27
import org.distorted.library.EffectTypes;
28
import org.distorted.library.type.Dynamic3D;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31

    
32
import android.graphics.Bitmap;
33
import android.graphics.Canvas;
34
import android.graphics.Paint;
35
import android.graphics.Paint.Style;
36
import android.opengl.GLES20;
37
import android.opengl.GLSurfaceView;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
class DeformRenderer implements GLSurfaceView.Renderer 
42
{
43
   public static final int MODE_DISTORT= 0;
44
   public static final int MODE_DEFORM = 1;
45
   public static final int MODE_SHEAR  = 2;
46
    
47
   private static final int NUM_VECTORS = 8;
48
   private static final int NUMLINES = 10;
49
   private static final int NUMFRAMES =10;
50

    
51
   private GLSurfaceView mView;
52
   private DistortedBitmap fps;
53
   private DistortedBitmap stretch;
54
   private Static3D touchPoint;
55
   private static Dynamic3D mReleasedDynamic;
56
   private static Dynamic3D mMovingDynamic;
57
   private static Static3D[] v;
58
   private static Static4D dr;
59
   private Canvas fpsCanvas;
60
   private Bitmap fpsBitmap, stretchBitmap;
61
   private int scrHeight, scrWidth;
62
   private Paint mPaint;
63
   private int fpsH, fpsW;
64
   private String fpsString = "";
65
   private long lastTime=0;
66
   private long[] durations;
67
   private int currDuration;
68
   private long shearID=0;
69
    
70
   private static int mMode = MODE_DISTORT;
71
   private static boolean bitmapCreated = false;
72
    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
   public DeformRenderer(GLSurfaceView view) 
76
      { 
77
      mView = view;
78
      
79
      dr = new Static4D(0,0,0,0);
80
     
81
      mReleasedDynamic = new Dynamic3D();
82
      mReleasedDynamic.setMode(Dynamic3D.MODE_PATH);
83
      mReleasedDynamic.setCount(0.5f);
84
      mReleasedDynamic.setDuration(NUM_VECTORS*500);
85

    
86
      mMovingDynamic = new Dynamic3D();
87
      mMovingDynamic.setMode(Dynamic3D.MODE_PATH);
88
      mMovingDynamic.setCount(0.5f);
89
      mMovingDynamic.setDuration(0);
90

    
91
      v = new Static3D[NUM_VECTORS];
92

    
93
      for(int i=0; i<NUM_VECTORS; i++)
94
        {
95
        v[i] = new Static3D(0,0,0);
96
        mReleasedDynamic.add(v[i]);
97
        }
98

    
99
      mMovingDynamic.add(v[0]);
100

    
101
      durations = new long[NUMFRAMES+1];
102
      currDuration = 0;
103
      
104
      for(int i=0; i<NUMFRAMES+1; i++)
105
        {
106
        durations[i]=0;   
107
        }
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   public static void setMode(int mode)
113
      {
114
      mMode = mode;  
115
      }
116
   
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
   public static void setRegionRadius(int r)
120
      {
121
      dr.set(0,0,r); 
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    public static void onPause()
127
      {
128
      bitmapCreated = false;  
129
      }
130
      
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
   
133
    public void onDrawFrame(GL10 glUnused) 
134
      {
135
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
136
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
137
    
138
      long time = System.currentTimeMillis();
139
      
140
      stretch.draw(time);
141
      
142
      mPaint.setColor(0xffffffff);
143
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
144
      mPaint.setColor(0xff000000);
145
      fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
146
      
147
      fps.setBitmap(fpsBitmap);
148
      fps.draw(time);
149
      
150
      computeFPS(time);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
156
      { 
157
      scrHeight = height;
158
      scrWidth  = width;
159
      
160
      Distorted.onSurfaceChanged(width, height);
161
      
162
      if( bitmapCreated==false )
163
        {
164
        createBitmap(scrWidth/2,scrHeight/2);
165
        stretch.move( new Static3D(scrWidth/4,scrHeight/4,0) );
166
        fps.move( new Static3D(5,5,0) );
167
        bitmapCreated=true;
168
        }
169
      else
170
        {
171
        stretch.abortEffects(EffectTypes.VERTEX);
172
        stretch.abortEffects(EffectTypes.FRAGMENT);
173
        stretch.abortEffect(shearID);
174
        }
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180
      {  
181
      try
182
        {
183
        Distorted.onSurfaceCreated(mView.getContext());
184
        }
185
      catch(Exception ex)
186
        {
187
        android.util.Log.e("DeformRenderer", ex.toString() );
188
        }
189
      }
190
    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    private void createBitmap(int w, int h)
194
      {  
195
      Canvas stretchCanvas;   
196
      
197
      mPaint = new Paint();
198
      stretch = new DistortedBitmap(w,h, 50);   
199
      stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
200
      stretchCanvas = new Canvas(stretchBitmap);
201
      
202
      fpsW = scrWidth/5;
203
      fpsH = fpsW/2;
204
        
205
      mPaint.setAntiAlias(true);
206
      mPaint.setTextAlign(Paint.Align.CENTER);
207
      mPaint.setTextSize(2*fpsH/3);
208
      mPaint.setColor(0xff008800);
209
      mPaint.setStyle(Style.FILL);
210
      stretchCanvas.drawRect(0, 0, w, h, mPaint);
211
      mPaint.setColor(0xffffffff);
212
      
213
      for(int i=0; i<=NUMLINES ; i++ )
214
        {
215
        stretchCanvas.drawRect(w*i/NUMLINES - 1,                0,  w*i/NUMLINES + 1,  h               , mPaint);
216
        stretchCanvas.drawRect(               0, h *i/NUMLINES -1,  w               ,  h*i/NUMLINES + 1, mPaint);  
217
        }
218
        
219
      touchPoint= new Static3D(0,0,0);
220
        
221
      fps = new DistortedBitmap( fpsW, fpsH, 2);
222
      fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
223
      fpsCanvas = new Canvas(fpsBitmap);
224
        
225
      stretch.setBitmap(stretchBitmap);
226
      fps.setBitmap(fpsBitmap);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    public void down(int x, int y)
232
      {
233
      stretch.abortEffects(EffectTypes.VERTEX);
234
      stretch.abortEffects(EffectTypes.FRAGMENT);
235
      stretch.abortEffect(shearID);
236

    
237
      int xt = x-scrWidth/4;
238
      int yt = y-scrHeight/4;
239
      
240
      if( xt<0 ) xt=0;
241
      if( xt>scrWidth/2 ) xt=scrWidth/2;
242
      if( yt<0 ) yt=0;
243
      if( yt>scrHeight/2 ) yt=scrHeight/2;
244
      
245
      touchPoint.set(xt,yt,0);
246
      
247
      v[0].set(0,0,0);
248
      
249
      switch(mMode)
250
        {
251
        case MODE_DISTORT: stretch.distort(v[0], dr, touchPoint, 0, 0.5f);
252
                           break;
253
        case MODE_DEFORM : stretch.deform( v[0], touchPoint);
254
                           break;
255
        case MODE_SHEAR  : shearID = stretch.shear(touchPoint, mMovingDynamic);
256
                           break;
257
        }                   
258
      }
259
    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public void move(int x, int y)
263
      {
264
      float fx = x;
265
      float fy = y;
266
      
267
      if( mMode==MODE_SHEAR )
268
        {
269
        fx /= (scrWidth/2);
270
        fy /= (scrHeight/2);
271
        }
272
      
273
      v[0].set(fx,fy);
274
      }
275
    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
    public void up()
279
      {
280
      stretch.abortEffects(EffectTypes.VERTEX);
281
      stretch.abortEffects(EffectTypes.FRAGMENT);
282
      stretch.abortEffect(shearID);
283
      
284
      float damp = -0.65f;
285
      
286
      for(int i=1; i<NUM_VECTORS-1; i++)
287
        {
288
        v[i].set( v[i-1].getX()*damp, v[i-1].getY()*damp );
289
        }
290
      v[NUM_VECTORS-1].set(0,0);
291
      
292
      switch(mMode)
293
        {
294
        case MODE_DISTORT: stretch.distort( mReleasedDynamic, dr, touchPoint);
295
                           break;
296
        case MODE_DEFORM : stretch.deform( mReleasedDynamic, touchPoint);
297
                           break;
298
        case MODE_SHEAR  : shearID = stretch.shear(touchPoint,mReleasedDynamic);
299
                           break;
300
        }      
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
    private void computeFPS(long currentTime)
306
      {
307
      if( lastTime!=0 )
308
        {
309
        currDuration++;
310
        if( currDuration>=NUMFRAMES ) currDuration = 0;  
311
        durations[NUMFRAMES] += ((currentTime-lastTime)-durations[currDuration]);
312
        durations[currDuration] = currentTime-lastTime;
313

    
314
        fpsString = "" + ((int)(10000.0f*NUMFRAMES/durations[NUMFRAMES]))/10.0f;
315
        }
316
      
317
      lastTime = currentTime;   
318
      }
319
    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321
    
322
}
(2-2/3)