Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 1585ba24

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
20 5068fa06 Leszek Koltunski
package org.distorted.examples.movingeffects;
21 427ab7bf Leszek Koltunski
22 e4330c89 Leszek Koltunski
import android.app.ActivityManager;
23 427ab7bf Leszek Koltunski
import android.content.Context;
24 e4330c89 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 427ab7bf Leszek Koltunski
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.Paint.Style;
28
import android.opengl.GLSurfaceView;
29
import android.view.MotionEvent;
30
import android.util.AttributeSet;
31
32 6173632c Leszek Koltunski
import org.distorted.library.effect.EffectType;
33
import org.distorted.library.effect.FragmentEffectAlpha;
34
import org.distorted.library.effect.FragmentEffectChroma;
35
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.effect.VertexEffectSwirl;
38 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
39 334c13fa Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43 427ab7bf Leszek Koltunski
44 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45 427ab7bf Leszek Koltunski
46
public class MovingEffectsSurfaceView extends GLSurfaceView
47
    {
48
    private static final int LOOP_TIME = 5000;
49 97dadfe5 Leszek Koltunski
    private static final int NUM_POINTS= 100;
50 6173632c Leszek Koltunski
51 427ab7bf Leszek Koltunski
    public static final int EFFECT_POINTS=0;
52
    public static final int EFFECT_BUBBLE=1;
53
    public static final int EFFECT_SINK  =2;
54
    public static final int EFFECT_TRANS =3;
55 4348b52e Leszek Koltunski
    public static final int EFFECT_CHROMA=4;
56 427ab7bf Leszek Koltunski
    public static final int EFFECT_SWIRL =5;
57 59759251 Leszek Koltunski
58 97dadfe5 Leszek Koltunski
    private static final Object lock = new Object();
59 4562f295 Leszek Koltunski
60 1585ba24 Leszek Koltunski
    private Dynamic3D mCenter;
61 398fb2ee Leszek Koltunski
    private Static4D mRegionVertex;
62 1585ba24 Leszek Koltunski
    private Static3D mRegionFragment;
63 4562f295 Leszek Koltunski
64
    private Paint mPaint;
65
    private int moving;
66
    private long mTime = 0;
67 427ab7bf Leszek Koltunski
    
68 4562f295 Leszek Koltunski
    private int mCurrEffect;
69 1585ba24 Leszek Koltunski
    private int mSize1, mSize2;
70 398fb2ee Leszek Koltunski
    private Static3D mDistort;
71 97dadfe5 Leszek Koltunski
72 f6d884d5 Leszek Koltunski
    private MovingEffectsRenderer mRenderer;
73
74 6173632c Leszek Koltunski
    private VertexEffectDistort mEffectDistort;
75
    private VertexEffectSink mEffectSink;
76
    private VertexEffectSwirl mEffectSwirl;
77
    private FragmentEffectChroma mEffectChroma;
78
    private FragmentEffectAlpha mEffectAlpha;
79
80 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81 427ab7bf Leszek Koltunski
    
82 41a81a14 Leszek Koltunski
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
83 427ab7bf Leszek Koltunski
      {
84 41a81a14 Leszek Koltunski
      super(context, attrs);
85 59759251 Leszek Koltunski
86 427ab7bf Leszek Koltunski
      mCurrEffect=EFFECT_POINTS;
87
      mPaint = new Paint();
88
      mPaint.setStyle(Style.FILL);
89
      moving = -1;
90
      
91 1585ba24 Leszek Koltunski
      mCenter         = new Dynamic3D(LOOP_TIME,0.0f);
92
      mRegionFragment = new Static3D(0,0,0);
93 398fb2ee Leszek Koltunski
      mRegionVertex   = new Static4D(0,0,0,0);
94
      mDistort        = new Static3D(0,0,0);
95 59759251 Leszek Koltunski
96 427ab7bf Leszek Koltunski
      if(!isInEditMode())
97
        {
98 e4330c89 Leszek Koltunski
        mRenderer = new MovingEffectsRenderer(this);
99
100 427ab7bf Leszek Koltunski
        setFocusable(true);
101
        setFocusableInTouchMode(true);
102 e4330c89 Leszek Koltunski
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
103
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
104
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
105 f6d884d5 Leszek Koltunski
        setRenderer(mRenderer);
106 6173632c Leszek Koltunski
107 1585ba24 Leszek Koltunski
        mEffectDistort = new VertexEffectDistort ( mDistort         , mCenter, mRegionVertex);
108
        mEffectSink    = new VertexEffectSink    (new Static1D(10)  , mCenter, mRegionVertex);
109
        mEffectSwirl   = new VertexEffectSwirl   (new Static1D(30)  , mCenter, mRegionVertex);
110
        mEffectAlpha   = new FragmentEffectAlpha (new Static1D(0.5f), mCenter, mRegionFragment, true);
111
        mEffectChroma  = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mCenter, mRegionFragment, true);
112 e4330c89 Leszek Koltunski
        }
113 427ab7bf Leszek Koltunski
      }
