Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.deform;
21 427ab7bf Leszek Koltunski
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25 42f65cb4 Leszek Koltunski
import org.distorted.library.effect.EffectName;
26 d1abb41e Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
27 d57f3a14 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectShear;
28
import org.distorted.library.effect.VertexEffectDeform;
29
import org.distorted.library.effect.VertexEffectDistort;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
35 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 e8b6aa95 Leszek Koltunski
37 b62eb334 Leszek Koltunski
import org.distorted.library.message.EffectListener;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 513b2e9c Leszek Koltunski
import org.distorted.library.type.Static1D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 427ab7bf Leszek Koltunski
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 061449ed Leszek Koltunski
class DeformRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
52 f6d884d5 Leszek Koltunski
   {
53 e8b6aa95 Leszek Koltunski
   private static final int NUM_VECTORS =  8;
54
   private static final int NUM_LINES   = 10;
55 427ab7bf Leszek Koltunski
56
   private GLSurfaceView mView;
57 b62eb334 Leszek Koltunski
   private DistortedTexture mTexture;
58
   private DistortedEffects mEffects;
59
   private MeshBase mMesh;
60 d218d64e leszek
   private DistortedScreen mScreen;
61 d1abb41e Leszek Koltunski
   private Static3D mTouchPoint, mScale;
62 7abd1d00 Leszek Koltunski
63 30c71dd5 Leszek Koltunski
   private Static3D[] vDistort;
64
   private Static3D[] vDeform;
65
   private Static3D[] vShear;
66
67 6161fe9a Leszek Koltunski
   private Static4D mRegion;
68 d3c2d1ef leszek
   private int scrHeight, scrWidth;
69 16b22aab Leszek Koltunski
   private int textureWidth, textureHeight;
70 d3c2d1ef leszek
   private float mRadius;
71
72 42f65cb4 Leszek Koltunski
   private EffectName mMode = EffectName.DISTORT;
73 d57f3a14 Leszek Koltunski
   private long mLastEffect = -1;
74
75
   private MatrixEffectShear mMovingShear, mReleasedShear;
76
   private VertexEffectDistort mMovingDistort, mReleasedDistort;
77
   private VertexEffectDeform mMovingDeform, mReleasedDeform;
78 d3c2d1ef leszek
79 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 30c71dd5 Leszek Koltunski
   DeformRenderer(GLSurfaceView view)
82 427ab7bf Leszek Koltunski
      { 
83
      mView = view;
84 f6d884d5 Leszek Koltunski
85 d1abb41e Leszek Koltunski
      mTexture    = new DistortedTexture();
86 698ad0a8 Leszek Koltunski
      mEffects    = new DistortedEffects();
87 a4d59c0b Leszek Koltunski
      mRegion     = new Static4D(0,0,0,0);
88
      mTouchPoint = new Static3D(0,0,0);
89 d1abb41e Leszek Koltunski
      mScale      = new Static3D(1,1,1);
90
91
      mEffects.apply( new MatrixEffectScale(mScale) );
92 7589635e Leszek Koltunski
93 7abd1d00 Leszek Koltunski
      // DISTORT
94 d57f3a14 Leszek Koltunski
      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 7589635e Leszek Koltunski
99 7abd1d00 Leszek Koltunski
      vDistort = new Static3D[NUM_VECTORS];
100 7589635e Leszek Koltunski
101 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_VECTORS; i++)
102
        {
103 7abd1d00 Leszek Koltunski
        vDistort[i] = new Static3D(0,0,0);
104 d57f3a14 Leszek Koltunski
        releasedDistortDynamic.add(vDistort[i]);
105 427ab7bf Leszek Koltunski
        }
106 7589635e Leszek Koltunski
107 d57f3a14 Leszek Koltunski
      movingDistortDynamic.add(vDistort[0]);
108 7589635e Leszek Koltunski
109 7abd1d00 Leszek Koltunski
      // Deform
110 d57f3a14 Leszek Koltunski
      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 7abd1d00 Leszek Koltunski
115
      vDeform = new Static3D[NUM_VECTORS];
116
117
      for(int i=0; i<NUM_VECTORS; i++)
118 427ab7bf Leszek Koltunski
        {
119 7abd1d00 Leszek Koltunski
        vDeform[i] = new Static3D(0,0,0);
120 d57f3a14 Leszek Koltunski
        releasedDeformDynamic.add(vDeform[i]);
121 427ab7bf Leszek Koltunski
        }
122 7abd1d00 Leszek Koltunski
123 d57f3a14 Leszek Koltunski
      movingDeformDynamic.add(vDeform[0]);
124 7abd1d00 Leszek Koltunski
125
      // Shear
126 d57f3a14 Leszek Koltunski
      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 7abd1d00 Leszek Koltunski
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 d57f3a14 Leszek Koltunski
        releasedShearDynamic.add(vShear[i]);
137 7abd1d00 Leszek Koltunski
        }
