Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 42ec9110

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