114
115 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116 427ab7bf Leszek Koltunski
117 4562f295 Leszek Koltunski
    public void onSurfaceChanged(int width,int height)
118 427ab7bf Leszek Koltunski
      {
119 398fb2ee Leszek Koltunski
      int max = width>height ? width:height;
120 97dadfe5 Leszek Koltunski
121 398fb2ee Leszek Koltunski
      mSize1 = max/200;
122
      mSize2 = max/80;
123 1585ba24 Leszek Koltunski
      int rad= max/6;
124 97dadfe5 Leszek Koltunski
125 1585ba24 Leszek Koltunski
      mRegionVertex.set(0,0,0,rad);
126
      mRegionFragment.set(rad,rad,rad);
127 398fb2ee Leszek Koltunski
      mDistort.set3(max/10);
128 427ab7bf Leszek Koltunski
      }
129
130 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131 427ab7bf Leszek Koltunski
132 4562f295 Leszek Koltunski
    public int getCurrentEffect()
133 427ab7bf Leszek Koltunski
      {
134
      return mCurrEffect;
135
      }
136
    
137 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
138 427ab7bf Leszek Koltunski
    
139 4562f295 Leszek Koltunski
    public void Bubble()
140 427ab7bf Leszek Koltunski
      {
141
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
142
      
143
      synchronized(lock)
144
        {
145 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
146 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
147
        q.abortByType(EffectType.FRAGMENT);
148
        q.apply(mEffectDistort);
149 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_BUBBLE;
150
        }
151
      }
152
153 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154 427ab7bf Leszek Koltunski
155 4562f295 Leszek Koltunski
    public void Sink()
156 427ab7bf Leszek Koltunski
      {
157
      if( mCurrEffect==EFFECT_SINK ) return; 
158
         
159
      synchronized(lock)
160
        {
161 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
162 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
163
        q.abortByType(EffectType.FRAGMENT);
164
        q.apply(mEffectSink);
165 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SINK;
166
        }
167
      }
168
169 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170 427ab7bf Leszek Koltunski
171 4562f295 Leszek Koltunski
    public void Chroma()
172 427ab7bf Leszek Koltunski
      {
173 4348b52e Leszek Koltunski
      if( mCurrEffect==EFFECT_CHROMA ) return;
174 427ab7bf Leszek Koltunski
         
175
      synchronized(lock)
176
        {
177 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
178 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
179
        q.abortByType(EffectType.FRAGMENT);
180
        q.apply(mEffectChroma);
181 4348b52e Leszek Koltunski
        mCurrEffect = EFFECT_CHROMA;
182 427ab7bf Leszek Koltunski
        }
183
      }
184
185 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186 427ab7bf Leszek Koltunski
187 4562f295 Leszek Koltunski
    public void Transparency()
188 427ab7bf Leszek Koltunski
      {
189
      if( mCurrEffect==EFFECT_TRANS ) return;   
190
      
191
      synchronized(lock)
192
        {
193 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
194 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
195
        q.abortByType(EffectType.FRAGMENT);
196
        q.apply(mEffectAlpha);
197 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_TRANS;
198
        }
199
      }
200
201 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202 427ab7bf Leszek Koltunski
203 4562f295 Leszek Koltunski
    public void Swirl()
204 427ab7bf Leszek Koltunski
      {
205
      if( mCurrEffect==EFFECT_SWIRL ) return;   
206
      
207
      synchronized(lock)
208
        {
209 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
210 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
211
        q.abortByType(EffectType.FRAGMENT);
212
        q.apply(mEffectSwirl);
213 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_SWIRL;
214
        }
215
      }
216
    
217 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218 427ab7bf Leszek Koltunski
219 4562f295 Leszek Koltunski
    public void Abort()