138
139 d57f3a14 Leszek Koltunski
      movingShearDynamic.add(vShear[0]);
140 392e16fd Leszek Koltunski
141 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
142 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
143 d57f3a14 Leszek Koltunski
144 513b2e9c Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156 42f65cb4 Leszek Koltunski
   void setMode(EffectName mode)
157 427ab7bf Leszek Koltunski
      {
158
      mMode = mode;  
159
      }
160
   
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163 30c71dd5 Leszek Koltunski
   void setRegionRadius(int r)
164 427ab7bf Leszek Koltunski
      {
165 d1abb41e Leszek Koltunski
      mRadius = ( r==100 ? 100.0f : r/100.0f);
166
      mRegion.set3(mRadius);
167 427ab7bf Leszek Koltunski
      }
168
169 b62eb334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
// keep aborting the 'released' effects, otherwise we are quickly going to run out of room in
171
// effect queues.
172
173 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
174 b62eb334 Leszek Koltunski
     {
175 8d5a8e06 Leszek Koltunski
     mEffects.abortById(effectID);
176 b62eb334 Leszek Koltunski
     }
177
178 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179 386fd702 Leszek Koltunski
180 f6d884d5 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
181
     {
182 24ab1cf8 leszek
     mScreen.render(System.currentTimeMillis());
183 f6d884d5 Leszek Koltunski
     }
184 427ab7bf Leszek Koltunski
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187 f6d884d5 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
188
     {
189
     scrHeight = height;
190
     scrWidth  = width;
191 6161fe9a Leszek Koltunski
192 d1abb41e Leszek Koltunski
     mRegion.set3(mRadius);
193 f4f3a440 Leszek Koltunski
194 f6d884d5 Leszek Koltunski
     Canvas stretchCanvas;
195 16b22aab Leszek Koltunski
     textureWidth = (int)(0.6f*width);
196
     textureHeight= (int)(0.6f*height);
197 b0ebdf5e Leszek Koltunski
198 b62eb334 Leszek Koltunski
     if( mMesh!=null ) mMesh.markForDeletion();
199 386fd702 Leszek Koltunski
     mMesh = new MeshRectangles(50,50*textureHeight/textureWidth);
200 d1abb41e Leszek Koltunski
201
     mScale.set(textureWidth,textureHeight,1);
202 698ad0a8 Leszek Koltunski
203 16b22aab Leszek Koltunski
     Bitmap stretchBitmap = Bitmap.createBitmap(textureWidth,textureHeight, Bitmap.Config.ARGB_8888);
204 f6d884d5 Leszek Koltunski
     stretchCanvas = new Canvas(stretchBitmap);
205 b0ebdf5e Leszek Koltunski
206 24ab1cf8 leszek
     Paint paint = new Paint();
207
     paint.setColor(0xff008800);
208
     paint.setStyle(Style.FILL);
209 16b22aab Leszek Koltunski
     stretchCanvas.drawRect(0, 0, textureWidth, textureHeight, paint);
210 24ab1cf8 leszek
     paint.setColor(0xffffffff);
211 b0ebdf5e Leszek Koltunski
212 f6d884d5 Leszek Koltunski
     for(int i=0; i<=NUM_LINES ; i++ )
213
       {
214 16b22aab Leszek Koltunski
       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 f6d884d5 Leszek Koltunski
       }
217 b0ebdf5e Leszek Koltunski
218 b62eb334 Leszek Koltunski
     mTexture.setTexture(stretchBitmap);
219 b0ebdf5e Leszek Koltunski
220
     mScreen.detachAll();
221 b62eb334 Leszek Koltunski
     mScreen.attach(mTexture,mEffects,mMesh);
222 b0ebdf5e Leszek Koltunski
223
     mScreen.resize(width, height);
224
     }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
     {
230 885b9cca leszek
     VertexEffectDistort.enable();
231
     VertexEffectDeform.enable();
232 b0ebdf5e Leszek Koltunski
233 061449ed Leszek Koltunski
     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 f6d884d5 Leszek Koltunski
     }
