Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 42ec9110

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 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
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 42ec9110 Leszek Koltunski
   private static final float QUOT      = 0.6f;
56 427ab7bf Leszek Koltunski
57
   private GLSurfaceView mView;
58 b62eb334 Leszek Koltunski
   private DistortedTexture mTexture;
59
   private DistortedEffects mEffects;
60
   private MeshBase mMesh;
61 d218d64e leszek
   private DistortedScreen mScreen;
62 d1abb41e Leszek Koltunski
   private Static3D mTouchPoint, mScale;
63 7abd1d00 Leszek Koltunski
64 30c71dd5 Leszek Koltunski
   private Static3D[] vDistort;
65
   private Static3D[] vDeform;
66
   private Static3D[] vShear;
67
68 6161fe9a Leszek Koltunski
   private Static4D mRegion;
69 d3c2d1ef leszek
   private int scrHeight, scrWidth;
70 16b22aab Leszek Koltunski
   private int textureWidth, textureHeight;
71 d3c2d1ef leszek
   private float mRadius;
72
73 42f65cb4 Leszek Koltunski
   private EffectName mMode = EffectName.DISTORT;
74 d57f3a14 Leszek Koltunski
   private long mLastEffect = -1;
75
76
   private MatrixEffectShear mMovingShear, mReleasedShear;
77
   private VertexEffectDistort mMovingDistort, mReleasedDistort;
78
   private VertexEffectDeform mMovingDeform, mReleasedDeform;
79 d3c2d1ef leszek
80 52e8e76a Leszek Koltunski
   private Dynamic3D mReleasedDistortDynamic, mReleasedDeformDynamic, mReleasedShearDynamic;
81
82 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84 30c71dd5 Leszek Koltunski
   DeformRenderer(GLSurfaceView view)
85 427ab7bf Leszek Koltunski
      { 
86
      mView = view;
87 f6d884d5 Leszek Koltunski
88 d1abb41e Leszek Koltunski
      mTexture    = new DistortedTexture();
89 698ad0a8 Leszek Koltunski
      mEffects    = new DistortedEffects();
90 a4d59c0b Leszek Koltunski
      mRegion     = new Static4D(0,0,0,0);
91
      mTouchPoint = new Static3D(0,0,0);
92 d1abb41e Leszek Koltunski
      mScale      = new Static3D(1,1,1);
93
94
      mEffects.apply( new MatrixEffectScale(mScale) );
95 7589635e Leszek Koltunski
96 7abd1d00 Leszek Koltunski
      // DISTORT
97 52e8e76a Leszek Koltunski
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
98
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
99 d57f3a14 Leszek Koltunski
      Dynamic3D movingDistortDynamic = new Dynamic3D(0,0.5f);
100
      movingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
101 7589635e Leszek Koltunski
102 7abd1d00 Leszek Koltunski
      vDistort = new Static3D[NUM_VECTORS];
103 7589635e Leszek Koltunski
104 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_VECTORS; i++)
105
        {
106 7abd1d00 Leszek Koltunski
        vDistort[i] = new Static3D(0,0,0);
107 52e8e76a Leszek Koltunski
        mReleasedDistortDynamic.add(vDistort[i]);
108 427ab7bf Leszek Koltunski
        }
109 7589635e Leszek Koltunski
110 d57f3a14 Leszek Koltunski
      movingDistortDynamic.add(vDistort[0]);
111 7589635e Leszek Koltunski
112 7abd1d00 Leszek Koltunski
      // Deform
113 52e8e76a Leszek Koltunski
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
114
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
115 d57f3a14 Leszek Koltunski
      Dynamic3D movingDeformDynamic = new Dynamic3D(0,0.5f);
116
      movingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
117 7abd1d00 Leszek Koltunski
118
      vDeform = new Static3D[NUM_VECTORS];
119
120
      for(int i=0; i<NUM_VECTORS; i++)
