Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ e4330c89

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