Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deform / DeformRenderer.java @ 01782e85

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedScreen;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.main.MeshObject;
30
import org.distorted.library.main.MeshFlat;
31 e8b6aa95 Leszek Koltunski
32 7abd1d00 Leszek Koltunski
import org.distorted.library.EffectNames;
33 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36 427ab7bf Leszek Koltunski
37
import android.graphics.Bitmap;
38
import android.graphics.Canvas;
39
import android.graphics.Paint;
40
import android.graphics.Paint.Style;
41
import android.opengl.GLSurfaceView;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class DeformRenderer implements GLSurfaceView.Renderer 
46 f6d884d5 Leszek Koltunski
   {
47 e8b6aa95 Leszek Koltunski
   private static final int NUM_VECTORS =  8;
48
   private static final int NUM_LINES   = 10;
49 427ab7bf Leszek Koltunski
50
   private GLSurfaceView mView;
51 d3c2d1ef leszek
   private DistortedTexture stretchTexture;
52
   private DistortedEffects stretchEffects;
53
   private MeshObject stretchMesh;
54 d218d64e leszek
   private DistortedScreen mScreen;
55 7589635e Leszek Koltunski
   private Static3D touchPoint;
56 7abd1d00 Leszek Koltunski
57 30c71dd5 Leszek Koltunski
   private Dynamic3D mReleasedDistortDynamic;
58
   private Dynamic3D mMovingDistortDynamic;
59
   private Static3D[] vDistort;
60
   private Dynamic3D mReleasedDeformDynamic;
61
   private Dynamic3D mMovingDeformDynamic;
62
   private Static3D[] vDeform;
63
   private Dynamic3D mReleasedShearDynamic;
64
   private Dynamic3D mMovingShearDynamic;
65
   private Static3D[] vShear;
66
67 6161fe9a Leszek Koltunski
   private Static4D mRegion;
68 d3c2d1ef leszek
   private int scrHeight, scrWidth;
69
   private float mRadius;
70
71
   private EffectNames mMode = EffectNames.DISTORT;
72
   private long mLastEffect = -1;
73
74 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 30c71dd5 Leszek Koltunski
   DeformRenderer(GLSurfaceView view)
77 427ab7bf Leszek Koltunski
      { 
78
      mView = view;
79 f6d884d5 Leszek Koltunski
80 d04a4886 Leszek Koltunski
      stretchEffects = new DistortedEffects();
81 f4f3a440 Leszek Koltunski
      mRegion = new Static4D(0,0,0,0);
82 7589635e Leszek Koltunski
83 7abd1d00 Leszek Koltunski
      // DISTORT
84
      mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
85
      mReleasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
86
      mMovingDistortDynamic = new Dynamic3D(0,0.5f);
87
      mMovingDistortDynamic.setMode(Dynamic3D.MODE_PATH);
88 7589635e Leszek Koltunski
89 7abd1d00 Leszek Koltunski
      vDistort = new Static3D[NUM_VECTORS];
90 7589635e Leszek Koltunski
91 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_VECTORS; i++)
92
        {
93 7abd1d00 Leszek Koltunski
        vDistort[i] = new Static3D(0,0,0);
94
        mReleasedDistortDynamic.add(vDistort[i]);
95 427ab7bf Leszek Koltunski
        }
96 7589635e Leszek Koltunski
97 7abd1d00 Leszek Koltunski
      mMovingDistortDynamic.add(vDistort[0]);
98 7589635e Leszek Koltunski
99 7abd1d00 Leszek Koltunski
      // Deform
100
      mReleasedDeformDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
101
      mReleasedDeformDynamic.setMode(Dynamic3D.MODE_PATH);
102
      mMovingDeformDynamic = new Dynamic3D(0,0.5f);
103
      mMovingDeformDynamic.setMode(Dynamic3D.MODE_PATH);
104
105
      vDeform = new Static3D[NUM_VECTORS];
106
107
      for(int i=0; i<NUM_VECTORS; i++)
108 427ab7bf Leszek Koltunski
        {
109 7abd1d00 Leszek Koltunski
        vDeform[i] = new Static3D(0,0,0);
110
        mReleasedDeformDynamic.add(vDeform[i]);
111 427ab7bf Leszek Koltunski
        }
112 7abd1d00 Leszek Koltunski
113
      mMovingDeformDynamic.add(vDeform[0]);
114
115
      // Shear
116
      mReleasedShearDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
117
      mReleasedShearDynamic.setMode(Dynamic3D.MODE_PATH);
118
      mMovingShearDynamic = new Dynamic3D(0,0.5f);
119
      mMovingShearDynamic.setMode(Dynamic3D.MODE_PATH);
120
121
      vShear = new Static3D[NUM_VECTORS];
122
123
      for(int i=0; i<NUM_VECTORS; i++)
124
        {
125
        vShear[i] = new Static3D(0,0,0);
126
        mReleasedShearDynamic.add(vShear[i]);
127
        }
128
129
      mMovingShearDynamic.add(vShear[0]);
130 392e16fd Leszek Koltunski
131 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
132 716d9f38 leszek
      mScreen.setDebug(DistortedScreen.DEBUG_FPS);
133 427ab7bf Leszek Koltunski
      }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137 30c71dd5 Leszek Koltunski
   void setMode(EffectNames mode)