220 427ab7bf Leszek Koltunski
      {
221
      synchronized(lock)
222
        {
223 d04a4886 Leszek Koltunski
        DistortedEffects q = mRenderer.getEffects();
224 6173632c Leszek Koltunski
        q.abortByType(EffectType.VERTEX);
225
        q.abortByType(EffectType.FRAGMENT);
226 1585ba24 Leszek Koltunski
        mCenter.removeAll();
227 427ab7bf Leszek Koltunski
        mCurrEffect = EFFECT_POINTS;
228 3c8b1903 Leszek Koltunski
        mRenderer.setRefresh();
229 427ab7bf Leszek Koltunski
        }
230
      }
231
232 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
233 427ab7bf Leszek Koltunski
 
234 4562f295 Leszek Koltunski
    public void drawCurve(Canvas c, long time)
235 427ab7bf Leszek Koltunski
      {
236
      synchronized(lock)
237
        {  
238 1585ba24 Leszek Koltunski
        int len = mCenter.getNumPoints();
239 97dadfe5 Leszek Koltunski
240 334c13fa Leszek Koltunski
        float[] drawCoord = new float[3];
241
        Static3D cu;
242 3c8b1903 Leszek Koltunski
243 427ab7bf Leszek Koltunski
        if( len>=2 )
244
          {
245 97dadfe5 Leszek Koltunski
          float step = (float)LOOP_TIME/(NUM_POINTS+1);
246
247
          for(int i=0; i<NUM_POINTS; i++)
248 427ab7bf Leszek Koltunski
            {
249 3c8b1903 Leszek Koltunski
            mPaint.setColor( 0xffffffff );
250 1585ba24 Leszek Koltunski
            mCenter.get( drawCoord, 0, (long)(i*step) );
251 97dadfe5 Leszek Koltunski
            c.drawCircle(drawCoord[0], drawCoord[1], mSize1, mPaint );
252 427ab7bf Leszek Koltunski
            }
253
          }
254
     
255
        mPaint.setColor(0xffff0000);
256
      
257
        for(int curr=0; curr<len; curr++)
258
          {       
259 1585ba24 Leszek Koltunski
          cu = mCenter.getPoint(curr);
260 e3eab072 Leszek Koltunski
          c.drawCircle(cu.get1(), cu.get2(), mSize2, mPaint);
261 427ab7bf Leszek Koltunski
          }
262
        
263
        if( time-mTime > LOOP_TIME ) mTime = time;
264
        }
265
      }
266
       
267 7bf107f7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268 427ab7bf Leszek Koltunski
    
269
    @Override public boolean onTouchEvent(MotionEvent event) 
270
      {
271
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
272 97dadfe5 Leszek Koltunski
273
      int xDown, yDown;
274
275 427ab7bf Leszek Koltunski
      switch(event.getAction())
276
        {
277 97dadfe5 Leszek Koltunski
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
278
                                      yDown = (int)event.getY();
279 427ab7bf Leszek Koltunski
                                    
280
                                      float gx, gy;
281 e3eab072 Leszek Koltunski
                                      Static3D dv;
282 1585ba24 Leszek Koltunski
                                      int len = mCenter.getNumPoints();
283 427ab7bf Leszek Koltunski
                                 
284
                                      for(int g=0; g<len; g++)
285
                                        {
286 1585ba24 Leszek Koltunski
                                        dv = mCenter.getPoint(g);
287 e3eab072 Leszek Koltunski
                                        gx = dv.get1();
288
                                        gy = dv.get2();
289 398fb2ee Leszek Koltunski
290 e3eab072 Leszek Koltunski
                                        float Z = mDistort.get3()/10;
291 398fb2ee Leszek Koltunski
292
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
293 427ab7bf Leszek Koltunski
                                          {
294
                                          moving = g;
295
                                          break;
296
                                          }
297
                                        }
298
                                      if( moving<0 )
299
                                        {
300
                                        synchronized(lock)
301
                                          {
302 1585ba24 Leszek Koltunski
                                          mCenter.add(new Static3D(xDown,yDown,0));
303 427ab7bf Leszek Koltunski
                                          }
304 3c8b1903 Leszek Koltunski
                                        }
305
                                      mRenderer.setRefresh();
306 427ab7bf Leszek Koltunski
                                      break;
307
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
308
                                        {
309 97dadfe5 Leszek Koltunski
                                        xDown = (int)event.getX();
310
                                        yDown = (int)event.getY();
311
312 1585ba24 Leszek Koltunski
                                        mCenter.setPoint(moving, xDown, yDown, 0);
313 3c8b1903 Leszek Koltunski
                                        }
314
                                      mRenderer.setRefresh();
315 427ab7bf Leszek Koltunski
                                      break;
316
        case MotionEvent.ACTION_UP  : moving = -1;
317
                                      break;
318
        }
319
      return true;
320
      }
321
  }