Project

General

Profile

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

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

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