138 427ab7bf Leszek Koltunski
      {
139
      mMode = mode;  
140
      }
141
   
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 30c71dd5 Leszek Koltunski
   void setRegionRadius(int r)
145 427ab7bf Leszek Koltunski
      {
146 f4f3a440 Leszek Koltunski
      mRadius = ( r==100 ? 100.0f : r/100.0f);
147
      mRegion.set3(mRadius*scrWidth);
148 427ab7bf Leszek Koltunski
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
   
152 f6d884d5 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
153
     {
154 24ab1cf8 leszek
     mScreen.render(System.currentTimeMillis());
155 f6d884d5 Leszek Koltunski
     }
156 427ab7bf Leszek Koltunski
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159 f6d884d5 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
160
     {
161
     scrHeight = height;
162
     scrWidth  = width;
163 6161fe9a Leszek Koltunski
164 f4f3a440 Leszek Koltunski
     mRegion.set3(mRadius*scrWidth);
165
166 f6d884d5 Leszek Koltunski
     Canvas stretchCanvas;
167 b0ebdf5e Leszek Koltunski
     int w=width/2;
168
     int h=height/2;
169
170 5f2a53b6 leszek
     if( stretchMesh!=null ) stretchMesh.markForDeletion();
171
172 b01acdaf Leszek Koltunski
     stretchMesh = new MeshFlat(50,50*h/w);
173 f6d884d5 Leszek Koltunski
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
174
     stretchCanvas = new Canvas(stretchBitmap);
175 b0ebdf5e Leszek Koltunski
176 24ab1cf8 leszek
     Paint paint = new Paint();
177
     paint.setColor(0xff008800);
178
     paint.setStyle(Style.FILL);
179
     stretchCanvas.drawRect(0, 0, w, h, paint);
180
     paint.setColor(0xffffffff);
181 b0ebdf5e Leszek Koltunski
182 f6d884d5 Leszek Koltunski
     for(int i=0; i<=NUM_LINES ; i++ )
183
       {
184 24ab1cf8 leszek
       stretchCanvas.drawRect(w*i/NUM_LINES - 1,                 0,  w*i/NUM_LINES + 1,  h                , paint);
185
       stretchCanvas.drawRect(                0, h *i/NUM_LINES -1,  w                ,  h*i/NUM_LINES + 1, paint);
186 f6d884d5 Leszek Koltunski
       }
187 b0ebdf5e Leszek Koltunski
188 f6d884d5 Leszek Koltunski
     touchPoint= new Static3D(0,0,0);
189 b0ebdf5e Leszek Koltunski
190
     if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
191 f6d884d5 Leszek Koltunski
     stretchTexture.setTexture(stretchBitmap);
192 b0ebdf5e Leszek Koltunski
193 630703d1 leszek
     stretchEffects.abortAllEffects();
194
     stretchEffects.move( new Static3D(scrWidth/4,scrHeight/4,0) );
195
196 b0ebdf5e Leszek Koltunski
     mScreen.detachAll();
197
     mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
198
199
     mScreen.resize(width, height);
200
     }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
    
204
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
205
     {
206
     DistortedEffects.enableEffect(EffectNames.DISTORT);
207
     DistortedEffects.enableEffect(EffectNames.DEFORM);
208
209
     try
210
       {
211
       Distorted.onCreate(mView.getContext());
212
       }
213
     catch(Exception ex)
214
       {
215
       android.util.Log.e("DeformRenderer", ex.toString() );
216
       }
217 f6d884d5 Leszek Koltunski
     }
218 427ab7bf Leszek Koltunski
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221 f6d884d5 Leszek Koltunski
   void down(int x, int y)
222
     {
223
     int xt = x-scrWidth/4;
224
     int yt = y-scrHeight/4;
225 427ab7bf Leszek Koltunski
      
226 f6d884d5 Leszek Koltunski
     if( xt<0 ) xt=0;
227
     if( xt>scrWidth/2 ) xt=scrWidth/2;
228
     if( yt<0 ) yt=0;
229
     if( yt>scrHeight/2 ) yt=scrHeight/2;
230 630703d1 leszek
      
231
     touchPoint.set(xt,yt,0);
232 f6d884d5 Leszek Koltunski
233
     switch(mMode)
234
       {
235
       case DISTORT: vDistort[0].set(0,0,0);
236 392e16fd Leszek Koltunski
                     mLastEffect = stretchEffects.distort( mMovingDistortDynamic, touchPoint, mRegion);
237 f6d884d5 Leszek Koltunski
                     break;
238
       case DEFORM : vDeform[0].set(0,0,0);
239 392e16fd Leszek Koltunski
                     mLastEffect = stretchEffects.deform( mMovingDeformDynamic, touchPoint, mRegion);
240 f6d884d5 Leszek Koltunski
                     break;
241
       case SHEAR  : vShear[0].set(0,0,0);
242 392e16fd Leszek Koltunski
                     mLastEffect = stretchEffects.shear(mMovingShearDynamic, touchPoint);
243 f6d884d5 Leszek Koltunski
                     break;
244
       }
245
     }
246 427ab7bf Leszek Koltunski
    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 f6d884d5 Leszek Koltunski
   void move(int x, int y)
250
     {
251
     switch(mMode)
252
       {
253
       case DISTORT: vDistort[0].set(x,y);
254
                     break;
255
       case DEFORM:  vDeform[0].set(x,y);
256
                     break;
257
       case SHEAR:   vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
258
                     break;
259
       }
260
     }
261 427ab7bf Leszek Koltunski
    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264 f6d884d5 Leszek Koltunski
   void up()
265
     {
266 392e16fd Leszek Koltunski
     stretchEffects.abortEffect(mLastEffect);
267 f6d884d5 Leszek Koltunski
268
     float damp = -0.65f;
269
270
     switch(mMode)
271
       {
272
       case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
273
                       {
274
                       vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
275
                       }
276
                     vDistort[NUM_VECTORS-1].set(0,0);
277 392e16fd Leszek Koltunski
                     stretchEffects.distort( mReleasedDistortDynamic, touchPoint, mRegion);
278 f6d884d5 Leszek Koltunski
                     break;
279
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
280
                       {
281
                       vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
282
                       }
283
                     vDeform[NUM_VECTORS-1].set(0,0);
284 392e16fd Leszek Koltunski
                     stretchEffects.deform( mReleasedDeformDynamic, touchPoint, mRegion);
285 f6d884d5 Leszek Koltunski
                     break;
286
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
287
                       {
288
                       vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
289
                       }
290
                     vShear[NUM_VECTORS-1].set(0,0);
291 392e16fd Leszek Koltunski
                     stretchEffects.shear(mReleasedShearDynamic, touchPoint);
292 f6d884d5 Leszek Koltunski
                     break;
293
       }
294
     }
295
   }