242 427ab7bf Leszek Koltunski
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245 f6d884d5 Leszek Koltunski
   void down(int x, int y)
246
     {
247 d1abb41e Leszek Koltunski
     float xt = (float)x/scrWidth-0.5f;
248
     float yt = 0.5f-(float)y/scrHeight;
249 f6d884d5 Leszek Koltunski
250
     switch(mMode)
251
       {
252
       case DISTORT: vDistort[0].set(0,0,0);
253 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingDistort);
254 ebb06a48 leszek
                     mLastEffect = mMovingDistort.getID();
255 e8e54972 Leszek Koltunski
                     mTouchPoint.set(xt,yt,0);
256 f6d884d5 Leszek Koltunski
                     break;
257
       case DEFORM : vDeform[0].set(0,0,0);
258 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingDeform);
259 d57f3a14 Leszek Koltunski
                     mLastEffect = mMovingDeform.getID();
260 e8e54972 Leszek Koltunski
                     mTouchPoint.set(xt,yt,0);
261 f6d884d5 Leszek Koltunski
                     break;
262
       case SHEAR  : vShear[0].set(0,0,0);
263 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingShear);
264 d57f3a14 Leszek Koltunski
                     mLastEffect = mMovingShear.getID();
265 849e0034 Leszek Koltunski
                     mTouchPoint.set(xt,yt,0);
266 f6d884d5 Leszek Koltunski
                     break;
267
       }
268
     }
269 427ab7bf Leszek Koltunski
    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272 f6d884d5 Leszek Koltunski
   void move(int x, int y)
273
     {
274 d1abb41e Leszek Koltunski
     float xm = (float)x/textureWidth;
275
     float ym = (float)y/textureHeight;
276
277 f6d884d5 Leszek Koltunski
     switch(mMode)
278
       {
279 d1abb41e Leszek Koltunski
       case DISTORT: vDistort[0].set( xm,-ym, 0);
280 f6d884d5 Leszek Koltunski
                     break;
281 d1abb41e Leszek Koltunski
       case DEFORM:  vDeform[0].set ( xm,-ym, 0);
282 f6d884d5 Leszek Koltunski
                     break;
283 d1abb41e Leszek Koltunski
       case SHEAR:   vShear[0].set  ( xm,-ym, 0);
284 f6d884d5 Leszek Koltunski
                     break;
285
       }
286
     }
287 427ab7bf Leszek Koltunski
    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290 f6d884d5 Leszek Koltunski
   void up()
291
     {
292 b62eb334 Leszek Koltunski
     mEffects.abortById(mLastEffect);
293 f6d884d5 Leszek Koltunski
294 d1abb41e Leszek Koltunski
     final float DAMP = -0.65f;
295 f6d884d5 Leszek Koltunski
296
     switch(mMode)
297
       {
298
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
299
                       {
300 d1abb41e Leszek Koltunski
                       vDistort[i].set( vDistort[i-1].get0()*DAMP, vDistort[i-1].get1()*DAMP, 0 );
301 f6d884d5 Leszek Koltunski
                       }
302 e3eab072 Leszek Koltunski
                     vDistort[NUM_VECTORS-1].set(0,0,0);
303 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedDistort);
304 8d5a8e06 Leszek Koltunski
                     mReleasedDistort.notifyWhenFinished(this);
305 f6d884d5 Leszek Koltunski
                     break;
306
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
307
                       {
308 d1abb41e Leszek Koltunski
                       vDeform[i].set( vDeform[i-1].get0()*DAMP, vDeform[i-1].get1()*DAMP, 0 );
309 f6d884d5 Leszek Koltunski
                       }
310 e3eab072 Leszek Koltunski
                     vDeform[NUM_VECTORS-1].set(0,0,0);
311 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedDeform);
312 8d5a8e06 Leszek Koltunski
                     mReleasedDeform.notifyWhenFinished(this);
313 f6d884d5 Leszek Koltunski
                     break;
314
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
315
                       {
316 d1abb41e Leszek Koltunski
                       vShear[i].set( vShear[i-1].get0()*DAMP, vShear[i-1].get1()*DAMP, 0 );
317 f6d884d5 Leszek Koltunski
                       }
318 e3eab072 Leszek Koltunski
                     vShear[NUM_VECTORS-1].set(0,0,0);
319 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedShear);
320 8d5a8e06 Leszek Koltunski
                     mReleasedShear.notifyWhenFinished(this);
321 f6d884d5 Leszek Koltunski
                     break;
322
       }
323
     }
324
   }