Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 061449ed

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.effect.EffectName;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.effect.MatrixEffectShear;
28
import org.distorted.library.effect.VertexEffectDeform;
29
import org.distorted.library.effect.VertexEffectDistort;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.mesh.MeshRectangles;
36

    
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import android.graphics.Bitmap;
44
import android.graphics.Canvas;
45
import android.graphics.Paint;
46
import android.graphics.Paint.Style;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class DeformRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
52
   {
53
   private static final int NUM_VECTORS =  8;
54
   private static final int NUM_LINES   = 10;
55

    
56
   private GLSurfaceView mView;
57
   private DistortedTexture mTexture;
58
   private DistortedEffects mEffects;
59
   private MeshBase mMesh;
60
   private DistortedScreen mScreen;
61
   private Static3D mTouchPoint, mScale;
62

    
63
   private Static3D[] vDistort;
64
   private Static3D[] vDeform;
65
   private Static3D[] vShear;
66

    
67
   private Static4D mRegion;
68
   private int scrHeight, scrWidth;
69
   private int textureWidth, textureHeight;
70
   private float mRadius;
71

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

    
75
   private MatrixEffectShear mMovingShear, mReleasedShear;
76
   private VertexEffectDistort mMovingDistort, mReleasedDistort;
77
   private VertexEffectDeform mMovingDeform, mReleasedDeform;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
   DeformRenderer(GLSurfaceView view)
82
      { 
83
      mView = view;
84

    
85
      mTexture    = new DistortedTexture();
86
      mEffects    = new DistortedEffects();
87
      mRegion     = new Static4D(0,0,0,0);
88
      mTouchPoint = new Static3D(0,0,0);
89
      mScale      = new Static3D(1,1,1);
90

    
91
      mEffects.apply( new MatrixEffectScale(mScale) );
92

    
93
      // DISTORT
94
      Dynamic3D releasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
95
      releasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
96
      Dynamic3D movingDistortDynamic = new Dynamic3D(0,0.5f);
97
      movingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
98

    
99
      vDistort = new Static3D[NUM_VECTORS];
100

    
101
      for(int i=0; i<NUM_VECTORS; i++)
102
        {
103
        vDistort[i] = new Static3D(0,0,0);
104
        releasedDistortDynamic.add(vDistort[i]);
105
        }
106

    
107
      movingDistortDynamic.add(vDistort[0]);
108

    
109
      // Deform
110
      Dynamic3D releasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
111
      releasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
112
      Dynamic3D movingDeformDynamic = new Dynamic3D(0,0.5f);
113
      movingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
114

    
115
      vDeform = new Static3D[NUM_VECTORS];
116

    
117
      for(int i=0; i<NUM_VECTORS; i++)
118
        {
119
        vDeform[i] = new Static3D(0,0,0);
120
        releasedDeformDynamic.add(vDeform[i]);
121
        }
122

    
123
      movingDeformDynamic.add(vDeform[0]);
124

    
125
      // Shear
126
      Dynamic3D releasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
127
      releasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
128
      Dynamic3D movingShearDynamic = new Dynamic3D(0,0.5f);
129
      movingShearDynamic.setMode(Dynamic3D.MODE_PATH);
130

    
131
      vShear = new Static3D[NUM_VECTORS];
132

    
133
      for(int i=0; i<NUM_VECTORS; i++)
134
        {
135
        vShear[i] = new Static3D(0,0,0);
136
        releasedShearDynamic.add(vShear[i]);
137
        }
138

    
139
      movingShearDynamic.add(vShear[0]);
140

    
141
      mScreen = new DistortedScreen();
142
      mScreen.showFPS();
143

    
144
      Static1D deformRadius = new Static1D(0.5f);
145

    
146
      mMovingDistort   = new VertexEffectDistort( movingDistortDynamic  ,               mTouchPoint, mRegion);
147
      mMovingDeform    = new VertexEffectDeform ( movingDeformDynamic   , deformRadius, mTouchPoint, mRegion);
148
      mMovingShear     = new MatrixEffectShear  ( movingShearDynamic    ,               mTouchPoint         );
149
      mReleasedDistort = new VertexEffectDistort( releasedDistortDynamic,               mTouchPoint, mRegion);
150
      mReleasedDeform  = new VertexEffectDeform ( releasedDeformDynamic , deformRadius, mTouchPoint, mRegion);
151
      mReleasedShear   = new MatrixEffectShear  ( releasedShearDynamic  ,               mTouchPoint         );
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
   void setMode(EffectName mode)
157
      {
158
      mMode = mode;  
159
      }
160
   
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
   void setRegionRadius(int r)
164
      {
165
      mRadius = ( r==100 ? 100.0f : r/100.0f);
166
      mRegion.set3(mRadius);
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
// keep aborting the 'released' effects, otherwise we are quickly going to run out of room in
171
// effect queues.
172

    
173
   public void effectFinished(final long effectID)
174
     {
175
     mEffects.abortById(effectID);
176
     }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
   public void onDrawFrame(GL10 glUnused)
181
     {
182
     mScreen.render(System.currentTimeMillis());
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
188
     {
189
     scrHeight = height;
190
     scrWidth  = width;
191

    
192
     mRegion.set3(mRadius);
193

    
194
     Canvas stretchCanvas;
195
     textureWidth = (int)(0.6f*width);
196
     textureHeight= (int)(0.6f*height);
197

    
198
     if( mMesh!=null ) mMesh.markForDeletion();
199
     mMesh = new MeshRectangles(50,50*textureHeight/textureWidth);
200

    
201
     mScale.set(textureWidth,textureHeight,1);
202

    
203
     Bitmap stretchBitmap = Bitmap.createBitmap(textureWidth,textureHeight, Bitmap.Config.ARGB_8888);
204
     stretchCanvas = new Canvas(stretchBitmap);
205

    
206
     Paint paint = new Paint();
207
     paint.setColor(0xff008800);
208
     paint.setStyle(Style.FILL);
209
     stretchCanvas.drawRect(0, 0, textureWidth, textureHeight, paint);
210
     paint.setColor(0xffffffff);
211

    
212
     for(int i=0; i<=NUM_LINES ; i++ )
213
       {
214
       stretchCanvas.drawRect(textureWidth*i/NUM_LINES-1,                           0, textureWidth*i/NUM_LINES+1, textureHeight              , paint);
215
       stretchCanvas.drawRect(                         0, textureHeight*i/NUM_LINES-1, textureWidth              , textureHeight*i/NUM_LINES+1, paint);
216
       }
217

    
218
     mTexture.setTexture(stretchBitmap);
219

    
220
     mScreen.detachAll();
221
     mScreen.attach(mTexture,mEffects,mMesh);
222

    
223
     mScreen.resize(width, height);
224
     }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
     {
230
     VertexEffectDistort.enable();
231
     VertexEffectDeform.enable();
232

    
233
     DistortedLibrary.onCreate(mView.getContext(),this);
234
     }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
   public void distortedException(Exception ex)
239
     {
240
     android.util.Log.e("Deform", ex.getMessage() );
241
     }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
   void down(int x, int y)
246
     {
247
     float xt = (float)x/scrWidth-0.5f;
248
     float yt = 0.5f-(float)y/scrHeight;
249

    
250
     switch(mMode)
251
       {
252
       case DISTORT: vDistort[0].set(0,0,0);
253
                     mEffects.apply(mMovingDistort);
254
                     mLastEffect = mMovingDistort.getID();
255
                     mTouchPoint.set(xt,yt,0);
256
                     break;
257
       case DEFORM : vDeform[0].set(0,0,0);
258
                     mEffects.apply(mMovingDeform);
259
                     mLastEffect = mMovingDeform.getID();
260
                     mTouchPoint.set(xt,yt,0);
261
                     break;
262
       case SHEAR  : vShear[0].set(0,0,0);
263
                     mEffects.apply(mMovingShear);
264
                     mLastEffect = mMovingShear.getID();
265
                     mTouchPoint.set(xt,yt,0);
266
                     break;
267
       }
268
     }
269
    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
   void move(int x, int y)
273
     {
274
     float xm = (float)x/textureWidth;
275
     float ym = (float)y/textureHeight;
276

    
277
     switch(mMode)
278
       {
279
       case DISTORT: vDistort[0].set( xm,-ym, 0);
280
                     break;
281
       case DEFORM:  vDeform[0].set ( xm,-ym, 0);
282
                     break;
283
       case SHEAR:   vShear[0].set  ( xm,-ym, 0);
284
                     break;
285
       }
286
     }
287
    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
   void up()
291
     {
292
     mEffects.abortById(mLastEffect);
293

    
294
     final float DAMP = -0.65f;
295

    
296
     switch(mMode)
297
       {
298
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
299
                       {
300
                       vDistort[i].set( vDistort[i-1].get0()*DAMP, vDistort[i-1].get1()*DAMP, 0 );
301
                       }
302
                     vDistort[NUM_VECTORS-1].set(0,0,0);
303
                     mEffects.apply(mReleasedDistort);
304
                     mReleasedDistort.notifyWhenFinished(this);
305
                     break;
306
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
307
                       {
308
                       vDeform[i].set( vDeform[i-1].get0()*DAMP, vDeform[i-1].get1()*DAMP, 0 );
309
                       }
310
                     vDeform[NUM_VECTORS-1].set(0,0,0);
311
                     mEffects.apply(mReleasedDeform);
312
                     mReleasedDeform.notifyWhenFinished(this);
313
                     break;
314
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
315
                       {
316
                       vShear[i].set( vShear[i-1].get0()*DAMP, vShear[i-1].get1()*DAMP, 0 );
317
                       }
318
                     vShear[NUM_VECTORS-1].set(0,0,0);
319
                     mEffects.apply(mReleasedShear);
320
                     mReleasedShear.notifyWhenFinished(this);
321
                     break;
322
       }
323
     }
324
   }
(2-2/3)