Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ 513b2e9c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.examples.movingeffects;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
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
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.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.effect.VertexEffectSwirl;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class MovingEffectsSurfaceView extends GLSurfaceView
47
    {
48
    private static final int LOOP_TIME = 5000;
49
    private static final int NUM_POINTS= 100;
50

    
51
    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
    public static final int EFFECT_CHROMA=4;
56
    public static final int EFFECT_SWIRL =5;
57

    
58
    private static final Object lock = new Object();
59

    
60
    private Dynamic3D mCenter;
61
    private Paint mPaint;
62
    private int moving;
63
    private int mScrWidth, mScrHeight;
64
    private long mTime = 0;
65
    
66
    private int mCurrEffect;
67
    private int mSize1, mSize2;
68
    private Static3D mDeform;
69
    private Static1D mRadius;
70

    
71
    private MovingEffectsRenderer mRenderer;
72

    
73
    private VertexEffectDeform mEffectDeform;
74
    private VertexEffectSink mEffectSink;
75
    private VertexEffectSwirl mEffectSwirl;
76
    private FragmentEffectChroma mEffectChroma;
77
    private FragmentEffectAlpha mEffectAlpha;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
    
81
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
82
      {
83
      super(context, attrs);
84

    
85
      mCurrEffect=EFFECT_POINTS;
86
      mPaint = new Paint();
87
      mPaint.setStyle(Style.FILL);
88
      moving = -1;
89
      
90
      mCenter = new Dynamic3D(LOOP_TIME,0.0f);
91
      mDeform = new Static3D(0,0,0.15f);
92
      mRadius = new Static1D(0);
93

    
94
      if(!isInEditMode())
95
        {
96
        mRenderer = new MovingEffectsRenderer(this);
97

    
98
        setFocusable(true);
99
        setFocusableInTouchMode(true);
100
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
101
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
102
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
103
        setRenderer(mRenderer);
104

    
105
        Static3D regionFrag = new Static3D(0.15f,0.15f,0.15f);
106
        Static4D regionVert = new Static4D(0,0,0,0.15f);
107

    
108
        mEffectDeform  = new VertexEffectDeform  ( mDeform          , mRadius, mCenter, regionVert);
109
        mEffectSink    = new VertexEffectSink    (new Static1D(10)  , mCenter, regionVert);
110
        mEffectSwirl   = new VertexEffectSwirl   (new Static1D(30)  , mCenter, regionVert);
111
        mEffectAlpha   = new FragmentEffectAlpha (new Static1D(0.5f), mCenter, regionFrag, true);
112
        mEffectChroma  = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mCenter, regionFrag, true);
113
        }
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    public void onSurfaceChanged(int width,int height)
119
      {
120
      int max = width>height ? width:height;
121

    
122
      mScrWidth  = width;
123
      mScrHeight = height;
124

    
125
      mRadius.set(max/2);
126

    
127
      mSize1 = max/200;
128
      mSize2 = max/80;
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    public int getCurrentEffect()
134
      {
135
      return mCurrEffect;
136
      }
137
    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void Bubble()
141
      {
142
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
143
      
144
      synchronized(lock)
145
        {
146
        DistortedEffects q = mRenderer.getEffects();
147
        q.abortByType(EffectType.VERTEX);
148
        q.abortByType(EffectType.FRAGMENT);
149
        q.apply(mEffectDeform);
150
        mCurrEffect = EFFECT_BUBBLE;
151
        }
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    public void Sink()
157
      {
158
      if( mCurrEffect==EFFECT_SINK ) return; 
159
         
160
      synchronized(lock)
161
        {
162
        DistortedEffects q = mRenderer.getEffects();
163
        q.abortByType(EffectType.VERTEX);
164
        q.abortByType(EffectType.FRAGMENT);
165
        q.apply(mEffectSink);
166
        mCurrEffect = EFFECT_SINK;
167
        }
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    public void Chroma()
173
      {
174
      if( mCurrEffect==EFFECT_CHROMA ) return;
175
         
176
      synchronized(lock)
177
        {
178
        DistortedEffects q = mRenderer.getEffects();
179
        q.abortByType(EffectType.VERTEX);
180
        q.abortByType(EffectType.FRAGMENT);
181
        q.apply(mEffectChroma);
182
        mCurrEffect = EFFECT_CHROMA;
183
        }
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
    public void Transparency()
189
      {
190
      if( mCurrEffect==EFFECT_TRANS ) return;   
191
      
192
      synchronized(lock)
193
        {
194
        DistortedEffects q = mRenderer.getEffects();
195
        q.abortByType(EffectType.VERTEX);
196
        q.abortByType(EffectType.FRAGMENT);
197
        q.apply(mEffectAlpha);
198
        mCurrEffect = EFFECT_TRANS;
199
        }
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    public void Swirl()
205
      {
206
      if( mCurrEffect==EFFECT_SWIRL ) return;   
207
      
208
      synchronized(lock)
209
        {
210
        DistortedEffects q = mRenderer.getEffects();
211
        q.abortByType(EffectType.VERTEX);
212
        q.abortByType(EffectType.FRAGMENT);
213
        q.apply(mEffectSwirl);
214
        mCurrEffect = EFFECT_SWIRL;
215
        }
216
      }
217
    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    public void Abort()
221
      {
222
      synchronized(lock)
223
        {
224
        DistortedEffects q = mRenderer.getEffects();
225
        q.abortByType(EffectType.VERTEX);
226
        q.abortByType(EffectType.FRAGMENT);
227
        mCenter.removeAll();
228
        mCurrEffect = EFFECT_POINTS;
229
        mRenderer.setRefresh();
230
        }
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
 
235
    public void drawCurve(Canvas c, long time)
236
      {
237
      synchronized(lock)
238
        {  
239
        int len = mCenter.getNumPoints();
240

    
241
        float[] drawCoord = new float[3];
242
        Static3D cu;
243

    
244
        if( len>=2 )
245
          {
246
          final float step = (float)LOOP_TIME/(NUM_POINTS+1);
247

    
248
          for(int i=0; i<NUM_POINTS; i++)
249
            {
250
            mPaint.setColor( 0xffffffff );
251
            mCenter.get( drawCoord, 0, (long)(i*step) );
252
            c.drawCircle( (drawCoord[0]+0.5f)*mScrWidth, 0.5f*mScrHeight-drawCoord[1]*mScrWidth, mSize1, mPaint );
253
            }
254
          }
255
     
256
        mPaint.setColor(0xffff0000);
257
      
258
        for(int curr=0; curr<len; curr++)
259
          {       
260
          cu = mCenter.getPoint(curr);
261
          c.drawCircle( (cu.get0()+0.5f)*mScrWidth, 0.5f*mScrHeight-cu.get1()*mScrWidth, mSize2, mPaint);
262
          }
263
        
264
        if( time-mTime > LOOP_TIME ) mTime = time;
265
        }
266
      }
267
       
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
    
270
    @Override
271
    public boolean onTouchEvent(MotionEvent event)
272
      {
273
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
274

    
275
      float xDown = event.getX()/mScrWidth - 0.5f;
276
      float yDown = (0.5f*mScrHeight - event.getY())/mScrWidth;
277

    
278
      switch(event.getAction())
279
        {
280
        case MotionEvent.ACTION_DOWN: float gx, gy;
281
                                      Static3D dv;
282
                                      int len = mCenter.getNumPoints();
283
                                 
284
                                      for(int g=0; g<len; g++)
285
                                        {
286
                                        dv = mCenter.getPoint(g);
287
                                        gx = dv.get0();
288
                                        gy = dv.get1();
289

    
290
                                        float Z = mDeform.get2()/7;
291

    
292
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
293
                                          {
294
                                          moving = g;
295
                                          break;
296
                                          }
297
                                        }
298
                                      if( moving<0 )
299
                                        {
300
                                        synchronized(lock)
301
                                          {
302
                                          mCenter.add(new Static3D(xDown,yDown,0));
303
                                          }
304
                                        }
305
                                      mRenderer.setRefresh();
306
                                      break;
307
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
308
                                        {
309
                                        mCenter.setPoint(moving, xDown, yDown, 0);
310
                                        }
311
                                      mRenderer.setRefresh();
312
                                      break;
313
        case MotionEvent.ACTION_UP  : moving = -1;
314
                                      break;
315
        }
316
      return true;
317
      }
318
  }
(3-3/3)