Project

General

Profile

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

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

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.MatrixEffectMove;
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.Distorted;
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.MeshFlat;
36

    
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39
import org.distorted.library.type.Dynamic3D;
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
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;
62

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

    
67
   private Static4D mRegion;
68
   private Static3D mMove;
69
   private int scrHeight, scrWidth;
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
      mEffects    = new DistortedEffects();
86
      mRegion     = new Static4D(0,0,0,0);
87
      mMove       = new Static3D(0,0,0);
88
      mTouchPoint = new Static3D(0,0,0);
89

    
90
      mEffects.registerForMessages(this);
91

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

    
98
      vDistort = new Static3D[NUM_VECTORS];
99

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

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

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

    
114
      vDeform = new Static3D[NUM_VECTORS];
115

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

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

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

    
130
      vShear = new Static3D[NUM_VECTORS];
131

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

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

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

    
143
      mMovingDistort   = new VertexEffectDistort( movingDistortDynamic  , mTouchPoint, mRegion);
144
      mMovingDeform    = new VertexEffectDeform ( movingDeformDynamic   , mTouchPoint, mRegion);
145
      mMovingShear     = new MatrixEffectShear  ( movingShearDynamic    , mTouchPoint         );
146
      mReleasedDistort = new VertexEffectDistort( releasedDistortDynamic, mTouchPoint, mRegion);
147
      mReleasedDeform  = new VertexEffectDeform ( releasedDeformDynamic , mTouchPoint, mRegion);
148
      mReleasedShear   = new MatrixEffectShear  ( releasedShearDynamic  , mTouchPoint         );
149

    
150
      mEffects.apply(new MatrixEffectMove(mMove));
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

    
162
   void setRegionRadius(int r)
163
      {
164
      mRadius = ( r==100 ? 100.0f : r/200.0f);
165
      mRegion.set4(mRadius*scrWidth);
166
      }
167

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

    
172
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
173
     {
174
     switch(em)
175
        {
176
        case EFFECT_FINISHED: mEffects.abortById(effectID); break;
177
        }
178
     }
179

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

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

    
194
     mRegion.set4(mRadius*scrWidth);
195

    
196
     Canvas stretchCanvas;
197
     int w=width/2;
198
     int h=height/2;
199

    
200
     if( mMesh!=null ) mMesh.markForDeletion();
201

    
202
     mMesh = new MeshFlat(50,50*h/w);
203
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, 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, w, h, paint);
210
     paint.setColor(0xffffffff);
211

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

    
218
     if( mTexture==null ) mTexture = new DistortedTexture(w,h);
219
     mTexture.setTexture(stretchBitmap);
220

    
221
     mMove.set(scrWidth/4,scrHeight/4,0);
222

    
223
     mScreen.detachAll();
224
     mScreen.attach(mTexture,mEffects,mMesh);
225

    
226
     mScreen.resize(width, height);
227
     }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
    
231
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
232
     {
233
     VertexEffectDistort.enable();
234
     VertexEffectDeform.enable();
235

    
236
     try
237
       {
238
       Distorted.onCreate(mView.getContext());
239
       }
240
     catch(Exception ex)
241
       {
242
       android.util.Log.e("Deform", ex.toString() );
243
       }
244
     }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
   void down(int x, int y)
249
     {
250
     int xt = x-scrWidth /4;
251
     int yt = y-scrHeight/4;
252
      
253
     if( xt<0 ) xt=0;
254
     if( xt>scrWidth /2 ) xt=scrWidth/2;
255
     if( yt<0 ) yt=0;
256
     if( yt>scrHeight/2 ) yt=scrHeight/2;
257
      
258
     mTouchPoint.set(xt, scrHeight/2-yt,0);   // OpenGL coord system and 2D coords have inverted Y axis
259

    
260
     switch(mMode)
261
       {
262
       case DISTORT: vDistort[0].set(0,0,0);
263
                     mEffects.apply(mMovingDistort);
264
                     mLastEffect = mMovingDistort.getID();
265
                     break;
266
       case DEFORM : vDeform[0].set(0,0,0);
267
                     mEffects.apply(mMovingDeform);
268
                     mLastEffect = mMovingDeform.getID();
269
                     break;
270
       case SHEAR  : vShear[0].set(0,0,0);
271
                     mEffects.apply(mMovingShear);
272
                     mLastEffect = mMovingShear.getID();
273
                     break;
274
       }
275
     }
276
    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
   void move(int x, int y)
280
     {
281
     switch(mMode)
282
       {
283
       case DISTORT: vDistort[0].set(x,-y,0);
284
                     break;
285
       case DEFORM:  vDeform[0].set(x,-y,0);
286
                     break;
287
       case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)(-y)/(scrHeight/2), 0);
288
                     break;
289
       }
290
     }
291
    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
   void up()
295
     {
296
     mEffects.abortById(mLastEffect);
297

    
298
     float damp = -0.65f;
299

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