121 427ab7bf Leszek Koltunski
        {
122 7abd1d00 Leszek Koltunski
        vDeform[i] = new Static3D(0,0,0);
123 52e8e76a Leszek Koltunski
        mReleasedDeformDynamic.add(vDeform[i]);
124 427ab7bf Leszek Koltunski
        }
125 7abd1d00 Leszek Koltunski
126 d57f3a14 Leszek Koltunski
      movingDeformDynamic.add(vDeform[0]);
127 7abd1d00 Leszek Koltunski
128
      // Shear
129 52e8e76a Leszek Koltunski
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
130
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
131 d57f3a14 Leszek Koltunski
      Dynamic3D movingShearDynamic = new Dynamic3D(0,0.5f);
132
      movingShearDynamic.setMode(Dynamic3D.MODE_PATH);
133 7abd1d00 Leszek Koltunski
134
      vShear = new Static3D[NUM_VECTORS];
135
136
      for(int i=0; i<NUM_VECTORS; i++)
137
        {
138
        vShear[i] = new Static3D(0,0,0);
139 52e8e76a Leszek Koltunski
        mReleasedShearDynamic.add(vShear[i]);
140 7abd1d00 Leszek Koltunski
        }
141
142 d57f3a14 Leszek Koltunski
      movingShearDynamic.add(vShear[0]);
143 392e16fd Leszek Koltunski
144 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
145 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
146 d57f3a14 Leszek Koltunski
147 513b2e9c Leszek Koltunski
      Static1D deformRadius = new Static1D(0.5f);
148
149 52e8e76a Leszek Koltunski
      mMovingDistort   = new VertexEffectDistort( movingDistortDynamic   ,               mTouchPoint, mRegion);
150
      mMovingDeform    = new VertexEffectDeform ( movingDeformDynamic    , deformRadius, mTouchPoint, mRegion);
151
      mMovingShear     = new MatrixEffectShear  ( movingShearDynamic     ,               mTouchPoint         );
152
      mReleasedDistort = new VertexEffectDistort( mReleasedDistortDynamic,               mTouchPoint, mRegion);
153
      mReleasedDeform  = new VertexEffectDeform ( mReleasedDeformDynamic , deformRadius, mTouchPoint, mRegion);
154
      mReleasedShear   = new MatrixEffectShear  ( mReleasedShearDynamic  ,               mTouchPoint         );
155 427ab7bf Leszek Koltunski
      }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159 42f65cb4 Leszek Koltunski
   void setMode(EffectName mode)
160 427ab7bf Leszek Koltunski
      {
161
      mMode = mode;  
162
      }
163
   
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166 30c71dd5 Leszek Koltunski
   void setRegionRadius(int r)
167 427ab7bf Leszek Koltunski
      {
168 d1abb41e Leszek Koltunski
      mRadius = ( r==100 ? 100.0f : r/100.0f);
169
      mRegion.set3(mRadius);
170 427ab7bf Leszek Koltunski
      }
171
172 b62eb334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// keep aborting the 'released' effects, otherwise we are quickly going to run out of room in
174
// effect queues.
175
176 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
177 b62eb334 Leszek Koltunski
     {
178 8d5a8e06 Leszek Koltunski
     mEffects.abortById(effectID);
179 b62eb334 Leszek Koltunski
     }
180
181 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182 386fd702 Leszek Koltunski
183 f6d884d5 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
184
     {
185 24ab1cf8 leszek
     mScreen.render(System.currentTimeMillis());
186 f6d884d5 Leszek Koltunski
     }
187 427ab7bf Leszek Koltunski
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
    
