Project

General

Profile

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

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

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