190 f6d884d5 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
191
     {
192
     scrHeight = height;
193
     scrWidth  = width;
194 6161fe9a Leszek Koltunski
195 d1abb41e Leszek Koltunski
     mRegion.set3(mRadius);
196 f4f3a440 Leszek Koltunski
197 f6d884d5 Leszek Koltunski
     Canvas stretchCanvas;
198 42ec9110 Leszek Koltunski
     textureWidth = (int)(QUOT*width);
199
     textureHeight= (int)(QUOT*height);
200 b0ebdf5e Leszek Koltunski
201 b62eb334 Leszek Koltunski
     if( mMesh!=null ) mMesh.markForDeletion();
202 f2085b96 Leszek Koltunski
     mMesh = new MeshSquare(50,50*textureHeight/textureWidth);
203 d1abb41e Leszek Koltunski
204
     mScale.set(textureWidth,textureHeight,1);
205 698ad0a8 Leszek Koltunski
206 16b22aab Leszek Koltunski
     Bitmap stretchBitmap = Bitmap.createBitmap(textureWidth,textureHeight, Bitmap.Config.ARGB_8888);
207 f6d884d5 Leszek Koltunski
     stretchCanvas = new Canvas(stretchBitmap);
208 b0ebdf5e Leszek Koltunski
209 24ab1cf8 leszek
     Paint paint = new Paint();
210
     paint.setColor(0xff008800);
211
     paint.setStyle(Style.FILL);
212 16b22aab Leszek Koltunski
     stretchCanvas.drawRect(0, 0, textureWidth, textureHeight, paint);
213 24ab1cf8 leszek
     paint.setColor(0xffffffff);
214 b0ebdf5e Leszek Koltunski
215 f6d884d5 Leszek Koltunski
     for(int i=0; i<=NUM_LINES ; i++ )
216
       {
217 16b22aab Leszek Koltunski
       stretchCanvas.drawRect(textureWidth*i/NUM_LINES-1,                           0, textureWidth*i/NUM_LINES+1, textureHeight              , paint);
218
       stretchCanvas.drawRect(                         0, textureHeight*i/NUM_LINES-1, textureWidth              , textureHeight*i/NUM_LINES+1, paint);
219 f6d884d5 Leszek Koltunski
       }
220 b0ebdf5e Leszek Koltunski
221 b62eb334 Leszek Koltunski
     mTexture.setTexture(stretchBitmap);
222 b0ebdf5e Leszek Koltunski
223
     mScreen.detachAll();
224 b62eb334 Leszek Koltunski
     mScreen.attach(mTexture,mEffects,mMesh);
225 b0ebdf5e Leszek Koltunski
226
     mScreen.resize(width, height);
227
     }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
    
231
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
232
     {
233 885b9cca leszek
     VertexEffectDistort.enable();
234
     VertexEffectDeform.enable();
235 b0ebdf5e Leszek Koltunski
236 7c72e798 Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(mView.getContext(),this);
237 061449ed Leszek Koltunski
     }
238
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
241
   public void distortedException(Exception ex)
242
     {
243
     android.util.Log.e("Deform", ex.getMessage() );
244 f6d884d5 Leszek Koltunski
     }
245 427ab7bf Leszek Koltunski
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248 f6d884d5 Leszek Koltunski
   void down(int x, int y)
249
     {
250 42ec9110 Leszek Koltunski
     float xt = (((float)x)/scrWidth-0.5f)/QUOT;
251
     float yt = (0.5f-((float)y)/scrHeight)/QUOT;
252 f6d884d5 Leszek Koltunski
253
     switch(mMode)
254
       {
255
       case DISTORT: vDistort[0].set(0,0,0);
256 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingDistort);
257 ebb06a48 leszek
                     mLastEffect = mMovingDistort.getID();
258 e8e54972 Leszek Koltunski
                     mTouchPoint.set(xt,yt,0);
259 f6d884d5 Leszek Koltunski
                     break;
260
       case DEFORM : vDeform[0].set(0,0,0);
261 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingDeform);
262 d57f3a14 Leszek Koltunski
                     mLastEffect = mMovingDeform.getID();
263 e8e54972 Leszek Koltunski
                     mTouchPoint.set(xt,yt,0);
264 f6d884d5 Leszek Koltunski
                     break;
265
       case SHEAR  : vShear[0].set(0,0,0);
266 b62eb334 Leszek Koltunski
                     mEffects.apply(mMovingShear);
267 d57f3a14 Leszek Koltunski
                     mLastEffect = mMovingShear.getID();
268 42ec9110 Leszek Koltunski
                     mTouchPoint.set(textureWidth*xt,textureHeight*yt,0);
269 f6d884d5 Leszek Koltunski
                     break;
270
       }
271
     }
272 427ab7bf Leszek Koltunski
    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275 f6d884d5 Leszek Koltunski
   void move(int x, int y)
276
     {
277 d1abb41e Leszek Koltunski
     float xm = (float)x/textureWidth;
278
     float ym = (float)y/textureHeight;
279
280 f6d884d5 Leszek Koltunski
     switch(mMode)
281
       {
282 d1abb41e Leszek Koltunski
       case DISTORT: vDistort[0].set( xm,-ym, 0);
283 f6d884d5 Leszek Koltunski
                     break;
284 d1abb41e Leszek Koltunski
       case DEFORM:  vDeform[0].set ( xm,-ym, 0);
285 f6d884d5 Leszek Koltunski
                     break;
286 d1abb41e Leszek Koltunski
       case SHEAR:   vShear[0].set  ( xm,-ym, 0);
287 f6d884d5 Leszek Koltunski
                     break;
288
       }
289
     }
290 427ab7bf Leszek Koltunski
    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293 f6d884d5 Leszek Koltunski
   void up()
294
     {
295 b62eb334 Leszek Koltunski
     mEffects.abortById(mLastEffect);
296 f6d884d5 Leszek Koltunski
297 d1abb41e Leszek Koltunski
     final float DAMP = -0.65f;
298 f6d884d5 Leszek Koltunski
299
     switch(mMode)
300
       {
301
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
302
                       {
303 d1abb41e Leszek Koltunski
                       vDistort[i].set( vDistort[i-1].get0()*DAMP, vDistort[i-1].get1()*DAMP, 0 );
304 f6d884d5 Leszek Koltunski
                       }
305 e3eab072 Leszek Koltunski
                     vDistort[NUM_VECTORS-1].set(0,0,0);
306 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedDistort);
307 52e8e76a Leszek Koltunski
                     mReleasedDistortDynamic.resetToBeginning();
308 8d5a8e06 Leszek Koltunski
                     mReleasedDistort.notifyWhenFinished(this);
309 f6d884d5 Leszek Koltunski
                     break;
310
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
311
                       {
312 d1abb41e Leszek Koltunski
                       vDeform[i].set( vDeform[i-1].get0()*DAMP, vDeform[i-1].get1()*DAMP, 0 );
313 f6d884d5 Leszek Koltunski
                       }
314 e3eab072 Leszek Koltunski
                     vDeform[NUM_VECTORS-1].set(0,0,0);
315 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedDeform);
316 52e8e76a Leszek Koltunski
                     mReleasedDeformDynamic.resetToBeginning();
317 8d5a8e06 Leszek Koltunski
                     mReleasedDeform.notifyWhenFinished(this);
318 f6d884d5 Leszek Koltunski
                     break;
319
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
320
                       {
321 d1abb41e Leszek Koltunski
                       vShear[i].set( vShear[i-1].get0()*DAMP, vShear[i-1].get1()*DAMP, 0 );
322 f6d884d5 Leszek Koltunski
                       }
323 e3eab072 Leszek Koltunski
                     vShear[NUM_VECTORS-1].set(0,0,0);
324 b62eb334 Leszek Koltunski
                     mEffects.apply(mReleasedShear);
325 52e8e76a Leszek Koltunski
                     mReleasedShearDynamic.resetToBeginning();
326 8d5a8e06 Leszek Koltunski
                     mReleasedShear.notifyWhenFinished(this);
327 f6d884d5 Leszek Koltunski
                     break;
328
       }
329
     }
330
   }